A little feeling about classpath

Source: Internet
Author: User
Tags documentation oracle documentation

The previous example involved only one Hellojava.java file, so there was no classpath involved. But there is no such simple program in actual programming, so it is often necessary to focus on classpath settings.

The official Oracle documentation for CLASSPATH is defined as:

The class path is the path of the Java runtime environment searches for classes and other resource files. The class search path (more commonly known by the shorter name, "class path") can is set using either The-classpath Optio n when calling a JDK tool ("preferred method") or by setting the CLASSPATH environment.

Here are a few questions: what to search for.
Third-party or user-defined. class, the path here is to tell JDK commands how to search for these. class. The documentation is further explained: paths to the. jar,. zip or. class files, and specifies in detail the Classpath specific settings in various cases:
1.1. Jar and. zip file: The path must be the. jar,.zip file itself.
1.2.. class file (in unnamed package): directory containing. class files
1.3.. class file (in named Package): A directory containing package root nodes (see the example below) which commands are used for CLASSPATH
Java, Javac, Javadoc, Jdb,javah command line options and CLASSPATH environment variables
3.1. Command line options overwrite CLASSPATH environment variables
3.2. The default classpath is the current directory, note that setting-classpath or classpath does not add the current directory by default. Classpath and Package

As mentioned above, when the. class that needs to be searched is at the named package, specify the directory Classpath contains the package root node. How to understand. An example might be clearer than an abstract explanation.

Suppose there is a class Navigationcontroller located in package com.nvidia.navigation:

Package com.nvidia.navigation;
......
public class Navigationcontroller
{public
  static void Main (string[] args) {} ...
}

Suppose we need to run Navigationcontroller how do we specify Classpath?

Java-classpath??? Com.nvidia.navigation.NavigationController

Suppose our Navigationcontroller.class path is C:\java\MyProject\bin\com\nvidia\navigation\NavigationController.class,
Where the package name is com.nvidia.navigation and what is the package root node. Obviously COM, then the COM directory where, is C:\java\MyProject\bin, then we know how to set up this classpath:

Java-classpath C:\java\MyProject\bin Com.nvidia.navigation.NavigationController

One thing to note is that the name of the package must be full and cannot be separated as a pathname, so classpath can only point to the directory of the package root node, and cannot continue to extend down. Classpath or CP

All commands that need to be classpath recognize the-classpath option, but do not know why the Java Command recognizes abbreviated-CP in addition to supporting-classpath:

JAVA-CP PATH1; PATH2 ... Path Order

The order of the specified path has an important effect. java virtual opportunities are searched in the order set by Classpath, and will continue to search for the following paths only if the previous path search fails.

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.