简体   繁体   English

通过命令提示符编译.java

[英]compile .java through command prompt

I'm new in programming, I'm learning Java right now. 我是编程新手,现在正在学习Java。 I tried to use the javac command, but the environment says that javac is an unknown command. 我尝试使用javac命令,但环境表明javac是未知命令。

How to use "javac" to compile .java files from the command prompt? 如何使用“ javac”从命令提示符下编译.java文件? I'm using eclipse 1.2.2.20100216-1730, JRE 1.6 and JRE6 我正在使用Eclipse 1.2.2.20100216-1730,JRE 1.6和JRE6

The JRE has the "java" program for running programs already compiled. JRE具有用于运行已编译程序的“ java”程序。 The "javac" program is only in the JDK. “ javac”程序仅在JDK中。 Download and install the JDK. 下载并安装JDK。 If BTW it still gives you the same error, then you need to add the javac directory to your PATH environment variable. 如果BTW仍然给您同样的错误,则需要将javac目录添加到PATH环境变量中。

Before the Java virtual machine (VM) can run a Java program, the program's Java source code must be compiled into byte-code using the javac compiler. 在Java虚拟机(VM)可以运行Java程序之前,必须使用javac编译器将该程序的Java源代码编译为字节代码。 Java byte-code is a platform independent version of machine code; Java字节码是机器代码的独立于平台的版本; the target machine is the Java VM rather than the underlying architecture. 目标计算机是Java VM,而不是基础体系结构。 To compile a Java source code file add.java, you would do the following: 要编译Java源代码文件add.java,请执行以下操作:

 javac  add.java

If there are no errors in your source file, the Java compiler will produce one or more .class files (one .class file for each class defined in the add.java source file). 如果源文件中没有错误,则Java编译器将生成一个或多个.class文件(对于add.java源文件中定义的每个类,一个.class文件)。 For example, the results of a successful compile of Foo.java will produce a byte-code version of the class in a file named Foo.class. 例如,成功编译Foo.java的结果将在名为Foo.class的文件中生成该类的字节码版本。

Every public class that you write must be in a separate .java file where the first part of the file name is identical to the class name. 您编写的每个公共类都必须位于单独的.java文件中,其中文件名的第一部分与类名相同。 The .java file additionally can contain code for protected and private classes. .java文件还可以包含受保护和私有类的代码。

Once you have successfully compiled your Java source code, you can invoke the Java VM to run your application's byte-code: 成功编译Java源代码后,就可以调用Java VM来运行应用程序的字节码:

 java <class with main method to run> [<command line args>, ...] 

For example, to run the main method from the Foo class: 例如,从Foo类运行main方法:

 java Foo

Any command line arguments (arguments to add's main method) follow the class name: 任何命令行参数(要添加的main方法的参数)都遵循类名:

 java add 10 20

Such Error may occur due to two reasons: 由于两个原因,可能会发生此错误:

  1. You have not installed java jdk on your system. 您尚未在系统上安装java jdk。
  2. You have not set the environment variables.classpath ,path. 您尚未设置环境变量。

Setting Path and classPath: 设置Path和classPath:

  • Windows XP Windows XP

    • Select Start, select Control Panel. 选择开始,选择控制面板。 double click System, and select the Advanced tab. 双击系统,然后选择高级选项卡。
    • Click Environment Variables. 单击环境变量。 In the section System Variables, find the PATH environment variable and select it. 在“系统变量”部分中,找到PATH环境变量并选择它。
    • Click Edit. 单击编辑。 If the PATH environment variable does not exist, click New. 如果PATH环境变量不存在,请单击“新建”。
    • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. 在“编辑系统变量(或新系统变量)”窗口中,指定PATH环境变量的值。 Click OK. 单击确定。 Close all remaining windows by clicking OK. 单击确定关闭所有剩余的窗口。
  • Windows Vista: Windows Vista:

    • From the desktop, right click the My Computer icon. 在桌面上,右键单击“我的电脑”图标。
    • Choose Properties from the context menu. 从上下文菜单中选择“属性”。
    • Click the Advanced tab (Advanced system settings link in Vista). 单击“高级”选项卡(在Vista中为“高级系统设置”链接)。
    • Click Environment Variables. 单击环境变量。 In the section System Variables, find the PATH environment variable and select it. 在“系统变量”部分中,找到PATH环境变量并选择它。
    • Click Edit. 单击编辑。 If the PATH environment variable does not exist, click New. 如果PATH环境变量不存在,请单击“新建”。
    • In the Edit System Variable (or New System Variable) window, specify the value of the PATH environment variable. 在“编辑系统变量(或新系统变量)”窗口中,指定PATH环境变量的值。 Click OK. 单击确定。 Close all remaining windows by clicking OK. 单击确定关闭所有剩余的窗口。

If you have not set the classpath and path you can access javac giving full path: 如果尚未设置类路径和路径,则可以访问javac并提供完整路径:

such as C:\\Java\\jdk1.7.0\\bin\\javac MyClass.java 例如C:\\Java\\jdk1.7.0\\bin\\javac MyClass.java

To check the path and classpath, type these commands in a command window: 要检查路径和类路径,请在命令窗口中键入以下命令:

echo $PATH
echo $CLASSPATH

If you get a blank command line in response to either of these, then that particular variable has no value (it has not yet been set). 如果您收到一个空白命令行来响应上述任何一个,则该特定变量没有值(尚未设置)。

setting path and classpath through cmd: 通过cmd设置路径和类路径:

set path=c:\j2sdk1.4.1_01\bin(Give the path of bin)

set classpath=;(or the directory where you want your class files)

Download and install JDK set environment path --> edit path in environment path and add ;/bin using javac command --> javac *.java or javac ClassName.java 下载并安装JDK设置环境路径->在环境路径中编辑路径,并使用javac命令添加; / bin-> javac * .java或javac ClassName.java

When run the main method you should note to [package] name java packagename.ClassName 运行main方法时,应注意[package]名称java packagename.ClassName

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

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