如何在 Windows 7 操作系统中运行 .class 文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2993932/
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
How to run a .class file in Windows 7 OS?
提问by jNoob
This is probably a stupid question, but how do I run a class file on windows 7? I usually create my own .java files and then use a basic IDE (with JDK6) to compile it to a class and run it automatically. My professor gave a .class file that we are supposed to play with extensively but I have no idea how to to run it here. Note that I cannot run it by typing:
这可能是一个愚蠢的问题,但如何在 Windows 7 上运行类文件?我通常创建自己的 .java 文件,然后使用基本的 IDE(使用 JDK6)将其编译为类并自动运行。我的教授给出了一个我们应该广泛使用的 .class 文件,但我不知道如何在这里运行它。请注意,我无法通过键入以下内容来运行它:
java classname.class
because it contains gui stuff especially buttons and such. If I do the above I get the following error:
因为它包含 gui 的东西,尤其是按钮等。如果我执行上述操作,则会出现以下错误:
java.lang.NoClassDefFoundError: Test1/2/class
Caused by: java.lang.ClassNotFoundException: Test1.2.class
at java.net.URLClassLoader.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Test1.2.class. Program will exit.
Exception in thread "main"
Any help would be highly appreciated. Thanks.
任何帮助将不胜感激。谢谢。
回答by Dean Harding
When you run it, don't include the .class
suffix:
运行时,不要包含.class
后缀:
java classname
回答by Matt
In addition to the above, it should also make no odds what your O/S is. Java compiles to byte code, which is interpreted by your JVM. You clearly have one installed since you got the java error you have pasted above.
除了上述内容之外,您的 O/S 也不应该是什么。Java 编译为字节码,由您的 JVM 解释。您显然已经安装了一个,因为您收到了上面粘贴的 java 错误。
回答by johncip
Like codeka said, you need to type java ClassName
at the command line, and if the class has a main method, it will be run.
就像codeka说的,需要java ClassName
在命令行输入,如果类有main方法,就会运行。
Java compiles each class that is defined in a particular source file into its own class (byte code) file. For example, Apple.class, Banana.class, and Cherry.class might get output after compiling Apple.java, if they are all defined there. So the actual name of the class in source will match the name of the file, minus the extension.
Java 将特定源文件中定义的每个类编译为它自己的类(字节码)文件。例如,Apple.class、Banana.class 和 Cherry.class 可能会在编译 Apple.java 后得到输出,如果它们都在那里定义的话。因此,源中类的实际名称将与文件名匹配,减去扩展名。
Now, let's say that someone accidentally (or intentionally, from the sound of it) renamed the class file. You have a file called WrongName.java, and you type java WrongName
. Note that the output will begin with the line:
现在,假设有人不小心(或有意地,从它的声音来看)重命名了类文件。您有一个名为 WrongName.java 的文件,然后键入java WrongName
. 请注意,输出将以以下行开头:
Exception in thread "main" java.lang.NoClassDefFoundError: WrongName (wrong name: RightName)
线程“main”中的异常 java.lang.NoClassDefFoundError: WrongName (wrong name: RightName)
Where RightName is what it should be. At that point you would rename your file to RightName.class, type java RightName
, and hopefully it will run. And if the name has a slash, then whatever precedes the slash is the name of the package. Let's say it's PackageName/RightName. First you need to create a directory called PackageName, and put RightName.class inside of it. Then, go one level up in your directories, and type java PackageName.RightName
.
RightName 应该是什么。那时,您将文件重命名为 RightName.class,键入java RightName
,希望它会运行。如果名称有斜线,那么斜线前面的就是包的名称。假设它是 PackageName/RightName。首先,您需要创建一个名为 PackageName 的目录,并将 RightName.class 放入其中。然后,在您的目录中上一级,然后键入java PackageName.RightName
.
Note the different kinds of exceptions: ClassNotFoundException
basically means that the class file was not found. NoClassDefFoundError
means that the class definitionwas not found. If the class does not have a main method and you try to run it as a program, you will get NoSuchMethodError: main
.
注意不同种类的异常:ClassNotFoundException
基本上意味着没有找到类文件。NoClassDefFoundError
表示未找到类定义。如果该类没有 main 方法,而您尝试将其作为程序运行,您将获得NoSuchMethodError: main
.