Explanation of Java environment variables-the main class cannot be found or loaded

Source: Internet
Author: User

Installed in the C: \ ProgramFiles \ Java \ jdk1.7.0 directory by default
Environment Variable Configuration:
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 JDK on a computer, we can set it to make it usable in the Java environment. Right-click my computer. Open properties.

Then select "environment variables" in "advanced". In the new open interface, you must set three attributes: "java_home"

, "Path", "classpath", where JDK is not installed. The path attribute may exist (other software may have been installed ).

Will be automatically added, such as storm audio and video ,). Java_home and classpath do not exist.

I. Click "new" and write java_home in the variable name. The variable meaning is the installation path of Java.

Then, write the installation path "C: \ ProgramFiles \ Java \ jdk1.7.0" in the variable value ".

2. Find the path in the system variable and create it if no path exists. Then click Edit. The meaning of the PATH variable is that the system has

If you can identify Java commands, the variable value is ".; % java_home % \ bin;" (where "% java_home %" indicates the setting

Java_home value), you can also directly write "C: \ Program Files \ Java \ jdk1.7.0 \ bin ;"
3. Click "new" at the end, and then write classpath on the variable name. The variable meaning is a class or lib

Java commands can only recognize classes in classpath. The value is ".; % java_home % \ Lib \ DT. jar; % java_home % \ Lib \ toos. jar; (to add. indicates the current path) ", which means the same as" % java_home %"

After the preceding three variables are set, press "OK" until the attribute window disappears. verify whether the installation is successful. Open "start" first

"->" Run ", enter" cmd "to enter the DOS system interface. Then, press "Java-version". If the installation is successful. The system displays Java version JDK "1.7.0 ".
Make sure that the installation is in c: \ ProgramFiles \ Java \ jdk1.7.0 and the environment variables can be copied directly. Enter javac in the DOS interface to check

Check whether the command is legal. Enter Java to check whether the command is legal.

In this way, the environment variables are set. The following describes path, classpth, and java_home.

Set java_home, classpath, and path as follows:
1. Set java_home:
1. For reference convenience, for example, if your JDK is installed in the c: \ Program Files \ Java \ jdk1.7.0 directory, set java_home

Directory path. To use this path later, you only need to enter % java_home % to avoid entering a long value for each reference.

Path string;
2. Normalization principle: when your JDK path is forced to change, you only need to change the value of the java_home variable. Otherwise, you need to change

Why do you use absolute paths to reference the JDK directory documentation? If you fail to complete the change and a program cannot find the JDK, the consequence is that the system crashes!
3. The third-party software will reference the agreed java_home variable. Otherwise, you will not be able to use the software normally. After using Java for a long time, you will know that if a software cannot be used normally, think about the problem.

2. Set classpath:
This is a very interesting question. Of course, it is also a question that affects beginners. The purpose of this variable setting is to allow the program to find the corresponding ". for example, compile a Java program ---. java,. class class file. If you execute Java A in the current directory, the corresponding result will be obtained (provided that you have set classpath ". "). now. when the class is moved to another directory (for example, "E: \") and Java A is executed, there will be a noclassdeffinderror exception because it cannot be found. class file, now you add classpath :".; e: \ "run Java in any directory
A. Let's see what the results will be ~~ :)~~~, Everything is normal. The Java Command finds the. Class file through classpath!

3. Set path:
The principle is very simple. Do you want to use % java_home % \ bin \ Java to execute Java commands at any time? Of course not. So, you

You can add % java_home % \ bin to the path. In this way, we can use Java to execute commands in any path.

. (When you enter your code in the Command Prompt window, the operating system will find the corresponding application in the current directory and PATH variable directory,

And execute .)

Now that all configurations are complete, check that:

Press "win" (between CTRL and ALT) + "r", Enter cmd, and press Enter.

Enter Java, javac, and Java-version respectively. If the environment variables are correctly configured, the content is returned.

Enter "Java" and press Enter:

Enter javac and press Enter:

Enter Java-version (this command is used to view the JDK version. Note: Add-version with a space after Java) and press Enter:


After configuration, let's test it.
Create a Java folder on drive C, create a helloworld. Java file, and copy and save the content.

Public class helloworld
{
Public static void main (string ARGs [])
{
System. Out. println ("Hello World ");
}
}

Start-> Run-> cmd open the command line
C: \ Documents ents and Settings \ Administrator> Cd C: \ Java

C: \ Java> javac helloworld. Java

C: \ Java> JAVA helloworld
Hello World

C: \ Java>

Note: The classpath value does not last; (semicolon) the following error occurs:
Error: The main class helloworld cannot be found or loaded.
Java helloworld errors in case letters

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.