使用-XX:+TraceClassPaths或者在服务器上执行jinfo时,都能获得classpath包含的jar包,例如:html
java.class.path = local/aaa/lib/spring-data-redis-1.8.3.RELEASE.jar:/usr/local/aaa/lib/spring-tx-4.3.8.RELEASE.jar:/usr/local/aaa/lib/spring-jdbc-4.3.7.RELEASE.jar:/usr/local/aaa/lib/classmate-1.3.1.jar:/usr/local/aaa/lib/javax.servlet-api-3.1.0.jar:/usr/local/aaa/lib/mongodb-driver-3.4.2.jar:/usr/local/aaa/lib/xml-apis-2.0.2.jar:/usr/local/aaa/lib/ufc-api-utils-2.0.0.jar:/usr/local/aaa/lib/log4j-over-slf4j-1.7.25.jar:/usr/local/aaa/lib/tomcat-embed-websocket-8.5.14.jar:...
这些jar的顺序不一样的机器老是不同的,平时没有问题,因此也没有细想过,这些jar包的顺序为何会不同的。 在以前排查的一个问题 的结尾还留了一个问题,为何有的机器会加载正确的类,有的就是错的。由于这一段在上线一个项目,灰度公测阶段,因此拖了些天,穿插着看了看加载相关的一些hotspot代码,之前也没看过,就边猜想边看了。由于是穿插着看,怕今天看的明天就忘了,因此博客结尾链接中流水帐记录了我看的过程。java
总之,通过一番查代码,最终在rt.jar中找到了JarFile这个类,代码在jdk的src.zip包里的。 用 https://github.com/saaavsaaa/warn-report/blob/master/src/main/java/report/btrace/JarBTrace.java 脚本跟踪了一下:node
正常的服务器脚本的输出:mysql
异常的服务器上脚本输出:linux
这个问题在多台服务器上都出现了,因此搜集多台的输出后能够确认这个规律,其实在结尾链接中代码记录里也能够看出,若是有多个同名的类只会加载其中第一个。在不出问题的服务器上commons-codec-1.10.jar是在http-1.1.0.jar以前加载的,而出问题的服务器正好相反。android
/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar!/sun/misc/URLClassPath.class中的private ArrayList path有全部加载的jar的URL。这个类里还有每一个jar的loader:ArrayList<URLClassPath.Loader> loaders,jar路径和jarLoader的映射:HashMap<String, URLClassPath.Loader> lmap。lmap经过private synchronized URLClassPath.Loader getLoader(int var1)方法从Stack urls中弹出的jar来构造loader并加入映射的。有两个URLClassLoader加载不一样的URLClassPath实例,一个只有jre/lib下的几个jar,一个有全部的。不过这都不重要,这些jar都是从classpath加载的。因而我在几个服务器上用jinfo输出了java.class.path,并对比了一下发现正常服务器和服务器中,这两个jar的顺序果真是不同的。那么我估计问题是出现jvm对java.class.path赋值的前面了,或许是操做系统什么的。ios
因而决定看看java.class.path初始化的状况,代码在/home/aaa/Github/hotspot/src/share/vm/runtime/arguments.cpp:_java_class_path = new SystemProperty("java.class.path", "", true)。SystemProperty的构造在/home/aaa/Github/hotspot/src/share/vm/runtime/arguments.hpp:git
// Constructor SystemProperty(const char* key, const char* value, bool writeable) { if (key == NULL) { _key = NULL; } else { _key = AllocateHeap(strlen(key)+1, mtInternal); strcpy(_key, key); } if (value == NULL) { _value = NULL; } else { _value = AllocateHeap(strlen(value)+1, mtInternal); ba(_value, value); } _next = NULL; _writeable = writeable; } };
构造里彷佛没什么关系,而后回到cpp仔细看了下,发现:github
// following are JVMTI agent writeable properties.
// Properties values are set to NULL and they are
// os specific they are initialized in os::init_system_properties_values().
// Set OS specific system properties values
os::init_system_properties_values();
这个方法的定义在os.hpp中,不过实现是不一样的系统不同,因此并无在对应的cpp中,我是linux系统,因此我去找了 /home/aaa/Github/hotspot/src/os/linux/vm/os_linux.cpp,不过也没有我想要的。arguments.cpp中只找到了对endorsed,ext,bootclasspath的目录文件读取。web
而后找到了-XX:+TraceClassPaths参数的输出位置: [classpath: ...]的调用链:
/home/aaa/Github/hotspot/src/share/vm/runtime/thread.cpp: // Parse arguments jint parse_result = Arguments::parse(args);
// Parse JAVA_TOOL_OPTIONS environment variable (if present)
jint result = parse_java_tool_options_environment_variable(&scp, &scp_assembly_required);
if (result != JNI_OK) {
return result;
}
// Parse JavaVMInitArgs structure passed in
result = parse_each_vm_init_arg(args, &scp, &scp_assembly_required, Flag::COMMAND_LINE);
if (result != JNI_OK) {
return result;
}
// Parse _JAVA_OPTIONS environment variable (if present) (mimics classic VM)
result = parse_java_options_environment_variable(&scp, &scp_assembly_required); if (result != JNI_OK) { return result; }
parse_java_tool_options_environment_variable和parse_java_options_environment_variable都有调用parse_each_vm_init_arg,这里提一句_JAVA_OPTIONS会覆盖JAVA_TOOL_OPTIONS的同key配置。
parse_each_vm_init_arg这方法一进来就看到这么一段略坑,不过也无所谓了响。。。
if (!match_option(option, "-Djava.class.path", &tail) &&
!match_option(option, "-Dsun.java.command", &tail) &&
!match_option(option, "-Dsun.java.launcher", &tail)) {
// add all jvm options to the jvm_args string. This string
// is used later to set the java.vm.args PerfData string constant.
// the -Djava.class.path and the -Dsun.java.command options are
// omitted from jvm_args string as each have their own PerfData
// string constant object.
build_jvm_args(option->optionString);
}
这就是打印上面那句的地方:
jint Arguments::parse_each_vm_init_arg
Arguments::fix_appclasspath():
if (!PrintSharedArchiveAndExit) {
ClassLoader::trace_class_path(tty, "[classpath: ", _java_class_path->value());
}
而后[Bootstrap loader class path=/usr/lib/jvm/java-8-oracle/jre/lib/resources.jar:/usr/lib/jvm/java-8-oracle/jre/lib/rt.jar:/usr/lib/jvm/java-8-oracle/jre/lib/sunrsasign.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jsse.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jce.jar:/usr/lib/jvm/java-8-oracle/jre/lib/charsets.jar:/usr/lib/jvm/java-8-oracle/jre/lib/jfr.jar:/usr/lib/jvm/java-8-oracle/jre/classes]这一句输出:
/home/aaa/Github/hotspot/src/share/vm/runtime/init.cpp
jint init_globals()
/home/aaa/Github/hotspot/src/share/vm/classfile/classLoader.cpp
void classLoader_init() {
ClassLoader::initialize();
}
void ClassLoader::initialize()
void ClassLoader::setup_bootstrap_search_path()
大致上代码都在这一部分了,能够看出代码中并无对加载顺序有所改变,而后比较重要的线索就是加载方法用的是opendir和readdir函数,在ios同事的协助下,终于肯定了,问题就是jar的加载顺序问题,而这个顺序其实是由文件系统决定的,linux内部是用inode来指示文件的。
因而写了个简单的c方法来验证,打印inode号:https://github.com/saaavsaaa/warn-report/blob/master/src/main/java/report/main.c
d_off:4066763678044974396 d_name: commons-codec-1.10.jar
d_off:4317940688349827723 d_name: commons-lang-2.5.jar
d_off:4319988583919237504 d_name: jul-to-slf4j-1.7.25.jar
d_off:4356646752930279233 d_name: spring-aop-4.3.8.RELEASE.jar
d_off:4579877846802590742 d_name: tomcat-embed-core-8.5.14.jar
d_off:4731608207059974753 d_name: groovy-2.4.3.jar
d_off:4771541818858258978 d_name: jpush-client-3.2.9.jar
d_off:4817159055427520488 d_name: httpcore-4.3.jar
d_off:5037058976412869958 d_name: rocketmq-common-3.2.6.jar
d_off:5112026581585883935 d_name: xmlParserAPIs-2.6.2.jar
d_off:5223887631628650300 d_name: commons-fileupload-1.2.2.jar
d_off:5270962929984509613 d_name: logback-core-1.1.11.jar
d_off:5295594039709144434 d_name: jackson-core-2.8.8.jar
d_off:5313324231349868064 d_name: .
d_off:5369671282559728007 d_name: spring-webmvc-4.3.8.RELEASE.jar
d_off:5480616600783493234 d_name: xalan-2.6.0.jar
d_off:5598132018198955916 d_name: unbescape-1.1.0.RELEASE.jar
d_off:5620136379821311472 d_name: spring-boot-starter-aop-1.5.3.RELEASE.jar
d_off:5639942392021544761 d_name: mybatis-3.4.4.jar
d_off:5688537857783605585 d_name: validation-api-1.1.0.Final.jar
d_off:5689351964973998306 d_name: json-lib-2.4-jdk15.jar
d_off:5708471019688158398 d_name: tagsoup-0.9.7.jar
d_off:5716046632217600256 d_name: xom-1.0b3.jar
d_off:5736731302988875630 d_name: p2p-repository-1.0-SNAPSHOT.jar
d_off:5770969695350360533 d_name: ognl-3.0.8.jar
d_off:5946256519116188501 d_name: jackson-annotations-2.8.0.jar
d_off:6011005565981751131 d_name: jxl-2.6.jar
d_off:6121401373763401899 d_name: spring-data-mongodb-1.10.3.RELEASE.jar
d_off:6155887434083949861 d_name: icu4j-2.6.1.jar
d_off:6203694621577639938 d_name: jboss-logging-3.3.0.Final.jar
d_off:6241670413890043636 d_name: jaxme-api-0.3.jar
d_off:6317802240634228683 d_name: thymeleaf-2.1.5.RELEASE.jar
d_off:6362118033392674189 d_name: logback-classic-1.1.11.jar
d_off:6391821784225315730 d_name: snakeyaml-1.17.jar
d_off:6447926989912518869 d_name: javassist-3.16.1-GA.jar
d_off:6586996539318953387 d_name: spring-boot-1.5.3.RELEASE.jar
d_off:6682174700565688505 d_name: mybatis-spring-1.3.1.jar
d_off:6858079168157560275 d_name: http-1.1.0.jar
对照前面正常服务器的截图能够发现,顺序和这是同样的。而出错的服务器:
d_off:7500897893766328572 d_name: http-1.1.0.jar
d_off:7630237192571233449 d_name: jaxen-1.1-beta-4.jar
d_off:7846439931967980783 d_name: xom-1.0b3.jar
d_off:7986273690820996399 d_name: jxl-2.6.jar
d_off:8013065173263952359 d_name: spring-boot-starter-thymeleaf-1.5.3.RELEASE.jar
d_off:8231450206996036007 d_name: spring-context-support-4.3.8.RELEASE.jar
d_off:8471297127500795042 d_name: jpush-client-3.2.9.jar
d_off:8635726305307688944 d_name: commons-codec-1.10.jar
一样能够对照上面错误服务器的截图,并且通常状况下,修改了文件名,再改回来,或者重新上传一个,这个编号依然仍是这个,因此在出问题的服务器上问题稳定复现。那么,这个问题终因而能够告一段落了,缘由搞清楚了,心理就有底了。解决什么的就随便了,我就随便把名字改为zzzhttp而后问题就在两个包都存在的状况下解决了。
d_off:8635726305307688944 d_name: commons-codec-1.10.jar
d_off:8710228832141418589 d_name: xercesImpl-2.6.2.jar
d_off:8722513994996448409 d_name: android-json-0.0.20131108.vaadin1.jar
d_off:8754081964290049159 d_name: ufc-api-utils-2.0.0.jar
d_off:8830796801498266528 d_name: httpcore-4.3.jar
d_off:8854152647200610772 d_name: tomcat-jdbc-8.5.11.jar
d_off:8971846312288780129 d_name: spring-tx-4.3.8.RELEASE.jar
d_off:8986236906371055996 d_name: bcprov-jdk15-1.45.jar
d_off:8988385379950226997 d_name: mysql-connector-java-5.1.30.jar
d_off:8994464230154278010 d_name: p2p-common-1.0-SNAPSHOT.jar
d_off:9012703293696799571 d_name: mybatis-spring-boot-starter-1.3.0.jar
d_off:9057592519440006836 d_name: zzzhttp-1.1.0.jar
流水帐记录:https://saaavsaaa.github.io/aaa/Java_Class_Path.html
微信公众号: