SET(CMAKE_JAVA_COMPILER "@CMAKE_JAVA_COMPILER@")
SET(CMAKE_JAVA_RUNTIME  "@CMAKE_JAVA_RUNTIME@")
SET(CMAKE_JAVA_ARCHIVE  "@CMAKE_JAVA_ARCHIVE@")
SET(CMAKE_JAVA_COMPILER_LOADED 1)
SET(CMAKE_JAVA_RUNTIME_LOADED 1)
SET(CMAKE_JAVA_ARCHIVE_LOADED 1)

MACRO(ADD_JAVA_LIBRARY LIBRARYNAME SOURCES)
  IF(CMAKE_JAVA_COMPILER AND CMAKE_JAVA_ARCHIVE)
    SET(CMAKE_JAVA_INCLUDE_PATH ${CMAKE_JAVA_INCLUDE_PATH}
                          ${CMAKE_CURRENT_SOURCE_DIR} 
                          ${CMAKE_CURRENT_BINARY_DIR}
                          ${LIBRARY_OUTPUT_PATH})
    SET(CMAKE_JAVA_INCLUDE_PATH_TOTAL "")

    FOREACH(DIR ${CMAKE_JAVA_INCLUDE_PATH})
       SET(CMAKE_JAVA_INCLUDE_PATH_TOTAL "${CMAKE_JAVA_INCLUDE_PATH_TOTAL}:${DIR}")
    ENDFOREACH(DIR)
    IF(WIN32)
      STRING(REGEX REPLACE "/" "\\\\" CMAKE_JAVA_INCLUDE_PATH_TOTAL 
             ${CMAKE_JAVA_INCLUDE_PATH_TOTAL})
      STRING(REGEX REPLACE "/" "\\\\" CMAKE_JAVA_CLASS_OUTPUT_PATH
             ${CMAKE_CURRENT_BINARY_DIR})
      STRING(REGEX REPLACE ":([^\\/])" "\\\\;\\1" CMAKE_JAVA_INCLUDE_PATH_TOTAL
             ${CMAKE_JAVA_INCLUDE_PATH_TOTAL})
    ELSE(WIN32)
      SET(CMAKE_JAVA_CLASS_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
    ENDIF(WIN32)

    ADD_CUSTOM_TARGET(${LIBRARYNAME}_target ALL)

    SET(CMAKE_JAVA_ALL_CLASS_FILES "")
    FOREACH(SOURCE ${SOURCES})
      GET_FILENAME_COMPONENT(CLASS_FILE ${SOURCE} NAME_WE)
      ADD_CUSTOM_COMMAND(SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}
                         COMMAND ${CMAKE_JAVA_COMPILER}
                         ARGS ${CMAKE_JAVA_COMPILE_FLAGS} 
                              -classpath ${CMAKE_JAVA_INCLUDE_PATH_TOTAL} 
                              ${CMAKE_CURRENT_SOURCE_DIR}/${SOURCE}
                              -d ${CMAKE_JAVA_CLASS_OUTPUT_PATH}/
                         TARGET ${LIBRARYNAME}_target
                         OUTPUTS ${CLASS_FILE}.class
                         COMMENT "Java File" )
      SET(CMAKE_JAVA_ALL_CLASS_FILES ${CMAKE_JAVA_ALL_CLASS_FILES} ${CLASS_FILE}.class)
    ENDFOREACH(SOURCE)
    IF(LIBRARY_OUTPUT_PATH)
      SET(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${LIBRARY_OUTPUT_PATH}) 
    ELSE(LIBRARY_OUTPUT_PATH)
      SET(CMAKE_JAVA_LIBRARY_OUTPUT_PATH ${CMAKE_CURRENT_BINARY_DIR})
    ENDIF(LIBRARY_OUTPUT_PATH)
    ADD_CUSTOM_COMMAND(SOURCE ${CMAKE_JAVA_ALL_CLASS_FILES}
                       COMMAND ${CMAKE_COMMAND}
                       ARGS -E chdir ${CMAKE_CURRENT_BINARY_DIR}
                            ${CMAKE_JAVA_ARCHIVE} 
                            -cvf ${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}/${LIBRARYNAME}.jar *.class
                       TARGET ${LIBRARYNAME}_target
                       DEPENDS ${CMAKE_JAVA_ALL_CLASS_FILES}
                       OUTPUTS ${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}/${LIBRARYNAME}.jar
                       COMMENT "Java Archive")
    ADD_CUSTOM_COMMAND(SOURCE ${LIBRARYNAME}_target
                       TARGET ${LIBRARYNAME}_target
                       DEPENDS ${CMAKE_JAVA_LIBRARY_OUTPUT_PATH}/${LIBRARYNAME}.jar)
  ELSE(CMAKE_JAVA_COMPILER AND CMAKE_JAVA_ARCHIVE)
    MESSAGE(SEND_ERROR "Java compiler and/or Java archiver not found")
  ENDIF(CMAKE_JAVA_COMPILER AND CMAKE_JAVA_ARCHIVE)
ENDMACRO(ADD_JAVA_LIBRARY LIBRARYNAME SOURCES)