开发者

Why can't I run my hello swing app?

开发者 https://www.devze.com 2023-02-26 00:23 出处:网络
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 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() {
          开发者_JS百科      helloswing ex = new helloswing();
                ex.setVisible(true);
            }
        });
    }
}


You need to specify the classpath. Try with

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


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

   java swingtutorial.helloswing


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消