Reputation: 73
Alas I have searched high and low for a solution to my problem including here and here but to no avail.
So the situation is that I have a folder called bin with all my class files including ImpulseResponseFunction. And in another folder called lib I have all the jar libraries. I have created the following manifest file in the META-INF folder:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_51-b00 (Oracle Corporation)
Main-Class: ImpulseResponseFunction
So I create the jar using:
jar cvfm magicCarbon.jar META-INF/MANIFEST.MF bin/*.class -classpath lib
And I get the following output:
-classpath : no such file or directory
added manifest
adding: bin/ImpulseResponseFunction$1.class(in = 549) (out= 352)(deflated 35%)
adding: bin/ImpulseResponseFunction.class(in = 3523) (out= 2057)(deflated 41%)
adding: bin/menuBarSepUp$1.class(in = 703) (out= 443)(deflated 36%)
adding: bin/menuBarSepUp$2.class(in = 703) (out= 441)(deflated 37%)
adding: bin/menuBarSepUp$3.class(in = 900) (out= 556)(deflated 38%)
adding: bin/menuBarSepUp.class(in = 1683) (out= 942)(deflated 44%)
adding: bin/MyCanvas.class(in = 865) (out= 482)(deflated 44%)
adding: bin/openImage.class(in = 635) (out= 433)(deflated 31%)
adding: bin/PlotTimeSeries.class(in = 1760) (out= 974)(deflated 44%)
adding: bin/WindowSetUp.class(in = 3723) (out= 1952)(deflated 47%)
adding: lib/(in = 0) (out= 0)(stored 0%)
adding: lib/jfreechart-1.0.19-experimental.jar(in = 13791) (out= 11975)(deflated 13%)
adding: lib/jfreechart-1.0.19-src.jar(in = 2009391) (out= 1927691)(deflated 4%)
adding: lib/hamcrest-core-1.3.jar(in = 45024) (out= 39918)(deflated 11%)
adding: lib/jfreechart-1.0.19-swt.jar(in = 79793) (out= 74024)(deflated 7%)
adding: lib/junit-4.11.jar(in = 245039) (out= 215441)(deflated 12%)
adding: lib/swtgraphics2d.jar(in = 17492) (out= 16614)(deflated 5%)
adding: lib/jfreesvg-2.0.jar(in = 50769) (out= 47864)(deflated 5%)
adding: lib/jfreechart-1.0.19.jar(in = 1561720) (out= 1480724)(deflated 5%)
adding: lib/orsonpdf-1.6-eval.jar(in = 72444) (out= 67006)(deflated 7%)
adding: lib/jcommon-1.0.23.jar(in = 330246) (out= 305996)(deflated 7%)
adding: lib/servlet.jar(in = 80054) (out= 70343)(deflated 12%)
adding: lib/orsoncharts-1.4-eval-nofx.jar(in = 445025) (out= 412412)(deflated 7%)
adding: lib/magicCarbon.jar(in = 4681972) (out= 4682157)(deflated 0%)
So first of all what is going on with classpath?
Then when I run using:
java -jar magicCarbon.jar
I get:
Exception in thread "main" java.lang.NoClassDefFoundError: ImpulseResponseFunction
Caused by: java.lang.ClassNotFoundException: ImpulseResponseFunctio
at java.net.URLClassLoader$1.run(URLClassLoader.java:217)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:205)
at java.lang.ClassLoader.loadClass(ClassLoader.java:323)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:294)
at java.lang.ClassLoader.loadClass(ClassLoader.java:268)
Could not find the main class: ImpulseResponseFunction. Program will exit.
Why can't it find ImpulseResponseFunction?
The program works all fine and dandy when I run in runtime within Eclipse. The problems arise when I try to create and run jar file.
If it helps I am running Ubuntu.
Any help with this would be greatly appreciated.
Thanks
Upvotes: 1
Views: 708
Reputation: 73
Got there in the end with help from JB Nizet. Thank you.
The solution:
1) Ensure that the manifest file (META-INF/MANIFEST.MF) is correct and references the correct libraries using class path. In my case this is:
Manifest-Version: 1.0
Ant-Version: Apache Ant 1.8.2
Created-By: 1.7.0_51-b00 (Oracle Corporation)
Class-Path: lib/jcommon-1.0.23.jar lib/jfreechart-1.0.19.jar
Main-Class: ImpulseResponseFunction
The class-path needs to be relative to the path location of the jar file that you intend to create or you can put the full path to the libraries. In other words this works also:
Class-Path: /full/path/name/lib/jarball.jar
2) cd into the directory where main class is because this needs to be root of the manifest file.
3) Run the jar command. For me this is as JB Nizet stated:
jar cvfm ../magicCarbon.jar ../META-INF/MANIFEST.MF *.class
Upvotes: -1
Reputation: 691635
Your class is in the default package (which, BTW, is not a good idea). So it must be at the root of the jar. The bin directory is not a package, and must thus not be in the jar file. Instead of
jar cvfm magicCarbon.jar META-INF/MANIFEST.MF bin/*.class -classpath lib
use
cd bin
jar cvfm ../magicCarbon.jar ../META-INF/MANIFEST.MF *.class
Also, the libraries may not be inside the jar. They must stay outside. The Class-Path manifest entry tells which external library the jar depends on.
Upvotes: 2