hjalpmig
hjalpmig

Reputation: 702

NoClassDefFoundError occurs when running .jar file

For school we are learning about .jar files and how to create them. Below I have code for a simple GUI component and Viewer.

 package mp6;
 import java.awt.Color;
 import java.awt.Font;
 import java.awt.Graphics;
 import java.awt.Graphics2D;
 import javax.swing.JComponent;

public class HelloComponent extends JComponent 
{

public void draw(Graphics2D g2)
{
    g2.setFont(new Font("Arial", Font.ITALIC, 48));
    g2.setColor(Color.red);
    g2.drawString("Hello ", 40, 150);
    g2.setColor(Color.green);
    g2.drawString("Coloured ", 165, 150);
    g2.setColor(Color.blue);
    g2.drawString("JAR File", 380, 150);
}

public void paintComponent(Graphics g)
{
    Graphics2D g2 = (Graphics2D) g;
    draw(g2);
}
}

Viewer:

package mp6
import javax.swing.JFrame;


public class HelloViewer {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) 
{
    final int FRAME_WIDTH = 600;
    final int FRAME_HEIGHT = 300;

    HelloComponent comp = new HelloComponent();

    JFrame frame = new JFrame();
    frame.setSize(FRAME_WIDTH, FRAME_HEIGHT);
    frame.setTitle("Hello World in Colour");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.add(comp);
    frame.setVisible(true);
}

}

After using the command line to compile into .class files, I used the command

jar cfe Hello.jar HelloViewer HelloViewer.class HelloComponent.class

For the exercise we are required to use the -e command to set the entry point. This creates a .jar file named Hello.jar . If ran by double clicking the .jar, a Java Exception occurs. If ran via the command line it displays the error

"Exception in thread main java.lang.NoClassDefFoundError: HelloViewer (wrong name: mp6/HelloViewer)". 

What is causing this error? Have I used the right command to construct the .jar ?

Upvotes: 2

Views: 883

Answers (2)

AlexR
AlexR

Reputation: 115328

This happens because you have not created your jar correctly. Your classes belong to package and therefore must be in directory with the same name.

When you are running command

jar cfe Hello.jar HelloViewer HelloViewer.class HelloComponent.class

in the directory where your class files located you create jar without path mp6. I guess that your classes are in directory ./mp6, so go one directory up and run command

jar cfe mp6/Hello.jar mp6/HelloViewer mp6/HelloViewer.class HelloComponent.class

Then check the jar by running jar -vft yourjar.jar

You have to see something like:

mp6/Hello.jar 
mp6/HelloViewer 
mp6/HelloViewer.class 

now you can use your jar.

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 691635

No, you haven't used the right command.

Your classes are in the package mp6. So, inside the jar, the .class files must also be in a folder mp6. And the name of the main class is not HelloViewer, but mp6.HelloViewer.

So, cd into the parent directory (the one containing the mp6 folder), and use

jar cfe Hello.jar mp6.HelloViewer mp6

Upvotes: 2

Related Questions