Classpath

Last updated

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.

Contents

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 Java Runtime Environment 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:

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.class                                   ---> SupportClass.class                               ---> UtilClass.class      

When we invoke Java, we specify the name of the application to run: org.mypackage.HelloWorld. However we must also tell Java where to look for the files and directories defining our package. So to launch the program, we use the following command:

Microsoft Windows Linux
 java -classpath D:\myprogram org.mypackage.HelloWorld 
 java -cp /home/user/myprogram org.mypackage.HelloWorld  

where:

Setting the path through an environment variable

The environment variable named CLASSPATH may be alternatively used to set the classpath. For the above example, we could also use on Windows:

D:\myprogram>setCLASSPATH=D:\myprogram D:\myprogram>java org.mypackage.HelloWorld 

The rule is that -classpath option, when used to start the java application, overrides the CLASSPATH environment variable. If none are specified, the current working directory is used as classpath. This means that when our working directory is D:\myprogram\ (on Linux, /home/user/myprogram/), we would not need to specify the classpath explicitly. When overriding however, it is advised to include the current folder "." into the classpath in the case when loading classes from current folder is desired.

The same applies not only to java launcher but also to javac, the java compiler.

Setting the path of a Jar file

If a program uses a supporting library enclosed in a Jar file called supportLib.jar, physically located in the directory D:\myprogram\lib\ and the corresponding physical file structure is:

D:\myprogram\       |       ---> lib\             |             ---> supportLib.jar       |       ---> org\             |             --> mypackage\                        |                        ---> HelloWorld.class                        ---> SupportClass.class                        ---> UtilClass.class 

the following command-line option is needed:

java -classpath D:\myprogram;D:\myprogram\lib\supportLib.jar org.mypackage.HelloWorld

or alternatively:

D:\myprogram>setCLASSPATH=D:\myprogram;D:\myprogram\lib\supportLib.jar D:\myprogram>java org.mypackage.HelloWorld 

Adding all JAR files in a directory

In Java 6 and higher, one can add all jar-files in a specific directory to the classpath using wildcard notation.

Windows example:

D:\myprogram>java -classpath ".;c:\mylib\*" MyApp 

Linux example:

$ java-classpath'.:/mylib/*'MyApp 

This works for both -classpath options and environment classpaths.

Setting the path in a manifest file

If a program has been enclosed in a Jar file called helloWorld.jar, located directly in the directory D:\myprogram, the directory structure is as follows:

D:\myprogram\       |       ---> helloWorld.jar        |       ---> lib\               |             ---> supportLib.jar 

The manifest file defined in helloWorld.jar has this definition:

Main-Class:org.mypackage.HelloWorldClass-Path:lib/supportLib.jar

The manifest file should end with either a new line or carriage return.

The program is launched with the following command:

java -jar D:\myprogram\helloWorld.jar [app arguments]

This automatically starts org.mypackage.HelloWorld specified in class Main-Class with the arguments. The user cannot replace this class name using the invocation java -jar. Class-Path describes the location of supportLib.jar relative to the location of the library helloWorld.jar. Neither absolute file path, which is permitted in -classpath parameter on the command line, nor jar-internal paths are supported. This means that if the main class file is contained in a jar, org/mypackage/HelloWorld.class must be a valid path on the root within the jar.

Multiple classpath entries are separated with spaces:

Class-Path:lib/supportLib.jar lib/supportLib2.jar

OS specific notes

Being closely associated with the file system, the command-line Classpath syntax depends on the operating system. [1] For example:

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

Related Research Articles

<span class="mw-page-title-main">JAR (file format)</span> Java archive file format

A JAR file is a package file format typically used to aggregate many Java class files and associated metadata and resources into one file for distribution.

<span class="mw-page-title-main">GNU Classpath</span> Implementation of standard class library of Java

GNU Classpath is a free software implementation of the standard class library for the Java programming language. Most classes from J2SE 1.4 and 5.0 are implemented. Classpath can thus be used to run Java-based applications. GNU Classpath is a part of the GNU Project. It was originally developed in parallel with libgcj due to license incompatibilities, but later the two projects merged.

<span class="mw-page-title-main">Apache Ant</span> Java software tool

Apache Ant is a software tool for automating software build processes for Java applications which originated from the Apache Tomcat project in early 2000 as a replacement for the Make build tool of Unix. It is similar to Make, but is implemented using the Java language and requires the Java platform. Unlike Make, which uses the Makefile format, Ant uses XML to describe the code build process and its dependencies.

cron Job scheduler for Unix-like operating systems

The cron command-line utility is a job scheduler on Unix-like operating systems. Users who set up and maintain software environments use cron to schedule jobs, also known as cron jobs, to run periodically at fixed times, dates, or intervals. It typically automates system maintenance or administration—though its general-purpose nature makes it useful for things like downloading files from the Internet and downloading email at regular intervals.

In computer programming, glob patterns specify sets of filenames with wildcard characters. For example, the Unix Bash shell command mv *.txttextfiles/ moves all files with names ending in .txt from the current directory to the directory textfiles. Here, * is a wildcard and *.txt is a glob pattern. The wildcard * stands for "any string of any length including empty, but excluding the path separator characters ".

Maven is a build automation tool used primarily for Java projects. Maven can also be used to build and manage projects written in C#, Ruby, Scala, and other languages. The Maven project is hosted by The Apache Software Foundation, where it was formerly part of the Jakarta Project.

In computer programming, an entry point is the place in a program where the execution of a program begins, and where the program has access to command line arguments.

In computing, Java Web Start is a deprecated framework developed by Sun Microsystems that allows users to start application software for the Java Platform directly from the Internet using a web browser. The technology enables seamless version updating for globally distributed applications and greater control of memory allocation to the Java virtual machine.

pkg-config is a computer program that defines and supports a unified interface for querying installed libraries for the purpose of compiling software that depends on them. It allows programmers and installation scripts to work without explicit knowledge of detailed library path information. pkg-config was originally designed for Linux, but it is now also available for BSD, Microsoft Windows, macOS, and Solaris.

In software engineering, a WAR file is a file used to distribute a collection of JAR-files, JavaServer Pages, Java Servlets, Java classes, XML files, tag libraries, static web pages and other resources that together constitute a web application.

<span class="mw-page-title-main">Portable application</span> Type of computer program

A portable application, sometimes also called standalone, is a program designed to operate without changing other files or requiring other software to be installed. In this way, it can be easily added to, run, and removed from any compatible computer without setup or side-effects.

<span class="mw-page-title-main">CMake</span> Cross-platform, compiler-independent build system generator

In software development, CMake is cross-platform free and open-source software for build automation, testing, packaging and installation of software by using a compiler-independent method. CMake is not a build system itself; it generates another system's build files. It supports directory hierarchies and applications that depend on multiple libraries. It can invoke native build environments such as Make, Qt Creator, Ninja, Android Studio, Apple's Xcode, and Microsoft Visual Studio. It has minimal dependencies, requiring only a C++ compiler on its own build system.

The Java Class Loader, part of the Java Runtime Environment, dynamically loads Java classes into the Java Virtual Machine. Usually classes are only loaded on demand. The virtual machine will only load the class files required for executing the program. The Java run time system does not need to know about files and file systems as this is delegated to the class loader.

The Simple API for Grid Applications (SAGA) is a family of related standards specified by the Open Grid Forum to define an application programming interface (API) for common distributed computing functionality.

The Java Platform Module System specifies a distribution format for collections of Java code and associated resources. It also specifies a repository for storing these collections, or modules, and identifies how they can be discovered, loaded and checked for integrity. It includes features such as namespaces with the aim of fixing some of the shortcomings in the existing JAR format, especially the JAR Hell, which can lead to issues such as classpath and class loading problems.

java-gnome is a set of language bindings for the Java programming language for use in the GNOME desktop environment. It is part of the official GNOME language bindings suite and provides a set of libraries allowing developers to write computer programs for GNOME using the Java programming language and the GTK cross-platform widget toolkit.

Java Native Access (JNA) is a community-developed library that provides Java programs easy access to native shared libraries without using the Java Native Interface (JNI). JNA's design aims to provide native access in a natural way with a minimum of effort. Unlike JNI, no boilerplate or generated glue code is required.

The Java Development Kit (JDK) is a distribution of Java technology by Oracle Corporation. It implements the Java Language Specification (JLS) and the Java Virtual Machine Specification (JVMS) and provides the Standard Edition (SE) of the Java Application Programming Interface (API). It is derivative of the community driven OpenJDK which Oracle stewards. It provides software for working with Java applications. Examples of included software are the Java virtual machine, a compiler, performance monitoring tools, a debugger, and other utilities that Oracle considers useful for Java programmers.

The Java Desktop Integration Components (JDIC) project provides components which give Java applications the same access to operating system services as native applications. For example, a Java application running on one user's desktop can open a web page using that user's default web browser, but the same Java application running on a different user's desktop would open the page in Opera . Initially the project supports features such as embedding the native HTML browser, programmatically opening the native mail client, using registered file-type viewers, and packaging JNLP applications as RPM, SVR4, and MSI installer packages. As a bonus, an SDK for developing platform-independent screensavers is included. Most of the features provided by JDIC were incorporated into the JDK starting with version 1.6. As a result, the development of the project has come to an end.

Dart is a programming language designed by Lars Bak and Kasper Lund and developed by Google. It can be used to develop web and mobile apps as well as server and desktop applications.

References

  1. "The Classpath" . Retrieved 2016-06-26.