平时都用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 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.