我在windows中使用命令行来编译然后执行我的java程序。我去了http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html并尝试编译 HelloWorldSwing.java 类。它有效,但是当我尝试“java HelloWorldSwing”时,它给了我一堆错误,并在线程“main”java.lang.NoClassDefFoundError: HelloWorldSwing (wrong name: start/HelloWorldSwing)中说了一些类似的东西
我尝试使用 java start/HelloWorldSwing 运行,它显示 noClassDefFoundError。我也没有得到 javac 的错误。这是教程中的代码:
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();
}
});
}
}
编辑:使用 javaw
弹出窗口
“发生java异常”
另一个窗口
“错误:找不到主类。 错误:发生 jni 错误,请检查您的安装并重试。"
运行任何 Java 程序都没有任何问题,我是不是遗漏了什么?有没有办法知道它是什么?
我还在 .java 和 .class 所在的同一路径中运行命令。
我编译程序的路径下没有文件夹start
编辑2 我用 java 尝试了 start/HelloWorldSwing 和 HelloWorldSwing。
我也没有收到 javac 的任何错误。当我使用 javaw 时,我得到 2 个弹出窗口,其中包含我之前键入的消息,java 给了我 NoClassDefFoundException,然后讨论了 ClassLoaders 等等。
编辑3 我通过删除“包开始”来让它工作;线。我需要做些什么才能让它发挥作用?
javaw 现在也可以工作,因为我删除了包行。
最佳答案
您从哪里调用 java
命令?根据您的描述,HelloWorldSwing.class 位于文件夹“start”中,但不在包中。这可能是错误的来源。尝试:
cd start
java HelloWorldSwing
编辑:教程中的代码确实有一个“package start;
”声明。你删除它了吗?如果没有,将 HelloWorldSwing 放入“开始”文件夹并运行
java start.HelloWorldSwing
来自当前文件夹。
另见 package tutorial .
关于java - 无法从命令行运行 swing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/509318/