java -cp 用法介绍

时间:2023-02-05 23:14:53



平时都用eclipse执行java程序惯了,今天想在dos下开两个界面看一下程序的消息交互,居然都不会用了,进入class文件所在目录,

执行java  SocketServer1 老是报class not found 错误,最后一问需要加java -cp 等参数, 汗颜啦!!

-cp 参数后面是类路径,是指定给解释器到哪里找到你的.class文件, 
写法: 
java -cp .;myClass.jar packname.mainclassname   
classpath中的jar文件能使用通配符,如果是多个jar文件,要一个一个地罗列出来,从某种意义上说jar文件也就是路径。

要指定各个JAR文件具体的存放路径,相同路径有多个可使用通配符 
java -cp .;c:\classes\myClass.jar;d:\classes\*.jar packname.mainclassname


例如,calss文件在D:\workspace\Test\src\sockettest 目录下,然后class文件在sockettest 这个package下

进入这个目录

D:\workspace\Test\src\sockettest>java -cp ../  sockettest.SocketServer1


如果是进入父目录即 D:\workspace\Test\src

需要使用D:\workspace\Test\src>java -cp .  sockettest.SocketServer1 

命令执行 



java -cp java的cp命令


java -cp .;c:\dir1\lib.jar Test

-cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号“;”

分隔,linux上是分号“:”分隔。不支持通配符,需要列出所有jar包,用一点“.”代表当前路径。
使用范例:

java -cp ..\lib\hsqldb.jar org.hsqldb.Server -database mydb 
或 
java -cp ../lib/hsqldb.jar org.hsqldb.Server -databa


java -cp .;c:\dir1\lib.jar Test

-cp 和 -classpath 一样,是指定类运行所依赖其他类的路径,通常是类库,jar包之类,需要全路径到jar包,window上分号“;”

分隔,linux上是分号“:”分隔。不支持通配符,需要列出所有jar包,用一点“.”代表当前路径。
使用范例:

java -cp ..\lib\hsqldb.jar org.hsqldb.Server -database mydb 
或 
java -cp ../lib/hsqldb.jar org.hsqldb.Server -databa



se.0 mydb -dbname.0 mydb 

拷贝自ITEYE网站(留待笔记用)。
以下是两个启动脚本:
1.linux系统:
 java -Dfile.encoding=GBK -cp .:../classes:../locale:../lis.jar:../lib/jcifs-1.2.17.jar:../lib/xalan.jar:../lib/xerces.jar:../lib/xercesImpl.jar:../lib/xmltransform.jar:../lib/xml-apis.jar:../lib/bsf.jar:../lib/bsh-1.2b7.jar:../lib/dbswing.jar:../lib/ifxjdbc.jar:../lib/jakarta-regexp-1.2.jar:../lib/jaxp.jar:../lib/jbcl.jar:../lib/jdbc2_0-stdext.jar:../lib/jdom.jar:../lib/jta-spec1_0_1.jar:../lib/Log4j.jar:../lib/mm.mysql-2.0.13-bin.jar:../lib/New_F1J9Swing.jar:../lib/New_Xalan.jar:../lib/oracle.jar:../lib/servlet.jar:../lib/Sprinta.jar:../lib/true_xalan.jar:../lib/commons-pool-1.2.jar:../lib/commons-dbcp-1.2.1.jar:../lib/commons-collections-3.1.jar:../lib/servlet-api.jar com.sinosoft.lis.tb.bpo.BPOServerStart compupacific 10
2.windows系统:
 java -classpath .; com.nci.utility.FileDispatcher FileDispatcher_parken.properties







java -cp classpath

Specify a list of directories, JAR archives, and ZIP archives to  search  for  class  files.  Class  path entries  are separated by colons (:). Specifying -classpath or -cp overrides any setting of the CLASSPATH environment variable.

As a special convenience, a class path element containing a basename of  * is considered equivalent to specifying a list of all the files in the directory with the extension .jar or .JAR (a java program  cannot  tell the difference between the two invocations).

For  example,  if directory foo contains a.jar and b.JAR, then the class path element foo/* is expanded to a A.jar:b.JAR, except that the order of jar files is unspecified. All jar files in  the  specified  directory, even  hidden  ones,  are included in the list. A classpath entry consisting simply of * expands to a list of all the jar files in the current directory. The CLASSPATH environment variable, where defined, will be simi-larly  expanded.  Any  classpath  wildcard expansion occurs before the Java virtual machine is started -- no Java program will ever see unexpanded wildcards except by querying the environment. For example; by invoking System.getenv("CLASSPATH").

For more information on class paths, see Setting the Class Path.

展开全文