• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

failure with jar

 
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My application was working fine until I made a .jar out of it. Now, when I run the .jar application I get a NoClassDefFoundError exception. According to the error message, java cannot find the class called Student, even though the file Student.class is right there in the same folder as the class file that has the main() method. What could be the problem?
 
Author
Posts: 3473
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Example: Say, you have a class named �Pet� in a project folder �c:\myProject� and package named com.xyz.client, will you be able to compile and run it as it is?

package com.xyz.client;

public class Pet {
public static void main(String[] args) {
System.out.println("I am found in the classpath");
}
}

To run c:\myProject> java com.xyz.client.Pet

The answer is no and you will get the following exception: �Exception in thread "main" java.lang.-NoClassDefFoundError: com/xyz/client/Pet�. You need to set the classpath. How can you do that? One of the following ways:

1.Set the operating system CLASSPATH environment variable to have the project folder �c:\myProject�. [Shown in the above diagram as the System �classpath class loader]

2.Set the operating system CLASSPATH environment variable to have a jar file �c:/myProject/client.jar�, which has the Pet.class file in it. [Shown in the above diagram as the System �classpath class loader].

3.Run it with �cp or �classpath option as shown below:

c:\>java �cp c:/myProject com.xyz.client.Pet
OR
c:\>java -classpath c:/myProject/client.jar com.xyz.client.Pet
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What command did you type exactly to create your JAR file?

Are you sure that Student.class is included in the JAR file? You can look what's inside the JAR with the following command:

jar tvf myjar.jar

What command are you using exactly to run the program inside the JAR file? Notice that there are two ways to do this:

1) Use the JAR file simply as a container for classes. When you do this, you must make sure that you put the JAR file in the classpath, by (a) using the -cp or -classpath switch, or (b) adding it to the CLASSPATH environment variable. To run a program inside a JAR, type something like:

java -cp myjar.jar com.mypackage.MyMainClass
(add anything else to the classpath if needed)

2) Create the JAR file as an executable JAR. This means you add a manifest file to it which contains the classpath and the name of the main class. You then run it like this:

java -jar myjar.jar

See The Java Tutorial: Packaging Programs in JAR Files
 
Ranch Hand
Posts: 694
Mac OS X Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
A .jar file has the same format as a .zip file.

One trick that you can do to see inside of your .jar file is to make a copy of it, change the extension from .jar to .zip and then unzip it.

Then, you'll be able to look into the resulting directory and see if your manifest file is correct and you'll be able to see the .class files that are in your .jar file.

Your manifest file is just a text file. The first line specifies the manifest file version. The second line specifies the main class file. Then there should be a couple of line-feed characters.

Kaydell
[ June 28, 2007: Message edited by: Kaydell Leavitt ]
 
Kevin Tysen
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you all for the advice. I figured out what I did wrong. In the book Head First Java, there is instruction for making a jar which reads like this:

jar -cvmf manifest.txt app1.jar *.class
OR
jar -cvmf manifest.txt app1.jar MyApp.class

When I first tried to make a jar, I tried the bottom example, and only one class was put into the jar. When I did the top example, everything worked okay.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic