Search code examples
javajarclassloaderclassnotfoundexception

ClassNotFoundException when using a self-created jar file


Using Eclipse I created some parser classes I want to provide to another project as a jar archive for validation purposes. So the parser project look like this:

ParserProject
- src
-- com.package.x
--- ClassA
--- ClassB
- lib
-- external1.jar
-- external2.jar

The ClassA and ClassB use the external jar archives, like Jackson or some Apache commons. To provide the functionality to another project, I exported the entire project as jar archive and executable jar archive (Right click on project > Export... > Java > JAR file > Select all files and "Export generated class files and resources" > Finish).

The jar file is created without any errors. When I use the parserproject.jar in my validation project, I can access all my methods using auto completion, but when I run the validation project, I get a java.lang.ClassNotFoundException: com.fasterxml.jackson.core.JsonParseException.

Now three strange things:

  1. All jackson jars are included in the parser project. Besides, I can run a main() method in the parser project and everything works fine, no ClassNotFoundException occurs.

  2. When I add the parserproject.jar to my validation project in the class path and open the jar archive in the Package Explorer, the parserproject.jar seems to contain all jars it needs.

  3. For the executable jar archive, all required external jars are contained in the MANIFEST.MF (Package Explorer > validation project > Referenced Libraries > + besides parserproject.jar > META-INF > MANIFEST.MF). It looks like this:

Manifest-Version: 1.0 Rsrc-Class-Path: ./ json-20140107.jar jackson-annotations-2.5.4.jar ja ckson-core-2.5.4.jar jackson-databind-2.5.4.jar commons-io-2.4.jar co mmons-validator-1.3.1.jar slf4j-api-1.7.5.jar slf4j-log4j12-1.7.5.jar json-schema-validator-2.2.6.jar jackson-module-jsonSchema-2.4.4.jar juniversalchardet-1.0.3.jar snakeyaml-1.15.jar commons-beanutils-1.7. 0.jar commons-digester-1.6.jar commons-logging-1.0.4.jar joda-time-2. 8.1.jar jopt-simple-4.6.jar jsr305-3.0.0.jar json-schema-core-1.2.5.j ar libphonenumber-6.2.jar jackson-coreutils-1.8.jar commons-lang-2.6. jar guava-16.0.1.jar msg-simple-1.1.jar btf-1.2.jar mailapi-1.4.3.jar uri-template-0.9.jar Class-Path: . Rsrc-Main-Class: com.package.SchemeValidator Main-Class: org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader

  1. I get the exception if and only if I use the generated jar file in my validation project. In case I get rid of the parserproject.jar and define a dependency to the ecplise parser project instead (Right click on validation project > Properties > Java Build Path > Projects) I do not get the ClassNotFoundException.

So now my question is, how I should export the jar so that every class is found. Thank you!


Solution

  • You might consider creating a so called fat jar which will contain all the needed classes. For example: http://fjep.sourceforge.net/

    If you do not want to go through the hassle of managing all the depencencies by yourself, consider using a build tool like Maven https://maven.apache.org/ or Gradle https://gradle.org/.