Project1
package com.pa;
public class JavaA {
public void hello(){
System.out.println("my name is A ! ");
}
public static void main(String[] args) {
JavaA obj=new JavaA();
obj.hello();
}
}
Project2
package com.pb;
import com.pa.JavaA;
public class JavaB {
public void hello(){
System.out.println("my name is B ! ");
}
public static void main(String[] args) {
JavaA a=new JavaA();
a.hello();
JavaB b=new JavaB();
b.hello();
}
}
Project3
package com.pc;
import com.pa.JavaA;
import com.pb.JavaB;
public class JavaC {
public void hello(){
System.out.println("my name is C ! ");
}
public static void main(String[] args) {
JavaA a=new JavaA();
a.hello();
JavaB b=new JavaB();
b.hello();
JavaC c=new JavaC();
c.hello();
}
}
C:\test\java\09_类路径classpath\project2>javac -classpath c:/test com/pb/JavaB.java
C:\test\java\09_类路径classpath\project2>java -classpath ./;c:/test com.pb.JavaB
my name is A !
my name is B !
解析:
java -classpath ./;c:/test com.pb.JavaB
-classpath
:指定class的查找路径
./
:路径1,当前目录
;
:多个路径用分号隔开
c:/test
:路径2
com.pb.JavaB
:要执行的java类
执行顺序:
- 先在
./
当前路径查找com/pb/JavaB.class,能找到 - JavaB用到了com.pa.JavaA
- 先再
./
当前路径查找com/pa/JavaA.class,找不到 - 再到
c:/test
路径下查找com/pa/JavaA.class,能找到,执行 - 若是都找不到,则报错类不存在