Java--包
编译器对文件(带有文件分隔符和扩展名 .java 的文件)进行操作。而 Java 解释器加载类(带有 . 分隔符)。
编译器在编译源文件的时候不检查目录结构。例如,假定有一个源文件开头有下列语句:
package com.mycompany;
即使这个源文件没有在子目录 com/mycompany 下,也可以进行编译。如果他不依赖于其他包,就不会出现编译错误。但是 ,最终的程序将无法运行,这是因为虚拟机找不到类文件。
运行的话需要在基目录即 com 同级目录下执行。
参考:http://blog.csdn.net/shixiaoguo90/article/details/50607716
java -cp
平时都用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.