I created a project in Eclipse and executed it succesfuly (It's a normal project, without Maven or Beans, really simple), I created a .jar with the project and tried to execute it but it throws the following error:
C:\Documents\wrapper-windows-x86-64-3.5.19-st\bin>java -cp QuoteHandler.jar stoc
k.view.Main
Initial SessionFactory creation failed.java.lang.NoClassDefFoundError: org/hiber
nate/cfg/Configuration
Exception in thread "Quotes" java.lang.ExceptionInInitializerError
at stock.controller.HollidayController.<clinit>(HollidayController.java:
25)
at stock.view.MainThread.run(MainThread.java:57)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NoClassDefFoundError: org/hibernate/cfg/Configuration
at stock.controller.HollidayController.<clinit>(HollidayController.java:
22)
... 2 more
Caused by: java.lang.ClassNotFoundException: org.hibernate.cfg.Configuration
at java.net.URLClassLoader$1.run(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 3 more
The .classpath file of the project has:
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre7"/>
<classpathentry kind="lib" path="lib/antlr-2.7.7.jar"/>
<classpathentry kind="lib" path="lib/dom4j-1.6.1.jar"/>
<classpathentry kind="lib" path="lib/hibernate-commons-annotations-4.0.1.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-core-4.1.10.Final.jar" sourcepath="lib/hibernate-core-4.1.10.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-entitymanager-4.1.10.Final.jar"/>
<classpathentry kind="lib" path="lib/hibernate-jpa-2.0-api-1.0.1.Final.jar"/>
<classpathentry kind="lib" path="lib/javassist-3.15.0-GA.jar"/>
<classpathentry kind="lib" path="lib/jboss-logging-3.1.0.GA.jar"/>
<classpathentry kind="lib" path="lib/jboss-transaction-api_1.1_spec-1.0.0.Final.jar"/>
<classpathentry kind="lib" path="lib/mysql-connector-java-5.1.20-bin.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.beans_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.web_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/org.springframework.context_3.1.4.RELEASE.jar"/>
<classpathentry kind="lib" path="lib/joda-time-2.2.jar"/>
<classpathentry kind="lib" path="lib/c3p0-0.9.1.jar"/>
<classpathentry kind="output" path="bin"/>
</classpath>
Checking on the Internet I modify the %CLASSPATH% variable to:
CLASSPATH = C:\Program Files\Java\jre7\lib;C:\Program Files\Java\jre7\lib\c3p0-0.9.1.jar
All the References are well configured in the Build Path of the project and it works, but when I'm executing the .jar it doesn't work (All the references are included in the jar). What am I missing?
when you specifically specify the class path when you run your program with -cp
you override the default CLASSPATH os variable.
So you need to either specify all the needed classes/jars as a list in -cp
, or you need to modify your CLASSPATH to have the directory of your jar. This question, Setting multiple jars in java classpath, describes setting multiple classpath elements.
Try following option from Eclipse:
Go to command prompt and change the dir to exported jar file and type java -jar <exported_jar>.jar
One note: In java for classpath lib/* does not resolve to all jars in directory, you have to type each and every jar entry manually.
You are trying to launch the program using command-line, the .classpath is file is for the eclipse project to maintain the list of jars pertaining to that project and has nothing to do with running the same code from command line, that's why you get the "java.lang.ClassNotFoundException"
Looking at the .classpath file, I think you project structure is like:
Proj
|
|---src
|
|---lib
So in order to run the code you can do,
For windows:
java -cp lib/*;/path/to/your/jar/QuoteHandler.jar stock.view.Main
For UNIX:
java -cp lib/*:/path/to/your/jar/QuoteHandler.jar stock.view.Main
Few things to note here: -cp argument is ignored if the -jar option is used.
1. So you have two options, modify the manifest file to include the classpath variables
2. Add this jar too in classpath along with other jars as mentioned above, and run the main class.
Also, using -cp will basically override the values of Environment Variable for that session. Next time again if you try the program again without -cp switch, default value (set in environment variable) of CLASSPATH will be picked.
UPDATE If you are using this command:
java -cp lib/*;QuoteHandler.jar stock.view.Main
1. Remove quotes they add no value
2. If I look at the command above it means that, you layout is something like:
somefolder
|
|----QuoteHandler.jar
|
|----lib
| |
| |--- antlr-2.7.7.jar
| |--- dom4j-1.6.1.jar
| |--- hibernate-commons-annotations-4.0.1.Final.jar
| |--- hibernate-core-4.1.10.Final.jar
| |---...and so on (NO SUBDIRECTORIES)
Is it the same structure you are having here?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With