1

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

2
  • What is your intent in calling jar with -classpath? Commented Mar 7, 2015 at 9:22
  • 1
    jar doesn't have a -classpath option. It just thinks you're trying to add a file called -classpath. Commented Mar 7, 2015 at 9:27

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I used: 'jar cvfm ../magicCarbon.jar ../META-INF/MANIFEST.MF .class' as you suggested and modified the manifest file by adding: 'Class-Path lib/' but I still get the error: 'Exception in thread "main" java.lang.NoClassDefFoundError: jar Caused by: java.lang.ClassNotFoundException: jar at java.net.URLClassLoader$1.run(URLClassLoader.java:217) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) Could not find the main class: jar. Program will exit.'
Sorry typo. I did use: Class-Path: lib/* in the manifest. I also specified all jar files explicitly e.g. 'lib/hamcrest-core-1.3.jar lib/jcommon-1.0.23.jar' etc, but the result was the same.
Could not find the main class: jar: how do you launch the jar? Why does java thinks the main class is named jar?
-1

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 

1 Comment

You can't put the full path to the libraries. A class-path item is is a relative URL whose base is the location of the containing JAR file. See the documentation.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.