The Java environment variable---cannot be found or the main class could not be loaded

Source: Internet
Author: User

The default installation of the environment variable in the C:\ProgramFiles\Java\jdk1.7.0 directory is configured as path=.; %java_home%\bin classpath=.; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar; Java_home=c:\program files\java\jdk1.7.0

After installing the JDK on the computer, let's make the settings available to the Java environment. First right click on my Computer. Open the property.

Then select "Environment variable" inside "advanced", the system variable in the new open interface needs to set three properties "Java_home"

, "Path", "Classpath", which is not installed in the JDK environment. The path attribute is possible (installation of other software may

will automatically increase, such as Storm Video,). and Java_home and classpath do not exist.

A: Point "new", and then in the variable name write on Java_home, gu name the meaning of the variable is the Java installation path, hehe, but

After the variable value is written to the path you just installed "C:\ProgramFiles\Java\jdk1.7.0".

Second: In the system variable to find the path, no new, and then click Edit, the meaning of the path variable is that the system under any path

can identify the Java command, the value of the variable is ".; %java_home%\bin; ", (where"%java_home% "means just set

Java_home), can also be directly written on "C:\Program Files\java\jdk1.7.0\bin;" Three: Finally, click "New", and then write classpath on the variable name, the meaning of this variable is for the Java load class (classes or LIB) road

Only classes in Classpath, Java commands can be recognized. Its value is ".; %java_home%\lib\dt.jar;%java_home%\lib\tools.jar (to Add. To indicate the current path) ", same as"%java_home% "

When the above three variables are set, press OK until the Properties window disappears, and it is verified to see if the installation was successful. First Open the Start

"Run", enter "cmd", into the DOS system interface. Then hit "Java-version" if the installation was successful. The system displays the Java version jdk "1.7.0". Make sure that the environment variable is installed in C:\ProgramFiles\Java\jdk1.7.0, and that you can enter JAVAC in the DOS interface to check

See if the command is legal, and also enter Java to see if the command is legitimate

So the environment variable is set up, the following to explain the Path,classpth,java_home

The following is the purpose of the Java_home,classpath,path: 1, set Java_home: First, for the convenience of reference, for example, your JDK installed in the C:\Program files\java\jdk1.7.0 directory, then set Java_ Home for the

Directory path, then you need to use this path in the future, just enter%java_home%, to avoid each reference to enter a very long

Path string; Second, the principle of normalization, when your JDK path is forced to change, you just need to change the value of java_home variable, otherwise, you will be more Gairen

How to use absolute path to refer to the JDK directory of the document, if you do not change the whole, a program can not find the JDK, the consequences are imagined----system crashes! Third-party software will refer to the agreed java_home variable, otherwise, you will not be able to use the software, after a long time in Java will know that if a software does not work properly, you may want to think about this is not the problem.

2, set Classpath: This is a very interesting, of course, the problem of torture beginners, this variable is set to the program can find the corresponding ". Class" file, as an example: you compile a Java program---a.java, Will get a A.class class file, you execute Java A in the current directory, you will get the corresponding result (if you have set Classpath to "."). Now, you move the a.class to a different directory (for example: "E:\"), execute Java A, there will be Noclassdeffinderror exception, because the. class file is not found, now you add Classpath to: ".; E:\ "Run Java A in any directory and see what happens ~ ~:) ~ ~ ~, everything is OK, java command found the. class file through Classpath!

3, set path: The reason is very simple, you want to use%java_home%\bin\java at any time to execute JAVA command, of course not, so, you

You can optionally add the%java_home%\bin to the path path so that we can execute the command in JAVA under any path.

. (When you enter your code in the Command Prompt window, the operating system looks for the application in the current directory and the path variable directory.

and execute.)

Now all configured, you can detect:

Press "WIN" (between Ctrl and ALT) + "R", type cmd, enter.

Enter java,javac,java-version separately, and if the environment variable is configured correctly, the content will be returned separately.

Enter "Java" and return to:

Enter Javac after the carriage return:

Enter java–version (This command is to view the JDK version of the command, note: Java After the space to add –version) after the carriage return:

Well, let's just test it. Create a Java folder in the C drive, create a Helloworld.java file, and copy the contents to save

public class HelloWorld {public static void main (String args[]) {System.out.println ("Hello World");}}

Start-run->cmd Open command line C:\Documents and SETTINGS\ADMINISTRATOR>CD C:\java

C:\java>javac Helloworld.java

C:\java>java HelloWorld Hello World

C:\java>

Note: The Classpath value is not at the end; (semicolon) The following error error occurred: The main class HelloWorld could not be found or could not be loaded. When Java HelloWorld, the letter case is wrong

These are all transferred from other people's articles, but want to add a little.

If none of the above is a problem, consider this. If you have package+ package name in the first line of your Java code, remove this line to resolve the problem.

The Java environment variable---cannot be found or the main class could not be loaded

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.