Search code examples
javajvmclasspathjvm-arguments

Controlling the loading of Java class in CLASSPATH


I am trying to apply patch using classpath approach, I mean I am adding modified class files as jar file, and while classes are getting loaded new version of classes are loaded. Therefore application is patched without changing original jar file.

The following classpath definition works fine;

java -cp patch/patch.jar;bin/  com.test.PatchClasspath

but when order of lib classes are changed it does not work.(as usual)

java -cp bin/;patch/patch.jar  com.test.PatchClasspath

I would like to know is there a JVM parameter which indicates the lib loading order?

EDITED: I amd modifying Util->print() method to verify patch is applied.

package com.test;

public class PatchClasspath {

public static void main(String[] args) {
    Util util = new Util();
    util.print();
}   

}


package com.test;

public class Util {


public void print(){
    System.out.println("Version-1");
}

}

Thanks.


Solution

  • There is no such parameter indicate the lib loading order in JVM (I believe), however, the java -classpath option itself will determine the class loading order base on the paths you put.

    JDK document explain this: http://docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html

    Specification order

    The order in which you specify multiple class path entries is important. The Java interpreter will look for classes in the directories in the order they appear in the class path variable. In the example above, the Java interpreter will first look for a needed class in the directory C:\java\MyClasses. Only if it doesn't find a class with the proper name in that directory will the interpreter look in the C:\java\OtherClasses directory.