- Introduction to JVM Languages
- Vincent van der Leun
- 548字
- 2021-07-02 21:46:31
Setting ClassPath for a JVM instance
ClassPath is a list of directories and/or individual JAR archive files that a JVM uses to find the classes referenced in the project. It is required by the java command, compilers (Java and most other JVM languages), and many other JDK- and JVM-related tools.
When you don't set a ClassPath explicitly, it defaults to the current directory. If all the class files that are used by the project are stored in the directory that is used to launch the program and the class file's package name matches the directory structure, then it's not necessary to set a ClassPath explicitly.
In the real world, you will use add-on libraries often. As mentioned in the previous chapter, many JVM languages require a runtime library to be loaded; without it, the application will not be able to run. It is a convention to put such add-on library files (often called dependencies) in a separate subdirectory. When a library is placed inside a JAR file, as is often the case, you have no other choice than to specify it on the ClassPath, as JVM never attempts to load JAR files automatically.
Let's look at a real-world example. This is the ClassPath that the open source Apache TomCat application server on my Windows machine requires in order to start:
C:\apache-tomcat-8.0.44\bin\bootstrap.jar;C:\apache-tomcat-8.0.44\bin\tomcat-juli.jar
The classes that Apache TomCat requires to start are apparently placed inside two JAR files: bootstrap.jar and tomcat-juli.jar. They are stored in the bin directory of Apache Tomcat's . As will be discussed in more detail later, a JAR file contains a collection of class files inside a single file.
Both the absolute and relative paths can be specified for a directory or JAR file. The start point of relative paths is the directory from where the command (for example, java or javac) is launched. All entries on a ClassPath are read from left to right until the class is found. The order of entries is therefore very important.
Setting a ClassPath can be done on multiple levels. JVM determines a used ClassPath in the following order:
- If the value of the CLASSPATH environmental variable is set, it is taken.
- If the -cp or -classpath command-line option is specified on the java (or other JDK tools, such as the javac Java compiler) command, then that one is taken. Other tools may require different command-line options.
Most major JVM applications include simple operating system shell scripts (for Linux/macOS) and batch files (for Windows) that set the ClassPath automatically, so that the end-user can start the program simply by launching a script. Some applications even include a native executable file that sets the correct ClassPath and launches the JVM invisibly under the hood; it also hides the fact that the application uses a JVM.
To make it easier for developers to launch their JVM application while testing or developing their applications, most build tools often offer a task (or a plugin that implements this task) that sets the ClassPath automatically and launches the application with a single command. We will investigate this handy feature of the popular Gradle build tool in Chapter 4, Java Programming.