How to compile and run Java files using the command line

Source: Internet
Author: User

To compile and run a Java file, it is easy to use only two commands:

(1) Javac: function: Compile Java file; How to: Javac Hello.java, if it is not wrong, a Hello.class file is generated in the same directory as Hello.java, which is a file that the operating system can use and run.

(2) Java: function: Run the. class file; How to: Java Hello, if not error, executes the Hello.class file. Note: There is no need to extend the name after hello.

The following examples illustrate:

Suppose I have a Hello.java file placed under the D:\javatest folder.

Open the CMD Command window, the C:\user\lenovo appears here is the system user name, here we need to transfer the path to the directory where the Java file resides.

Immediately after entering D: Enter, the interface will appear:

At this point we need to navigate to the directory where the Java files are located (here is the Javatest directory) and enter the CD javatest carriage return. If placed under the C drive, cd\ enter

Now go to the D:\javatest directory, where the Hello.java file is here

Then we can compile with javac command, enter Javac Hello.java return

If there is no hint that the compilation was successful, open the D:\javatest folder and you will find that there is a file called Hello.class, which is the file that the operating system can run.

Then enter Java Hello return, you will see the result of the file is running, here because my Hello.java file output a statement, so the result is as follows:

Here are a few things to watch out for

First: The content in Hello.java is a class, and the name of the class must be exactly the same as the. java file name. An error will occur if the inconsistency occurs. Here you will understand more clearly when you create a class file through eclipse, and don't repeat it here.

Second: The use of Javac and Java commands should be noted in the format.

Third: To use these two commands, you must configure environment variables.

How to install the JDK and configure environment variables:

JDK installation is very simple, only need to go to the official website after the double-click to run, and then continue to the next step, until the completion, of course, you need to know where your JDK installed in the directory, in order to configure environment variables.

Next, configure the environment variables:

Java_home: Variable value fill in the JDK installation directory (I am D:\Java\jdk1.7.0)

Path: At the end of the variable value input%java_home%\bin;%java_home%\jre\bin, (note that the original path of the variable value is not at the end; number, if not, first enter; number and then enter the above code)

CLASSPATH: Variable value filled in.; %java_home%\lib;%java_home%\lib\tools.jar (Note that there is a point in the front)

Enter the cmd command window, enter java–version carriage return, if the installation is successful, you can see the interface:

Of course you can set it in the user variable

How to compile and run Java files using the command line

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.