简体   繁体   English

无法从命令行运行swing

[英]can't run swing from the command line

i use the command line in windows to compile and then execute my java programs. 我在windows中使用命令行来编译然后执行我的java程序。 i've gone to http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html and tried compiling the HelloWorldSwing.java class. 我去了http://java.sun.com/docs/books/tutorial/uiswing/start/compile.html并尝试编译HelloWorldSwing.java类。 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 HelloWorldSwing”它给了我一堆错误,并在线程“主”java.lang.NoClassDefFoundError:错误的名称:开始/ HelloWorldSwing中说出一些异常的行。

i try running with java start/HelloWorldSwing and it says noClassDefFoundError. 我尝试使用java start / HelloWorldSwing运行,它说noClassDefFoundError。 i get no errors with javac either. 我也没有javac的错误。 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();
            }
        });
    }
}

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? 从来没有运行任何java程序的任何问题,我错过了什么? is there a way to know what it is? 有没有办法知道它是什么?

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启动/ HelloWorldSwing和HelloWorldSwing。

I don't get any errors with javac also. 我也没有与javac有任何错误。 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. 当我使用javaw并且java给我NoClassDefFoundException时,我得到2个弹出窗口,其中包含我之前键入的消息,然后讨论ClassLoaders和诸如此类的东西。

EDIT3 I got it to work by removing the "package start;" EDIT3我通过删除“包开始”让它工作了 line. 线。 what would i have to do to make it work with that? 我需要做些什么来使它与它一起工作?

javaw also works now that I removed the package line. javaw现在也可以删除包行。

Where are you invoking the java command from? 你在哪里调用java命令? From your description, HelloWorldSwing.class is in the folder "start", but is not in a package. 根据您的描述,HelloWorldSwing.class位于文件夹“start”中,但不在包中。 This is likely the source of the error. 这可能是错误的根源。 Try: 尝试:

cd start
java HelloWorldSwing

EDIT: The code from the tutorial does have a " package start; " declaration in it. 编辑:教程中的代码确实有一个“ package start; ”声明。 Did you remove it? 你删除了吗? If not, put HelloWorldSwing into the folder "start" and run 如果没有,请将HelloWorldSwing放入文件夹“start”并运行

java start.HelloWorldSwing

from the current folder. 从当前文件夹。

See also the package tutorial . 另请参阅包教程

Try this: 尝试这个:

java HelloWorldSwing java HelloWorldSwing

Rather than: 而不是:

java start/HelloWorldSwing java start / 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/HelloWorldSwing You 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文件所在的同一目录编译并运行您的第一个程序。

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; b)保持package start; line in the code and append the -d option to javac and use the full class name. 代码中的一行,并将-d选项附加到javac并使用完整的类名。

I hope this helps. 我希望这有帮助。

Tried the code works fine make sure your in the same directory as the Java file and do 试过代码工作正常,确保你在与Java文件相同的目录中并做到

javac HelloWorldSwing.java
java HelloWorldSwing

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. 在Java中,包结构必须由目录结构镜像。 So if your classes are in a package called 'start', the compiled class files must be in a directory called 'start'. 因此,如果您的类位于名为“start”的包中,则编译的类文件必须位于名为“start”的目录中。 So, make sure that HelloWorldSwing.class is in the 'start' directory and run the following form the parent directory: 因此,请确保HelloWorldSwing.class位于“start”目录中并运行以下形式的父目录:

java start.HelloWorldSwing

you can compile it by: 你可以编译它:

    javac HelloWorldSwing.java

you can run it by: 你可以运行它:

    java -cp . HelloWorldSwing.java

This one really works. 这个确实有效。

只需添加一行:

import java.awt.*;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM