java 无法从命令行运行 Swing
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/509318/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
can't run swing from the command line
提问by
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.htmland 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)
我使用 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 try running with java start/HelloWorldSwing and it says noClassDefFoundError. i get no errors with javac either. here's the code from the tutorial:
我尝试使用 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();
}
});
}
}
EDIT: used javaw
编辑:使用 javaw
window pops up
弹出窗口
"a java exception has occurred"
“发生Java异常”
another window
另一个窗口
"error: could not find the main class. error: a jni error has occurred, please check your installation and try again."
“错误:找不到主类。错误:发生jni错误,请检查您的安装并重试。”
never had any problems running any java programs, am i missing something? is there a way to know what it is?
运行任何java程序从来没有任何问题,我错过了什么吗?有没有办法知道它是什么?
i'm also running the command in the same path where the .java and .class are.
我也在 .java 和 .class 所在的同一路径中运行命令。
there is no folder start in the path where i compiled the program.
在我编译程序的路径中没有文件夹开始。
EDIT2 I tried both start/HelloWorldSwing and HelloWorldSwing with java.
EDIT2 我用 java 尝试了 start/HelloWorldSwing 和 HelloWorldSwing。
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.
我也没有收到任何 javac 错误。当我使用 javaw 和 java 给我 NoClassDefFoundException 时,我得到 2 个带有我之前输入的消息的弹出窗口,然后谈论 ClassLoaders 和诸如此类的东西。
EDIT3 I got it to work by removing the "package start;" line. what would i have to do to make it work with that?
EDIT3 我通过删除“包启动”来让它工作;线。我该怎么做才能让它发挥作用?
javaw also works now that I removed the package line.
javaw 现在也可以工作,因为我删除了包行。
采纳答案by OscarRyz
Yep. That page has a slight bug:
是的。该页面有一个小错误:
The class uses a package, but in the run instructions the package is not used
该类使用了一个包,但在运行说明中没有使用该包
You can do two things:
你可以做两件事:
a) Drop the package name (delete the line pacakge start;) and run as indicated
a) 删除包名(删除行pacakge start;)并按指示运行
Or
或者
b) Leave the package start;line in the code and append the -doption to javacand use the full class name.
b) 保留package start;代码中的行并将-d选项附加到javac并使用完整的类名。
I hope this helps.
我希望这有帮助。
回答by Michael Myers
Where are you invoking the javacommand 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:
你java从哪里调用命令?根据您的描述,HelloWorldSwing.class 位于“start”文件夹中,但不在包中。这很可能是错误的来源。尝试:
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
编辑:教程中的代码确实有一个“ package start;”声明。你删除了吗?如果没有,将 HelloWorldSwing 放入文件夹“start”并运行
java start.HelloWorldSwing
from the current folder.
从当前文件夹。
See also the package tutorial.
另请参阅包教程。
回答by OscarRyz
Try this:
试试这个:
java HelloWorldSwing
java HelloWorldSwing
Rather than:
而不是:
java start/HelloWorldSwing
java开始/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.
java 编译器 ( javac ) 的参数是一个文件(这就是 start/HelloWorldSwing.java 可能工作的原因)但 Java 解释器 ( java ) 的参数是一个类名。
That's why you don't append the .class in the command line, and since there is no class named start/HelloWorldSwingYou get that error message ( NoClassDefFoundError ), that reads "There is not class definition found with that name ).
这就是为什么您不在命令行中附加 .class 的原因,并且由于没有名为start/HelloWorldSwing 的类,您会收到该错误消息 ( NoClassDefFoundError ),内容为“没有找到具有该名称的类定义”。
To keep thing easier, compile and run your first programs from the same directory where your .java files are.
为了让事情更简单,从 .java 文件所在的同一目录编译和运行你的第一个程序。
回答by Mark Davidson
Tried the code works fine make sure your in the same directory as the Java file and do
尝试代码工作正常确保您在与 Java 文件相同的目录中并执行
javac HelloWorldSwing.java
java HelloWorldSwing
回答by Dan Dyer
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”的包中,则编译后的类文件必须位于名为“start”的目录中。因此,请确保 HelloWorldSwing.class 在 'start' 目录中并从父目录运行以下命令:
java start.HelloWorldSwing
回答by demcy
Just add a line:
只需添加一行:
import java.awt.*;
import java.awt.*;
回答by Varshini
you can compile it by:
您可以通过以下方式编译它:
javac HelloWorldSwing.java
you can run it by:
您可以通过以下方式运行它:
java -cp . HelloWorldSwing.java
This one really works.
这个真的有效。

