Compiling and executing Java programs in the command-symbol mode

Source: Internet
Author: User
Tags sublime editor

For Java beginners, it is recommended to use a plain text file to write Java programs, and use the tool program to compile and execute Java programs in command-mode. Compile. Java using the Javac tool and Execute. class using Java tools. (Recommended sublime editor).

How do I compile and execute a Java program in the command-symbol mode? A long time to find out a lot of information finally solved. Record, easy to summarize, share, consult.

First download and install Sun's jdk,http://www.oracle.com/technetwork/cn/java/javase/downloads/index.html based on your computer's operating system. You will be prompted to install the JRE during installation, and be careful to place the JDK and JRE in two folders. My directory: D:\hsl\JAVA\JDK and D:\hsl\JAVA\JRE.

The common JRE is primarily a platform for developing good Java programs to perform. The JDK itself also has its own JRE, located in the JRE directory of the JDK installation directory. My directory: D:\hsl\JAVA\JDK\jre. The JRE attached to the JDK itself is primarily for testing purposes when developing Java programs. Beginners only know that there are two different VMs.

After installing the JDK, set path and classpath. (If you use the IDE tool Eclipse, open eclipse directly and use without setting environment variables)

Let's talk about my JDK path and. Java and. Class paths. The JDK installation path is located in the D:\hsl\java\jdk,.java and. Class paths at C:\java.

Environment variable configuration:

Path=.; %java_home%\bin;
Classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar;
Java_home=d:\hsl\java\jdk

The following is a detailed description of the environment variable configuration (enter the contents of double quotes):

Right-click My Computer to open the properties. Select Advanced-Environment variables. Need to set three properties "Java_home", "path", "Classpath", in the environment without the JDK installed, path usually exists (install the other software will automatically increase, such as Matlab,python), Java_ Home and classpath do not exist.

1, the new variable java_home, the content of the variable is the Java installation path.

Purpose: To facilitate references such as JDK installed in the D:\hsl\JAVA\JDK directory, set D:\hsl\JAVA\JDK to the environment variable value. To use this path in the future, simply enter%java_home% to avoid entering a long path string for each reference. When the JDK path is forced to change, just change the value of the Java_home variable, or change any document that references the JDK directory with an absolute path. Third-party software will refer to the agreed-upon java_home variable, otherwise you will not be able to use the software normally. In Java for a long time you will know that if a software does not work properly, it may be caused by this.

2, found in the system variable path (no new), the meaning of the variable is the system in any path can be recognized Java command, the value of the variable is ". %java_home%\bin; " (Just set the value of Java_home), you can also write the installation path "D:\HSL\JAVA\JDK\BIN;" directly.

Purpose: This variable is set in order for the program to find the corresponding ". Class" file. For example: You compile a Java program A.java, you will get a A.class class file, in the current directory execution javaa, will get the corresponding results (if you have set Classpath to "."). Now, you move the a.class to a different directory (for example: "D:\"), execute javaa, and you will not find the. class file. Now you add Classpath as: ".; E:\ ", and then run javaa in any directory, everything is OK, the Java command found the. class file through Classpath.

3, the new variable classpath, the meaning of this variable is the Java load Class (or LIB) path, only the class in Classpath, the Java command can be recognized. Its value is ".; %java_home%\lib\dt.jar;%java_home%\lib\toos.jar; " (Add. Indicates the current path), with the same meaning as "%java_home%".

Purpose: Do you want to use%java_home%\bin\java to execute JAVA commands at all times? Of course not. You can choose to add the%java_home%\bin to the path path so that we can execute the command in JAVA under any path (when you enter code at the command prompt, the operating system looks for the application in the current directory and path variable directory, and executes).

The above three environment variables are set and determined.

Verify that the installation and configuration are successful. Open cmd to enter the DOS system interface. Enter java,javac,java–version separately for verification.

In cmd, enter "Java"

Input Javac

Enter java-version (the command to view the JDK version, Java after the space to add –version)

The configuration is done, and the code tests below.

Create a Java folder in the C drive (why is it a C drive?). I did it because I created it in D, and I couldn't run it. Class, wasted a lot of time, although the reason has been found, but for convenience or on the C-drive bar, anyway, the code accounted for a small amount of memory. Create a new TXT, enter the code, and save as Helloworld.java. Code:

Public class helloworld{Public static void Main (String args[]) { System.out.println ("Hello World"
); }}

In the cmd input command, run successfully,

CD C:\java : Enter the directory where the. Java and. Class are stored

Javac Helloworld.java: Compile with Javac utility. java

Java HelloWorld: Executes. Class with a Java utility.

Tips: (not related to the above)

1, Ctrl + V does not work under CMD, but the right mouse button to choose Paste.

2, open cmd, the default in the C directory, to switch to another disk directory, only input CD D:\hsl\JAVA\javaclasspath is not valid, first enter D:, and then enter the CD D:\hsl\JAVA\javaclasspath can enter the corresponding directory d.

3, can also not set environment variables, directly in the cmd input

Set path=d:\hsl\java\jdk\bin;%path%
Set classpath=%classpath%;D: \hsl\java\javaclasspath

But each time you open cmd, you paste the same command. I have tried, the only lesson compiled cannot run, did not find the reason. Everyone can try:)

4. Equivalent command

D:
CD D:\hsl\JAVA\javaclasspath
Javachellojava.java

Equivalent to

D:
Javac D:\hsl\JAVA\javaclasspath\HelloJava.java

Reference:

[1] http://blog.csdn.net/yougou_sully/article/details/7182877

[2] Java Jdk6 Learning Notes

Compiling and executing Java programs in the command-symbol mode

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.