Classpath in Java

Source: Internet
Author: User

CLASSPATH is one of the most important concepts in Java, but it is often ignored. Without knowing the classpath, you will not know how java locates your class files. CLASSPATH is an environment variable that enables Java to locate user-defined classes. In Windows, you can set it in System Properties> advanced> environment variables. For Windows, you can enter echo % CLASSPATH % to view the settings. When running a program, you can use the-cp parameter or-classpath parameter to re-specify the classpath without getting it from the environment variable. This is also the best way for different applications to use different classpath. For example, the command java-classpath c: \ classes; c: \ jar \ abc. jar; c: \ zip \ abc.zip; c: \ src abc the default CLASSPATH pointing to the current path, query the classes in the current path. If you have multiple directories defined in the CLASSPATH variable, Java will query and find them from the first directory. Only when they are not found in the first directory, in the second directory. This is a very useful feature. For example, there is a CLASSPATH =/home/tester/first:/home/tester/second. Suppose there are two different versions of the test class. In the first and second directories, when running java Test, because java searches in order, it will first go to the first directory for search, if the test class is found, it will not be searched in the second directory. If the test class is deleted from the first directory, the class will be searched from the second directory. When you use the-jar command line option to run your program, the CLASSPATH environment variables will be ignored, and the-cp and-classpath switches will also be ignored. In this case, you have to be in META-INF/MANIFEST. A common problem with setting classpath using Class-Path attribute in MF is that if my current CLASSPATH variable points to the current directory ". ", there is a class in the testing package as Test, such as the directory structure C: \ project \ testing \ Test. class. In this case, run the "java Test" command in the C: \ project \ testing \ directory. What will happen? This command will not be executed, because it will give you an exception in thread "main" java. lang. NoClassDefFoundError: Test. Because the class name is not Test, but testing. Test, although classpath is set to the current directory. If the directory is C: \ project \ testing \, run the java testing command. test will also throw an exception in thread "main" java. lang. noClassDefFoundError: testing/Test because it will be in the current directory ". "started to look for the class named Test in the testing package, but could not be found, because there is no path like testing after C: \ project \ testing. To run successfully, you must run C: \ project> java testing. Test in the C: \ project directory.

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.