Jump to content

Classpath

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by 41.210.154.76 (talk) at 19:02, 9 January 2018 (Setting the path to execute Java programs: mtnuganda). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

Classpath is a parameter in the Java Virtual Machine or the Java compiler that specifies the location of user-defined classes and packages. The parameter may be set either on the command-line, or through an environment variable.

Overview and architecture

Similar to the classic dynamic loading behavior, when executing Java programs, the Java Virtual Machine finds and loads classes lazily (it loads the bytecode of a class only when the class is first used). The classpath tells Java where to look in the filesystem for files defining these classes.

The virtual machine searches for and loads classes in this order:

  1. bootstrap classes: the classes that are fundamental to the Java Platform (comprising the public classes of the Java Class Library, and the private classes that are necessary for this library to be functional).
  2. extension classes: packages that are in the extension directory of the JRE or JDK, jre/lib/ext/
  3. user-defined packages and libraries

By default only the packages of the JDK standard API and extension packages are accessible without needing to set where to find them. The path for all user-defined packages and libraries must be set in the command-line (or in the Manifest associated with the Jar file containing the classes).

Setting the path to execute Java programs

Supplying as application argument

Suppose we have a package called org.mypackage containing the classes:

  • HelloWorld (main class)
  • SupportClass
  • UtilClass

and the files defining this package are stored physically under the directory D:\myprogram (on Windows) or /home/user/myprogram (on Linux).

The file structure looks like this:

Microsoft Windows Linux
D:\myprogram\
      |
      ---> org\  
            |
            ---> mypackage\
                     |
                     ---> HelloWorld.class       
                     ---> SupportClass.class   
                     ---> UtilClass.class     
/home/user/myprogram/
---> org/ ---> mypackage/ ---> HelloWorld.c

OS specific notes

Being closely associated with the file system, the command-line Classpath syntax depends on the operating system.[1] For example: * on all Unix-like operating systems (such as Linux and Mac OS X), the directory structure has a Unix syntax, with separate file paths separated by a colon (":"). * on Windows, the directory structure has a Windows syntax, and each file path must be separated by a semicolon (";"). This does not apply when the Classpath is defined in manifest files, where each file path must be separated by a space (" "), regardless of the operating system.

See also

* Java Classloader * Java Module System

References

  1. ^ "The Classpath". Retrieved 2016-06-26.
* Java Classpath Tutorial * Note explaining how Java classes are found, on Oracle website * Specification of how to set the Classpath on Oracle site