现在的位置: 首页 > 综合 > 正文

java中-classpath和路径的使用

2017年08月23日 ⁄ 综合 ⁄ 共 3019字 ⁄ 字号 评论关闭

java中-classpath和路径的使用

javac -classpath
的使用:


javac:如果当前你要编译的java
文件中引用了其它的类(比如说:继承),但该引用类的.
class文件不在当前目录下,这种情况下就需要在javac命令后面加上-classpath
参数,通过使用以下三种类型的方法
          来指导编译器在编译的时候去指定的路径下查找引用类。
          (1).
绝对路径:javac -classpath
c:/junit3.
8.
1/junit.
jar   xxx.
java

          (2).
相对路径:javac -classpath
.
.
/junit3.
8.
1/junit.
javr  xxx.
java

          (3).
系统变量:javac -classpath
%classpath% xxx.
java
(注意:%classpath%表示使用系统变量classpath的值进行查找,这里假设junit.
jar的路径就包含在classpath系统变量中)

javac 绝对路径的使用:




javac:假设你要编译的类文件名叫:helloworld.
java
,其完全路径为:d:/java
/helloworld.
java
。但你所在的当前目录是:c:/documents and settings/peng>。如果想在这里执行编译,会有什么结果呢?
 
(1).
c:/documents and settings/peng> javac helloworld.
java
这时编译器会给出如下的错误提示信息:
     error: cannot read: helloworld.
java

 这是因为默认情况下javac是在当前目录下查找类文件,很明显这个路径不是我们存放类文件的地方,所以就会报错了
        
(2).
c:/documents and settings/peng>javac d:/java
/helloworld.
java
 
      这时编译成功。
 所以,只要你执行javac命令的目录不是类文件存放的目录,你就必须在javac命令中显式地指定类文件的路径。
java
-classpath
的使用:


java
:假设我们的classpath设置为:d:/peng/java
/pro ,在该目录下有三个文件:helloworld.
java
/ helloworldextendstestcase / helloworldextendshelloworld。这三个文件的类声明分别如下:
helloworld.
java
:public class helloworld 
helloworldextendshelloworld.
java
:public class helloworldextendshelloworld extends helloworld
helloworldextendstestcase.
java
:public class helloworldextendstestcase extends junit.
framework.
testcase 
      假设我们已经按照上面关于javac -classpath
和javac 绝对路径的使用,顺利地完成了三个文件地编译。现在我们在c:/documents and settings/peng>目录下执行这三个.
class文件
(1).
c:/documents and settings/peng>java
 helloworld 
      hello world
可以看到执行成功。为什么我们在

 
c:/documents and settings/peng>执行命令,jvm能够找到d:/peng/java
/pro/helloworld.
class文件呢?这是因为我们配置了系统变量classpath,并且指向了目录:d:/peng/java
/pro 。所以jvm会默认去该目录下加载类文件,而不需要指定.
class文件的绝对路径了。
         
(2).
c:/documents and settings/peng>java
helloworldextendshelloworld

       
hello world
可以看到执行成功了。helloworldextendshelloworld继承了helloworld类,所以在执行时jvm会先查找在classpath下是否存在一个helloworld.
class文件,因为我们已经成功编译了helloworld 类了,所以可以成功执行helloworldextendshelloworld.
class
 
(3).
c:/documents and settings/peng>java
helloworldextendstestcase
     exception in thread "main" java
.
lang.
noclassdeffounderror: junit/framework/testcase
可以看到程序抛出异常了,提示找不到junit.
framework.
testcase文件。为什么同样在:/peng/java
/pro 下,helloworldextendshelloworld.
class就可以成功执行,而这个就不行了呢?这是因为:            junit.
framework.
testcase.
class文件并不存在于当前目录下,所以为了能够让程序成功运行,我们必须通过指定classpath的方式,让jvm可以找到junit.
framework.
testcase这个类,如(4):
(4).
c:/documents and settings/peng>java
-classpath
%classpath% helloworldextendstestcase
      hello world

总结:


(1).
何时需要使用-classpath
:当你要编译或执行的类引用了其它的类,但被引用类的.
class文件不在当前目录下时,就需要通过-classpath
来引入类
(2).
何时需要指定路径:当你要编译的类所在的目录和你执行javac命令的目录不是同一个目录时,就需要指定源文件的路径(classpath是用来指定.
class路径的,不是用来指定.
java
文件的路径的)




 ex:
javac -classpath D:/activemq/apache-activemq-4.1.0-incubator.jar D:/activemq/example/src/*.java  -d D:/activemq/example/bin
java
-classpath D:/activemq/apache-activemq-4.1.0-incubator.jar 
D:/activemq/example/bin/ProducerTool.class tcp://localhost:61616
test.mysubject
java -classpath
D:/activemq/apache-activemq-4.1.0-incubator.jar 
D:/activemq/example/bin/ConsumerTool.class tcp://localhost:61616
test.mysubject

 

转自:http://blog.csdn.net/anyoneking/archive/2007/09/05/1773228.aspx

抱歉!评论已关闭.