如何在cmd中运行java程序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37973276/
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 java program in cmd
提问by Ugur Ayd?n
I wrote a normal ''Hello World!'' code in gedit and compile it. ?t runs perfect in linux terminal. I tried to run it in Windows 8.1 cmd but it didn't work. When I write ''java Muz'' , it says:
我在 gedit 中编写了一个普通的“Hello World!”代码并编译它。?t 在 linux 终端中完美运行。我试图在 Windows 8.1 cmd 中运行它,但它没有用。当我写 ''java Muz'' 时,它说:
Error: Could not find or load main class Muz.
错误:无法找到或加载主类 Muz。
What should I do???
我该怎么办???
(I'm new at java btw!)
(顺便说一句,我是java新手!)
回答by szania04
I think that this question was already answered here: How do I run a Java program from the command line on Windows?and the sorce is here: http://www.skylit.com/javamethods/faqs/javaindos.html
我认为这里已经回答了这个问题: How do I run a Java program from the command line on Windows?来源在这里:http: //www.skylit.com/javamethods/faqs/javaindos.html
For this example your file is in C:\mywork\
对于此示例,您的文件位于 C:\mywork\
Type
类型
C:\> cd \mywork
This makes C:\mywork
the current directory.
这使C:\mywork
当前目录。
C:\mywork> dir
This displays the directory contents. You should see HelloWorld.java among the files.
这将显示目录内容。您应该在文件中看到 HelloWorld.java。
C:\mywork> set path=%path%;C:\Program Files\Java\jdk1.8.0_51\bin
(use the JDK folder for the version installed on your system). This tells the system where to find JDK programs.
(使用系统上安装的版本的 JDK 文件夹)。这告诉系统在哪里可以找到 JDK 程序。
C:\mywork> javac HelloWorld.java
This runs javac.exe, the compiler. You should see nothing but the next system prompt...
这将运行编译器 javac.exe。你应该只看到下一个系统提示......
C:\mywork> dir
javac has created the HelloWorld.class file. You should see HelloWorld.java and HelloWorld.class among the files.
javac 创建了 HelloWorld.class 文件。您应该在文件中看到 HelloWorld.java 和 HelloWorld.class。
C:\mywork> java HelloWorld
This runs the Java interpreter. You should see the program output:
这将运行 Java 解释器。您应该会看到程序输出:
Hello, World!
If the system cannot find javac
, check the set path command. If javac
runs but you get errors, check your Java text. If the program compiles but you get an exception, check the spelling and capitalization in the file name and the class name and the java HelloWorld
command. Java is case-sensitive!
如果系统找不到javac
,请检查 set path 命令。如果javac
运行但出现错误,请检查您的 Java 文本。如果程序编译但出现异常,请检查文件名、类名和java HelloWorld
命令中的拼写和大小写。Java 区分大小写!