以HelloWorld.java 为例( 假设该文件的位置是/home/HelloWorld.java): view plaincopy to clipboardPRint? package a.b; public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World!"); } } package a.b; public class HelloWorld{ public static void main(String[] args){ System.out.println("Hello World!"); } }
下面举例来说明该问题: 假设以下代码(位置:/src/java/code/a/b/TestT.java): view plaincopy to clipboardprint? package a.b; import c.d.T; public class TestT { public static void main(String[] args) { T t = new T(); t.p(); } } package a.b; import c.d.T; public class TestT { public static void main(String[] args) { T t = new T(); t.p(); } } 引入的文件(位置:/src/java/code/tmp/c/d/T.java) view plaincopy to clipboardprint? package c.d; public class T { public void p(){ System.out.println("class:T"); } } package c.d; public class T { public void p(){ System.out.println("class:T"); } } 假设现在编译两个文件(目录:/src/java/),则编译命令为:javac -classpath code/tmp code/a/b/TestT.java 执行命令为:java -cp code;code/tmp a/b/TestT
假设现在编译不同磁盘的三个文件(目录:e:/src/java/),则编译命令为: 假设以下代码(位置:e:/src/java/code/a/b/TestT.java): view plaincopy to clipboardprint? package a.b; import c.d.T; import e.f.T1; public class TestT { public static void main(String[] args) { T t = new T(); t.p(); T1 t1 = new T1(); t1.p(); } } package a.b; import c.d.T; import e.f.T1; public class TestT { public static void main(String[] args) { T t = new T(); t.p(); T1 t1 = new T1(); t1.p(); } } 引入的文件1(位置:d:/java/code/tmp/c/d/T.java)
view plaincopy to clipboardprint? package c.d; public class T { public void p(){ System.out.println("class:T"); } } package c.d; public class T { public void p(){ System.out.println("class:T"); } } 引入的文件2(位置:c:/code/tmp/e/f/T1.java) view plaincopy to clipboardprint? package e.f; public class T1 { public void p(){ System.out.println("class:T1"); } } package e.f; public class T1 { public void p(){ System.out.println("class:T1"); } } 如果当前目录为:e:/src/java/ 编译命令为:javac -classpath d:/java/code/tmp;c:/code/tmp code/a/b/TestT.java执行命令为:java -cp code;d:/java/code/tmp;c:/code/tmp a/b/TestT