2
votes

I've a question which I'm hoping someone out there can help me with.

I've a two java projects one we shall call CORE which contains a bunch of classes that common functionality and provides hibernate interfaces / dao's etc to our db.

The other project(s) require CORE in order to interact with our DB and to utilise common core functionality.

Now I've created a Jar file using Gradle, imported the jar file via my IDE to the other projects and everything compiles correctly.

However when I try to run the other projects I get

Java.lang.NoClassDefFoundError: Could not initialise class

Now I understand that when creating a Jar file you need to provide a manifest file that details the entry point....however the Core project doesn't have any runnable main method....so how can I create my Jar such that all other projects can use CORE?

Thanks


Update

Ok so I think I need more help here guys (thanks for all the info so far)

Right so I have in my gradle file the jar as a compile and runtime dependancy

dependencies {
compile files('lib/CORE-all-1.0.jar')
runtime files('lib/CORE-all-1.0.jar')
compile 'org.testng:testng:6.9.6'
compile 'org.hamcrest:hamcrest-all:1.3'
compile 'com.fasterxml.jackson.core:jackson-annotations:2.6.1'
compile 'com.fasterxml.jackson.core:jackson-core:2.6.1'
compile 'com.fasterxml.jackson.core:jackson-databind:2.6.1'
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-xml:2.6.1'
compile 'org.codehaus.jackson:jackson-mapper-asl:1.9.13'
compile 'org.apache.httpcomponents:httpclient:4.5'
compile 'joda-time:joda-time:2.8.2'
compile 'org.slf4j:jcl-over-slf4j:1.7.12'
compile 'ch.qos.logback:logback-classic:1.1.3'
compile 'net.sf.ehcache:ehcache:2.10.1'

}

And if I print the class path I cans see the Jar :(

:printClasspath /Users/xxxxx/IdeaProjects/testProject/lib/CORE-all-1.0.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.testng/testng/6.9.6/e6788337be58ddc30c01d0ade783fb06cc825c7c/testng-6.9.6.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-all/1.3/63a21ebc981131004ad02e0434e799fd7f3a8d5a/hamcrest-all-1.3.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-annotations/2.6.1/f9661ddd2456d523b9428651c61e34b4ebf79f4e/jackson-annotations-2.6.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-core/2.6.1/892d15011456ea3563319b27bdd612dbc89bb776/jackson-core-2.6.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.core/jackson-databind/2.6.1/45c37a03be19f3e0db825fd7814d0bbec40b9e0/jackson-databind-2.6.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.dataformat/jackson-dataformat-xml/2.6.1/a6fbc5b4c5622cda94d86ccfe93208b954765ccd/jackson-dataformat-xml-2.6.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.codehaus.jackson/jackson-mapper-asl/1.9.13/1ee2f2bed0e5dd29d1cb155a166e6f8d50bbddb7/jackson-mapper-asl-1.9.13.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpclient/4.5/a1e6cbb3cc2c5f210dd1310ff9fcb2c09c0d1438/httpclient-4.5.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/joda-time/joda-time/2.8.2/d27c24204c5e507b16fec01006b3d0f1ec42aed4/joda-time-2.8.2.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.slf4j/jcl-over-slf4j/1.7.12/adef7a9e1263298255fdb5cb107ff171d07c82f3/jcl-over-slf4j-1.7.12.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-classic/1.1.3/d90276fff414f06cb375f2057f6778cd63c6082f/logback-classic-1.1.3.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/net.sf.ehcache/ehcache/2.10.1/a507a1302b631d0718fe29ae95d4e52e646ecc5a/ehcache-2.10.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.google.inject/guice/4.0/199b7acaa05b570bbccf31be998f013963e5e752/guice-4.0-no_aop.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.beanshell/bsh/2.0b4/a05f0a0feefa8d8467ac80e16e7de071489f0d9c/bsh-2.0b4.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant/1.7.0/9746af1a485e50cf18dcb232489032a847067066/ant-1.7.0.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.beust/jcommander/1.48/bfcb96281ea3b59d626704f74bc6d625ff51cbce/jcommander-1.48.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.yaml/snakeyaml/1.15/3b132bea69e8ee099f416044970997bde80f4ea6/snakeyaml-1.15.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/junit/junit/4.10/e4f1766ce7404a08f45d859fb9c226fc9e41a861/junit-4.10.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.fasterxml.jackson.module/jackson-module-jaxb-annotations/2.6.1/6e49b856747d22cc74fd0dddf8c584de1522916c/jackson-module-jaxb-annotations-2.6.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.codehaus.woodstox/stax2-api/3.1.4/ac19014b1e6a7c08aad07fe114af792676b685b7/stax2-api-3.1.4.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.codehaus.jackson/jackson-core-asl/1.9.13/3c304d70f42f832e0a86d45bd437f692129299a4/jackson-core-asl-1.9.13.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.apache.httpcomponents/httpcore/4.4.1/f5aa318bda4c6c8d688c9d00b90681dcd82ce636/httpcore-4.4.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/commons-logging/commons-logging/1.2/4bfc12adfe4842bf07b657f0369c4cb522955686/commons-logging-1.2.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/commons-codec/commons-codec/1.9/9ce04e34240f674bc72680f8b843b1457383161a/commons-codec-1.9.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.slf4j/slf4j-api/1.7.12/8e20852d05222dc286bf1c71d78d0531e177c317/slf4j-api-1.7.12.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/ch.qos.logback/logback-core/1.1.3/e3c02049f2dbbc764681b40094ecf0dcbc99b157/logback-core-1.1.3.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/javax.inject/javax.inject/1/6975da39a7040257bd51d21a231b76c915872d38/javax.inject-1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/aopalliance/aopalliance/1.0/235ba8b489512805ac13a8f9ea77a1ca5ebe3e8/aopalliance-1.0.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/com.google.guava/guava/16.0.1/5fa98cd1a63c99a44dd8d3b77e4762b066a5d0c5/guava-16.0.1.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.apache.ant/ant-launcher/1.7.0/e7e30789211e074aa70ef3eaea59bd5b22a7fa7a/ant-launcher-1.7.0.jar /Users/xxxxx/.gradle/caches/modules-2/files-2.1/org.hamcrest/hamcrest-core/1.1/860340562250678d1a344907ac75754e259cdb14/hamcrest-core-1.1.jar

And I'm still seeing the error :(

3
I don't think it is mandatory for all JARs to have a entry point (Main-class as you call it) ..hagrawal
What kind of other project and how you try to run it?v.ladynev
Just as an FYI it's another Java project. And the jar is being added via the IDE as a dependency.Philban
Also it's compiling fine as in it sees all the classes. However when it runs it spits out that errorPhilban
It's a runtime classpath error, rather than a compile time error. Clearly, your compilation classpath is fine. It's your runtime classpath where the problem is arising. Either the core lib needs to be included inside the jar and referred to in the manifest's Class-Path variable, or it is external and referred to directly via the classpath of the call to run the application jar (or environment variable, if you prefer).ManoDestra

3 Answers

1
votes

If your JAR file is a library, it doesn't get a Main-Class entry in its MANIFEST.MF. Libraries support other programs, they don't run.

If your JAR file is a program, it needs to include both the Main-Class entry in its MANIFEST.MF, and the manifest should probably mention the required JAR files with a Class-Path entry in MANIFEST.MF.

If your JAR file is a program that is embedded in other programs (it shouldn't be, but it happens) the OTHER programs will control the startup behavior. There is no such thing as a "library provided program launcher".

There are Java Frameworks, which are programs that are missing key pieces of functionality. In this case, your code is basically a library the framework loads and runs, and adding a Main-Class to your JAR file will not impact the Framework's Main-Class behavior, because the Framework is the launching program.

With this in mind, the IDE often maintains a separate Run configuration that doesn't always follow the conventions that you might use on the command line. You should review your IDE run configurations, and if possible align them to either chain to the desired command line call, or take measures to ensure they don't drift apart. Odds are, in your case, that your IDE lacks the two JAR files on the same CLASS_PATH, but the details of how to fix that are specific to your IDE and the types or projects you have created.

0
votes

You certainly have a classpath problem.

  • If you run your project from your IDE you need to add core dependencies too (like hibernate).

    Gradle can update your classpath with the idea plugin : https://docs.gradle.org/current/userguide/idea_plugin.html

  • If you run your project in command line you need to specify all your dependencies with classpath option. For example : java -cp "Test.jar;lib/*" my.package.MainClass.

    Or you can create a uberjar with wildfly-swarm or onejar.

0
votes

Your core jar simply needs to be included on the classpath of any applications that are using it.

E.g.

java -cp core.jar;app.jar path.to.main.class.in.app.jar

Or you can wrap up the core jar inside your app jar when you build it and add some values to the manifest, as follows:

Class-Path: core.jar
Main-Class: path.to.main.class.in.app.jar

Then build app.jar, including core.jar in the root...

jar cfm app.jar MANIFEST.MF pathToClasses core.jar

and call via...

java -jar app.jar

In this example, the core.jar is included on the classpath and app.jar knows its main class already, so the call is simpler.

Or create a "fat jar" with something like sbt-assembly or onejar.