Running Java program with CMD under Win7 and configuration of path and Classpath

Source: Internet
Author: User

Use Eclipse to write run Java program, want to try how to compile in cmd.

Of course, the JDK will have to be installed. Otherwise there is no JVM virtual machine, how to talk about compiling.

After installing the JDK, for example, I am installing jdk1.7.0_05 (the path is C:\Program files\java\jdk1.7.0_05), you need to set the environment variable.


1 setting path (two ways)

Method 1: Open Cmd,set path to view the current environment variable. See a large string of code, path right after the equal sign is the environment variable, can be found that there is no jdk1.7. Need to Add.

Type set path=%path%; C:\Program Files\java\jdk1.7.0_05\bin can be

You can then view it with the set path and find that the JDK has been added to the right of the equals sign.

This method is temporary, and it works, but it doesn't work when you close the cmd window.


Method 2 (Recommended): Right-click on My Computer-Properties-Advanced system Settings-click on the environment variable. In the user environment variable, the new name is path, and the value of the variable is

C:\ProgramFiles\Java\jdk1.7.0_05\bin of the environment variable can be, click OK. (If you have more than one user for your computer, add this to the system variable)

At this point, open the cmd window, set path, you can see that the JDK has been added to the path

This method is permanently set


2 setting Classpath

Right-click on My Computer--Properties--Advanced system settings--click on environment variables. Create a new environment in user environment variable named CLASSPATH with variable value

C:\ProgramFiles\Java\jdk1.7.0_05\lib\tools.jar variable can be, click OK. (If you have more than one user for your computer, add this to the system variable)

At this point, open the cmd window, set classpath, you can see the setting of the CLASSPATH environment variable

This method is permanently set

At this time in the cmd type Java,javac, if a lot of code indicates that the setting is successful.


d Disk Test directory to create a new Notepad, to write a Helloworld.java source file (remember to hide the extension of the known file is checked out, otherwise HelloWorld.java.txt, unable to compile)

Open cmd, enter Test (d: and CD test)

Type Javac Helloworld.java, this means that the compiled Java bytecode. class file is stored in the same directory as the Java source file; Notice here, Javac is followed by the name of class in the Java file, if the class name in the Java file is not HelloWorld, There will be mistakes in translation.

Then run the bytecode of this source file. Note here that the CLASSPATH environment variable is in C disk, so it is necessary to temporarily modify Classpath to D:\test, that is, type

Set classpath=.; %classpath% (to be under D:\test) at the same time point D disk Test and the previous C disk, the Java virtual machine will first go to your designated directory search, if not found, will be in the current directory lookup.

Remember that there is no space on the right side of the Classpath equal sign, even if the directory name you want to store contains spaces (such as program Files), you cannot appear in Chinese

Then type the Java HelloWorld we can finally see the results of the operation on CMD.


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.