节约时间,开门见山,不啰嗦了,看代码:
有两个jar包(必须打jar包来分析)
cl1.jar只要一个类外加一个没用内容的test.txt文件位于test包下
- Java code
package test;public class A {}
cl2.jar只要一个类
- Java code
package test;public class Main { public static void println(Class clazz) { System.out.println("=====" + clazz.getName() + "====="); try { System.out.println("1 " + clazz.getResource("")); } catch (Exception e) { System.out.println("1 error " + e.getMessage()); } try { System.out.println("2 " + clazz.getResource("/")); } catch (Exception e) { System.out.println("2 error " + e.getMessage()); } try { System.out.println("3 " + clazz.getClassLoader().getResource("")); } catch (Exception e) { System.out.println("3 error " + e.getMessage()); } System.out.println("4 " + clazz.getResource("test.txt")); } public static void main(String[] args) { println(Main.class); try { println(Class.forName("test.A")); } catch (ClassNotFoundException e) { System.out.println("ClassNotFound " + e.getMessage()); } catch (Exception e) { e.printStackTrace(); } try { println(Class.forName("oracle.jdbc.driver.OracleDriver")); } catch (ClassNotFoundException e) { System.out.println("ClassNotFound" + e.getMessage()); } catch (Exception e) { e.printStackTrace(); } }}
批处理
- BatchFile code
@set CLASSPATH=.;%CLASSPATH%;cl1.jar;ojdbc14_g.jar;@ECHO java -jar cl2.jar调用**************************************************@java -jar cl2.jar@ECHO 将cl2.jar添加到CLASSPATH中@set CLASSPATH=.;%CLASSPATH%;cl2.jar;@ECHO java test.Main调用**************************************************@java test.Mainpause
输出
java -jar cl2.jar调用**************************************************
=====test.Main=====
1 null
2 null
3 null
4 null
ClassNotFound test.A
ClassNotFoundoracle.jdbc.driver.OracleDriver
将cl2.jar添加到CLASSPATH中
java test.Main调用**************************************************
=====test.Main=====
1 null
2 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
3 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
4 jar:file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/cl1.jar!/test/test.txt
=====test.A=====
1 null
2 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
3 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
4 jar:file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/cl1.jar!/test/test.txt
=====oracle.jdbc.driver.OracleDriver=====
1 jar:file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/ojdbc14_g.jar!/oracle/jdbc/driver/
2 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
3 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/
4 null
不打jar包输出,eclipse测试的,没有在cmd中测试,但是在cmd下结果应该一致。
=====test.Main=====
1 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/test/
2 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/
3 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/
4 jar:file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl1/cl1.jar!/test/test.txt
=====test.A=====
1 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/test/
2 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/
3 file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl2/bin/
4 jar:file:/D:/SYJ.WORK/SYJ.WORKSPACE/ws1/cl1/cl1.jar!/test/test.txt