user310291
user310291

Reputation: 38228

Why can't I run my hello swing app?

I compile with javac helloswing.java but cannot run with java swingtutorial.helloswing as it said Exception in thread main NoClassDefFoundError. Could not find main class

I just added classpath to c:...\rt.jar but still java -cp . swingtutorial.helloswing cannot find main why ?

package swingtutorial;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;

public class helloswing extends JFrame {

    public helloswing() {
       setTitle("Hello Swing");
       setSize(300, 200);
       setLocationRelativeTo(null);
       setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                helloswing ex = new helloswing();
                ex.setVisible(true);
            }
        });
    }
}

Upvotes: 0

Views: 207

Answers (4)

Jonas Kongslund
Jonas Kongslund

Reputation: 5268

You need to specify the classpath. Try with

javac swingtutorial\helloswing.java
java -cp . swingtutorial.helloswing

Upvotes: 1

You need to understand the classpath concept in Java better before you can solve this problem on your own.

I would suggest having a look on the official Java tutorial section on this: http://download.oracle.com/javase/tutorial/java/package/managingfiles.html

Upvotes: 1

mdma
mdma

Reputation: 57787

You will need to run

java swingtutorial.helloswing -cp [classpath]

Since the package is swingtutorial, you need to specify that in the name of the class to run.

Upvotes: 1

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103155

You may need to use the fully qualified name of the class:

   java swingtutorial.helloswing

Upvotes: 1

Related Questions