I have this great little program that I have made in the JCreator IDE. It has four class files. All of the classes are in a package called org.jfree.panning. They are listed as follows:

XYPLOT.class*
PanningChartPanel.class
PannableXYPLOT.class
Pannable.class

* = Main Class

There are also four .java files corresponding to each .class file, if that helps.
My program also makes use of a third party library - JFreeChart.jar

The program compiles perfectly in the JCreator, but the problem for me is that I have absolutely no experience compiling jar executables from Java files. I tried to create a jar file inside of the JCreator IDE (I put "Main-Class: XYPLOT" in the manifest.txt), but when I double click the jar executable, it says it cant find the Main Class. Can anyone guide me in the right direction, or tell me what is wrong? Thank you for your help.

Recommended Answers

All 8 Replies

There are several steps to putting an app in a jar. A screwup anywhere will cause it to fail.
Create a manifest file with the Main-Class: line
Be careful of packages and case.
Add a Class-path: line for your third party jar
compile all the class files into a folder matching their packages
use the jar command to put the manifest and class files into the jar file.

Can you show:
what you have done
The full contents of the command prompt window when you try to execute the jar file.

There are several steps to putting an app in a jar. A screwup anywhere will cause it to fail.
Create a manifest file with the Main-Class: line
Be careful of packages and case.
Add a Class-path: line for your third party jar
compile all the class files into a folder matching their packages
use the jar command to put the manifest and class files into the jar file.

Can you show:
what you have done
The full contents of the command prompt window when you try to execute the jar file.

Sure thing. So I created a folder off my desktop called .org.jfree.panning. Inside the folder I have the third party library .jar, and the 4 class files. I compile a jar and modify the MANIFEST.MF inside to include the Main-Class line and the Class-Path line. The Main-Class is called XYPLOT. The .jar executable that is made is called XYPLOT.jar.

The first time I try to run the executable .jar produced, this is what command prompt gives me:

C:\Users\Luke\Desktop\org.jfree.panning>java -jar XYPLOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: XYPLOT (wrong name: org/jfree/panning/XYPLOT)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClassCond(ClassLoader.java:632)
at java.lang.ClassLoader.defineClass(ClassLoader.java:616)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:141)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:283)
at java.net.URLClassLoader.access$000(URLClassLoader.java:58)
at java.net.URLClassLoader$1.run(URLClassLoader.java:197)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: XYPLOT. Program will exit.

After it tells me that "XYPLOT" is the wrong name and says "org/jfree/panning/XYPLOT", I decide to make "org/jfree/panning/XYPLOT" the Main-Class path. Then, I run the command again. This is what I get back:

C:\Users\Luke\Desktop\org.jfree.panning>java -jar XYPLOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/jfree/panning/XYPLOT
Caused by: java.lang.ClassNotFoundException: org.jfree.panning.XYPLOT
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
Could not find the main class: org/jfree/panning/XYPLOT. Program will exit.

C:\Users\Luke\Desktop\org.jfree.panning>java -jar XYPLOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: XYPLOT (wrong name: org/jfree/panning/XYPLOT)

Thanks. That shows what I need.

The Main-Class: entry in the manifest file doesn't match the package path of the class file.

Please post the contents of the manifest file and the package statement from your program.

Also be sure that the paths of the class files in the jar file matches their package path.

Thanks. That shows what I need.

The Main-Class: entry in the manifest file doesn't match the package path of the class file.

Please post the contents of the manifest file and the package statement from your program.

Also be sure that the paths of the class files in the jar file matches their package path.

Thank you for the help.
I fooled around with my Manifest a bit, right now my Manifest.MF looks like this:

Manifest-Version: 1.0
Class-Path jfreechart-1.0.13.jar
Created-By: 1.6.0_18 (Sun Microsystems Inc.)
Main-Class: org.jfree.panning.XYPLOT

The package statement from all four class files looks like this in the code:

package org.jfree.panning;

Also, when you say that the paths of the class files match there package paths, I do not understand what you mean. When I open my .jar executable and look at its contents, all four of my .class files are are at the root.

The .class files in the jar file must be in folders/have paths that match the package they are in. You do that by issuing the jar command in the folder that contains the start of the package path.

Here's a batch file I use to create a jar file:

@Rem Create the HTTPServer.jar file:
SET JarName=%DEV_HOME%\JavaDevelopment\HTTPServer\HTTPServer.jar
%DEV_HOME%
cd %DEV_HOME%\JavaDevelopment\
jar -cf %JarName% HTTPServer\*.class NormsDev\Timer*.class Servlets\*.class NormsTools\SaveStdOutput*.class NormsDev\WarningBox*.class NormsTools\GetIntInput*.class NormsTools\GetInput*.class NormsTools\MakeEnterDoAction*.class NormsTools\EnvironVars*.class NormsTools\FindOurHome*.class NormsTools\FindInTA*.class NormsTools\ErrDialog*.class NormsTools\MessageArea*.class NormsTools\ChoiceOfYesOrNo*.class

cd %DEV_HOME%\JavaDevelopment\HTTPServer\
jar -ufm %JarName% HTTPServer.mnf HTTPServerFmJar.bat *.ini servlet.properties MyChat\*.class

ECHO OFF
ECHO ---- Created: %JarName% ----
@ECHO Remember to copy to \www for server
MORE

I have to change directory (cd) to get the correct paths for the .mnf, .ini and .properties files

The .class files in the jar file must be in folders/have paths that match the package they are in. You do that by issuing the jar command in the folder that contains the start of the package path.

Here's a batch file I use to create a jar file:


I have to change directory (cd) to get the correct paths for the .mnf, .ini and .properties files

AHH I see. So in this case, my .Jar directory would look like this.

org\jfree\panning\(All four class files here)

Here is a question. Do I put my third party library .jar at the root of the executable?

Did you add the "Class-Path: 3rdparty.jar"
entry to the manifest file? Notice the :

If the jar file is on the classpath, the JVM should be able to find the class files in the jar

Did you add the "Class-Path: 3rdparty.jar"
entry to the manifest file? Notice the :

If the jar file is on the classpath, the JVM should be able to find the class files in the jar

Ah yes yes it works now : ) thank you very much!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.