1

I am using CMake to compile jar file with add_jar command. Problem is that when I try to add INCLUDE_JARS to specify dependency to external jar, the code will not run. Here is the code example:

add_jar(testJar
    SOURCES
        sources/com/test/Main.java
    INCLUDE_JARS 
        ${CMAKE_SOURCE_DIR}/extern/org.json/json-20171018.jar
    ENTRY_POINT com.test.Main
)

Running the testJar with "java -jar testJar.jar" gives me the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException

The org.json jar should be in classpath, right? Adding manifest file with classpath solves the problem but is there way to do this without the manifest file?

And yes, I would use maven or gradle for building but as of restrictions in the project I cannot do that :)

drodil
  • 2,292
  • 1
  • 22
  • 37

1 Answers1

1

The option INCLUDE_JARS only makes sure that the given external jar is added to the class path upon compilation of the given Java source files. The jar file is neither copied to the build directory nor is it added to the jar file generated by add_jar.

If inclusion of the external jar in the Manifest file is not an option for you, manually add it to the class path upon running the testJar, i.e.:

java -cp path/to/json-20171018.jar -jar testJar.jar
sakra
  • 62,199
  • 16
  • 168
  • 151
  • Actually, adding the classpath in the command line is mutually exclusive with the `-jar` option (see [this answer](https://stackoverflow.com/a/250173/948128)) – piwi Nov 09 '18 at 22:53