Java - Classpath

java

http://kevinboone.net/classpath.html

// Classpath

java -cp ".;C:\parsers\commons\commons-lang-2.4.jar" MakeWarFile
java -cp ".;c:\parsers\log4j\log4j-1.2.14.jar" ExperimentLog4J

// isplay the version of java, and the value of the CLASSPATH environment variable
String version = System.getProperty("java.version");
String classpath = System.getProperty("java.class.path");

Is there a command line switch that would display the value of the CLASSPATH environment variable?

I do not know the answer to this question yet. From the command line, we can use the -cp option to specify the classpath. See java-cheat-sheet for an example. But is there a command line option that would display the Java CLASSPATH value? This would be handy to debug issues relating to CLASSPATH.

Specifying classpath from command line:

java -cp ".;C:\parsers\commons\commons-lang-2.4.jar" MakeWarFile
java -cp ".;c:\parsers\log4j\log4j-1.2.14.jar" ExperimentLog4J

How can we display the version of java, and the value of the CLASSPATH environment variable?

public class CheckJavaVersion {
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        String version = System.getProperty("java.version");
        System.out.println("Running version:" + version);
        String classpath = System.getProperty("java.class.path");
        System.out.println("Class path:" + classpath);
    }
}

What else should we know about classpath?

We may need to reference classes in other JAR files from within a JAR file. For example, in a typical situation, an applet is bundled in a JAR file whose manifest references a different JAR file (or several different JAR files) that serves as utilities for the purpose of that applet. When we create a JAR file, within our JAR file, we can specify classes to include in the Class-Path header field in the manifest file. The Class-Path header takes the following form:

Class-Path: jar1-name jar2-name directory-name/jar3-name

By using Class-Path header in the manifest, we can avoid having to specify a long -classpath flag when invoking Java to run our application.

The Class-Path headers points to classes or JAR files on the local network, not JAR files within the JAR file or classes accessible over Internet protocols. Normally, if our JAR file includes multiple classes, we do not need to add those classes to the classpath. However, if our JAR file contains other JAR files, to load classes inside these JAR files within our JAR file into the class path, we must write custom code. For example, if MyJar.jar contains another JAR file named MyUtils.jar, we cannot use the Class-Path header in MyJar.jar's manifest to load classes in MyUtils.jar into the class path. We first create a text file named Manifest.txt with the following contents:

Class-Path: MyUtils.jar

The text file must end with a new line or carriage return. The last line will not be parsed properly if it does not end with a new line or carriage return. We then create a JAR file named MyJar.jar:

jar cfm MyJar.jar Manifest.txt MyPackage/*.class

This creates the JAR file with a manifest with the following content:

Manifest-Version: 1.0
Class-Path: MyUtils.jar
Created-By: 1.7.0_06 (Oracle Corporation)

The classes in MyUtils.jar are now loaded into the class path when we run MyJar.jar

I think the procedure above discuss creating the MyJar.jar file. I think it assume that MyUtils.jar already exist, and is not contained within MyJar.jar. See https://docs.oracle.com/javase/tutorial/deployment/jar/downman.html

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License