如何将 .java 文件转换为 .class 文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1296249/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 07:56:56  来源:igfitidea点击:

how to convert .java file to a .class file

java

提问by

can anyone tell me how I can convert a .java file into .class file with an executable bytecode.

谁能告诉我如何将 .java 文件转换为带有可执行字节码的 .class 文件。

Thanks

谢谢

回答by Cullen Walsh

From the command line, run

从命令行,运行

javac ClassName.java

回答by Peter Smit

To get a .class file you have to compile the .java file.

要获得 .class 文件,您必须编译 .java 文件。

The command for this is javac. The manual for this is found here (Windows)

这个命令是javac。可在此处找到手册(Windows)

In short:

简而言之:

javac File.java

回答by Jonathan

Use the javac program that comes with the JDK (visit java.sun.com if you don't have it). The installer does not automatically add javac to your system path, so you'll need to add the bin directory of the installed path to your system path before you can use it easily.

使用JDK自带的javac程序(如果没有,请访问java.sun.com)。安装程序不会自动将 javac 添加到您的系统路径中,因此您需要将安装路径的 bin 目录添加到您的系统路径中,然后才能轻松使用它。

回答by Aviad Ben Dov

A .javafile is the code file. A .classfile is the compiled file.

一个.java文件是代码文件。一个.class文件是编译的文件。

It's not exactly "conversion" - it's compilation. Suppose your file was called "herb.java", you would write (in the command prompt):

这不完全是“转换”——它是编译。假设您的文件名为“herb.java”,您将编写(在命令提示符下):

 javac herb.java

It will create a herb.class file in the current folder.

它将在当前文件夹中创建一个 herb.class 文件。

It is "executable" only if it contains a static void main(String[])method inside it. If it does, you can execute it by running (again, command prompt:)

只有当它包含一个static void main(String[])方法时它才是“可执行的” 。如果是这样,您可以通过运行来执行它(再次,命令提示符:)

 java herb

回答by Philippe Carriere

As the others stated : it's by compiling. You can use the javac command, but I'f you're new at java, I suggest you use a software for compiling your code (and IDE) such as Eclipseand it will do the job for you.

正如其他人所说:它是通过编译。您可以使用 javac 命令,但如果您是 Java 新手,我建议您使用诸如Eclipse 之类的软件来编译您的代码(和 IDE),它会为您完成这项工作。

回答by Thorbj?rn Ravn Andersen

I would suggest you read the appropriate sections in The Java Tutorial from Sun:

我建议您阅读 Sun 的 Java 教程中的相应部分:

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

http://java.sun.com/docs/books/tutorial/getStarted/cupojava/win32.html

回答by Avery

After compiling it you can jar it.

编译后你可以jar它。

java -jar AppName.jar

java -jar 应用程序名称.jar

http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html

http://windowstipoftheday.blogspot.com/2005/10/setting-jar-file-association.html