tags:

views:

767

answers:

5

i use the command line in windows to compile and then execute my java programs. i've gone to http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html and tried compiling the HelloWorldSwing.java class. it worked, but when i try "java HelloWorldSwing" it gives me a bunch of erros and says something along the lines of Exception in thread "main" java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)

i try running with java start/HelloWorldSwing and it says noClassDefFoundError. i get no errors with javac either. here's the code from the tutorial:

import javax.swing.*;        

public class HelloWorldSwing {
    /**
     * Create the GUI and show it.  For thread safety,
     * this method should be invoked from the
     * event-dispatching thread.
     */
    private static void createAndShowGUI() {
        //Create and set up the window.
        JFrame frame = new JFrame("HelloWorldSwing");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //Add the ubiquitous "Hello World" label.
        JLabel label = new JLabel("Hello World");
        frame.getContentPane().add(label);

        //Display the window.
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        //Schedule a job for the event-dispatching thread:
        //creating and showing this application's GUI.
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                createAndShowGUI();
            }
        });
    }
}

EDIT: used javaw

window pops up

"a java exception has occurred"

another window

"error: could not find the main class. error: a jni error has occurred, please check your installation and try again."

never had any problems running any java programs, am i missing something? is there a way to know what it is?

i'm also running the command in the same path where the .java and .class are.

there is no folder start in the path where i compiled the program.

EDIT2 I tried both start/HelloWorldSwing and HelloWorldSwing with java.

I don't get any errors with javac also. I get 2 pop up windows with the messages I've typed previously when I use javaw and java gives me the NoClassDefFoundException, then talks about the ClassLoaders and whatnot.

EDIT3 I got it to work by removing the "package start;" line. what would i have to do to make it work with that?

javaw also works now that I removed the package line.

+2  A: 

Try this:

java HelloWorldSwing

Rather than:

java start/HelloWorldSwing

The argument to the java compiler ( javac ) is a file ( that's why start/HelloWorldSwing.java probably worked ) but the argument to the Java interpreter ( java ) is a class name.

That's why you don't append the .class in the command line, and since there is no class named start/HelloWorldSwing You get that error message ( NoClassDefFoundError ), that reads "There is not class definition found with that name ).

To keep thing easier, compile and run your first programs from the same directory where your .java files are.

OscarRyz
He did; see the first paragraph of the question.
Michael Myers
@mmyers. I didn't noticed.
OscarRyz
+1  A: 

Tried the code works fine make sure your in the same directory as the Java file and do

javac HelloWorldSwing.java
java HelloWorldSwing
Mark Davidson
+3  A: 

Where are you invoking the java command from? From your description, HelloWorldSwing.class is in the folder "start", but is not in a package. This is likely the source of the error. Try:

cd start
java HelloWorldSwing

EDIT: The code from the tutorial does have a "package start;" declaration in it. Did you remove it? If not, put HelloWorldSwing into the folder "start" and run

java start.HelloWorldSwing

from the current folder.

See also the package tutorial.

Michael Myers
Not if the class contains a package directive.
Pourquoi Litytestdata
@Pourquoi Litytestdata: What doesn't happen?
Michael Myers
@mmyers:Yeap, probably the error is there, in the start package. :)
OscarRyz
+1  A: 

The code that you linked to is not the same as the code that you included in your question. It has this line at the top:

package start;

In Java, the package structure must be mirrored by the directory structure. So if your classes are in a package called 'start', the compiled class files must be in a directory called 'start'. So, make sure that HelloWorldSwing.class is in the 'start' directory and run the following form the parent directory:

java start.HelloWorldSwing
Dan Dyer
+1  A: 
OscarRyz