How do you execute a JAR file?

I'm sorry this is so basic, but I honestly do not know how to do this nor can I find anything online here or anywhere via Google to help me with this one.

I have a JAR file that I created on another machine using JDK 1.6.0 and NetBeans 5.5 on WinXP.

I copied and pasted that JAR file over to my machine at home also using JDK 1.6.0 and NetBeans 5.5 on WinXP

I put the exact directory name where the JAR file resides, "C:\Program Files\Java\jdk1.6.0\classes", in CLASSPATH and rebooted my machine.

However, upon CD'*** to that directory and doing "java -classpath . GUI.jar", I get a "NoClassDefFoundError" when trying to do so.

Furthermore, I was not sure how to use NetBeans to run the JAR file to execute the classes within.

What did I do wrong? I'm so sorry this is so basic.

Thanx

Phil

[847 byte] By [ppowell777a] at [2007-11-26 16:27:39]
# 1
http://java.sun.com/javase/6/docs/technotes/tools/windows/java.html
tschodta at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 2

You are using netbeans to build your jar. Go to your project properties (right click on the project name and choose properties). Then select "Run" from the tree. You will find a text box "Main Class" there, put the name of the class in there that has your main() method, including full package if you have one. That is WITHOUT the .class extension.

Build your jar, it will be placed in the dist directory of your netbeans project. You can now run it from netbeans itself if you want. If ever you want to run the jar from the command line on your home machine, just navigate to the directory with the jar file and execute this:

java -jar yourjar.jar

and you're off. If your runtime environment is propertly setup you can even execute the jar by simply double clicking on it from the windows explorer. No need to change the CLASSPATH variable at all, in fact it is ignored when executing a jarred application.

Also a note: you do not need to reboot your machine when you change an environment property. Simply change them using my computer -> properties -> advanced tab -> environmental variables. If you have any command prompts open, close and reopen them so the properties are updated properly.

gimbal2a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 3

> You are using netbeans to build your jar.

[snip]

No, I used a different netbeans to build my jar. The Netbeans I have right now has no project for me to run because all there is is a jar, so I cannot run the project that doesn't exist nor know how to create a project from a JAR nor import a project from a JAR as well, sorry

ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 4

> http://java.sun.com/javase/6/docs/technotes/tools/wind

> ows/java.html

That is what kept giving me NoClassDefFoundError each time I tried every single variation of

java -classpath . -jar GUI.jar

I could think of. Again, the exact same JAR that works on a different machine with the exact same JDK/JRE/JVM setup as this one

ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 5
> I tried every single variation of> > java -classpath . -jar GUI.jar> The '-classpath' option has no effect when using '-jar' . The classpath is taken from the manifest file. What is the content of your manifest file?
sabre150a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 6

> > I tried every single variation of

> >

> > java -classpath . -jar GUI.jar

> >

>

> The '-classpath' option has no effect when using

> '-jar' . The classpath is taken from the manifest

> file. What is the content of your manifest file?

Manifest-Version: 1.0

Ant-Version: Apache Ant 1.6.5

Created-By: 1.6.0-b105 (Sun Microsystems Inc.)

Main-Class: GUI.IconMaker

Class-Path:

X-COMMENT: Main-Class will be added automatically by build

ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 7
So your main class is IconMaker in package GUI (note the case) and you require no external jars. Is this right?
sabre150a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 8
> So your main class is IconMaker in package GUI (note> the case) and you require no external jars. Is this> right?I require no other external jars other than tools.jar I would think whose path is already defined in Path and CLASSPATH for good measure
ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 9

> > So your main class is IconMaker in package GUI

> (note

> > the case) and you require no external jars. Is

> this

> > right?

>

> I require no other external jars other than tools.jar

> I would think whose path is already defined in Path

> and CLASSPATH for good measure

Well, the only way I could get it to work is to blow open the JAR file using WinZip, then java IconMaker

works like a charm!

ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 10

> > > So your main class is IconMaker in package GUI

> > (note

> > > the case) and you require no external jars. Is

> > this

> > > right?

> >

> > I require no other external jars other than

> tools.jar

> > I would think whose path is already defined in

> Path

> > and CLASSPATH for good measure

>

> Well, the only way I could get it to work is to blow

> open the JAR file using WinZip, then java

> IconMaker

works like a charm!

Then your main class is not in package GUI so the Main-Class attribute should be just IconMaker and not GUI.IconMaker .

sabre150a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 11

> > > > So your main class is IconMaker in package GUI

> > > (note

> > > > the case) and you require no external jars. Is

> > > this

> > > > right?

> > >

> > > I require no other external jars other than

> > tools.jar

> > > I would think whose path is already defined in

> > Path

> > > and CLASSPATH for good measure

> >

> > Well, the only way I could get it to work is to

> blow

> > open the JAR file using WinZip, then java

> > IconMaker

works like a charm!

>

> Then your main class is not in package GUI so the

> Main-Class attribute should be just IconMaker and not

> GUI.IconMaker .

Ok I edited manifest.mf to read Main-Class to be "IconMaker", yet I still get this:

java -jar GUI.jar

Exception in thread "main": NoClassDefFoundError: GUI/IconMaker

Is there a tool that can edit files within a JAR file? WinZip is not working

ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 12
Expand the jar file using the 'jar' utility. Edit the manifiest. Re-jar using the 'jar' utility.
sabre150a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 13
> Expand the jar file using the 'jar' utility. Edit the> manifiest. Re-jar using the 'jar' utility.Upon doing so, when I do thisjava -jar GUI.jarNow I get the error:Failed to load Main-Class manifest attribute from GUI.jar
ppowell777a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 14

> java -jar GUI.jar

>

> Now I get the error:

>

> Failed to load Main-Class manifest attribute

> from GUI.jar

We can do nothing. You have to make sure the main class is in the jar file, that the manifiest correctly specifies the main class and that all the other classes are in the jar.

We can't see the jar content, we can't see your source, we can't see anything so YOU have to do the investigation yourself OR give us ALL the information.

sabre150a at 2007-7-8 22:51:53 > top of Java-index,Java Essentials,New To Java...
# 15

> > java -jar GUI.jar

> >

> > Now I get the error:

> >

> > Failed to load Main-Class manifest attribute

> > from GUI.jar

>

> We can do nothing. You have to make sure the main

> class is in the jar file, that the manifiest

> correctly specifies the main class and that all the

> other classes are in the jar.

>

> We can't see the jar content, we can't see your

> source, we can't see anything so YOU have to do the

> investigation yourself OR give us ALL the information.

I'll be happy to give you ALL the information. What email address do you have for me to send you GUI.jar?

ppowell777a at 2007-7-21 16:45:38 > top of Java-index,Java Essentials,New To Java...
# 16

.

>

> I'll be happy to give you ALL the information. What

> email address do you have for me to send you GUI.jar?

I don't and won't publish my email address. You will have to publish everything here. Start with your source, manifest file and pseudo directory listing of your jar.

sabre150a at 2007-7-21 16:45:38 > top of Java-index,Java Essentials,New To Java...
# 17

> .

> >

> > I'll be happy to give you ALL the information.

> What

> email address do you have for me to send you

> GUI.jar?

>

> I don't and won't publish my email address. You will

> have to publish everything here. Start with your

> source, manifest file and pseudo directory listing of

> your jar.

Don't have the source to publish and have already published the manifest.mf..

I will attempt to try to fix it tomorrow at work anyway; someone else on another thread told me NetBeans will create problems when you build a JAR file with its main class in the "default package" which is unnamed.

ppowell777a at 2007-7-21 16:45:38 > top of Java-index,Java Essentials,New To Java...
# 18

I've had similar problems.

Make sure that all packages your app accesses are in the appropriate folders. For example:

import somepackage.utils

class Blahblah {

...

}

On your development machine, somepackage.jar is in your C:/.../jdk/jre/lib/ext folder. On the target machine (assuming its not a development machine) that same jar should be in the C:/.../jre/lib/ext folder.

Your main executable jar's manifest should include the line

Main-Class: mypackage.MyClass

mokopaa at 2007-7-21 16:45:38 > top of Java-index,Java Essentials,New To Java...