无法从命令行运行 Swing [英] can't run swing from the command line
问题描述
我在windows中使用命令行编译然后执行我的java程序.我去了 http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html 并尝试编译 HelloWorldSwing.java 类.它有效,但是当我尝试java HelloWorldSwing"时,它给了我一堆错误,并在线程main"java.lang.NoClassDefFoundError:HelloWorldSwing(错误名称:start/HelloWorldSwing)中说出了一些类似异常的内容
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)
我尝试使用 java start/HelloWorldSwing 运行,它显示 noClassDefFoundError.我也没有遇到 javac 错误.这是教程中的代码:
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();
}
});
}
}
使用 javaw
弹出窗口
发生java异常"
另一个窗口
"错误:找不到主类.错误:发生了 jni 错误,请检查您的安装并重试."
"error: could not find the main class. error: a jni error has occurred, please check your installation and try again."
在运行任何 Java 程序时从未遇到过任何问题,我是否遗漏了什么?有没有办法知道它是什么?
never had any problems running any java programs, am i missing something? is there a way to know what it is?
我也在 .java 和 .class 所在的同一路径中运行命令.
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.
编辑2我用 java 尝试了 start/HelloWorldSwing 和 HelloWorldSwing.
EDIT2 I tried both start/HelloWorldSwing and HelloWorldSwing with java.
javac 也没有出现任何错误.当我使用 javaw 和 java 给我 NoClassDefFoundException,然后谈论 ClassLoaders 和诸如此类的东西时,我得到了 2 个带有我之前键入的消息的弹出窗口.
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.
编辑3我通过删除包启动"来让它工作.线.我该怎么做才能让它发挥作用?
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 现在也可以使用,因为我删除了包行.
javaw also works now that I removed the package line.
推荐答案
是的.该页面有一个小错误:
Yep. That page has a slight bug:
该类使用了一个包,但在运行说明中没有使用该包
The class uses a package, but in the run instructions the package is not used
你可以做两件事:
a) 删除包名(删除行 pacakge start;
)并按指示运行
a) Drop the package name (delete the line pacakge start;
) and run as indicated
或者
b) 保留代码中的 package start;
行并将 -d
选项附加到 javac
并使用完整的类名.
b) Leave the package start;
line in the code and append the -d
option to javac
and use the full class name.
我希望这会有所帮助.
这篇关于无法从命令行运行 Swing的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!