0

So i created a project on eclipse, and i want to create a JAR file with it. The project folder contains the following folders:

  • src (contains two packages)
  • JRE System libraries
  • Referenced Libraries ("jasypt-1.9.2.jar" and "json_simple-1.1.jar")
  • libs (again: "jasypt-1.9.2.jar" and "json_simple-1.1.jar")
  • resources (containing images and files)

If i go to: rclick on Project's name -> Properties -> Java Build Path -> Libraries

I see both libraries correctly added to the Build Path.

Then i go to rclick on Project's name -> export -> JAR file and here i export everything (just to be sure..!). Then i put the MainClass as entry point and then the JAR file is created without any problem.

When i extract the JAR file (registry.jar), i see that it contains everything it needs: [package1, package2, META-INF, libs, resources, .classpath, .project]

Also, the .classpath is correct (at least, seems to me.. not 100% sure!):

   <?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src"/>
    <classpathentry exported="true" kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
    <classpathentry exported="true" kind="lib" path="libs/jasypt-1.9.2.jar"/>
    <classpathentry exported="true" kind="lib" path="libs/json_simple-1.1.jar"/>
    <classpathentry kind="output" path="bin"/>
</classpath>

BUT THEN when i go to execute the registry.jar file from command line (both Windows and Ubuntu), like this:

java -jar registry.jar

i get:

Exception in thread "main" java.lang.NoClassDefFoundError: org/json/simple/parser/ParseException
at ...
at ...
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
at java.net.URLClassLoader$1.run<Unknown Source>
at ...
at ...
... lots more

The program compile and works perfectly inside eclipse. I just can't figure out what's the problem...

OxPenguin
  • 93
  • 8
  • I think the referenced post will solve your problem. Having jars within a jar is not the usual thing to do. – Robert Moskal Aug 15 '15 at 16:19
  • extract your jar file and check the libraries exist or not by executing the command jar -xtvf – Shriram Aug 15 '15 at 16:20
  • @RobertMoskal thank you but it's not the same problem. I'm trying to generate the JAR directly from eclipse, then run it. – OxPenguin Aug 15 '15 at 16:58
  • I think it is the same problem. You are generating the jar and then trying to run it outside of eclipse. Having jars in the lib directory of a jar requires a special technique like using onejar. – Robert Moskal Aug 15 '15 at 17:02

1 Answers1

0

The .classpath file has no meaning for Java. It is only used by Eclipse. Java does not recognize that the needed libraries are under the lib entry in the Jar file.

You'd have to include the path of the libraries through the command:

java -jar registry.jar -cp lib

where in this case lib is a folder residing next to the exported Jar.

If you're using Maven, you can the assembly plugin to generate an executable file. See How can I create an executable JAR with dependencies using Maven?.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174