1

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.

4

2 回答 2

2

没有这样的参数指示JVM中的lib加载顺序(我相信),但是, java -classpath 选项本身将根据您放置的路径确定类加载顺序。

JDK文档对此进行了解释: http: //docs.oracle.com/javase/1.4.2/docs/tooldocs/windows/classpath.html

规格顺序

指定多个类路径条目的顺序很重要。Java 解释器将按照类路径变量中出现的顺序在目录中查找类。在上面的示例中,Java 解释器将首先在目录 C:\java\MyClasses 中查找所需的类。只有在该目录中找不到具有正确名称的类时,解释器才会在 C:\java\OtherClasses 目录中查找。

于 2013-09-20T17:40:36.760 回答
1

Try -verbose:class,这将显示所有已加载的类,它们的加载顺序以及从哪个 jar 加载。

要控制加载类的顺序,您可以修改类路径中 jar 的顺序,使用java -cp. 我认为没有办法控制从特定 jar 加载的类的顺序。

要验证是否应用了补丁,一个简单/虚拟的解决方案是添加一个带有System.out.println(""). 例如:

static {
  System.out.println("[DBG] : My Patch v1.0 is loaded.");
}
于 2013-09-20T17:52:59.853 回答