Mouse.java
package com.my.liujun;
public class Mouse {
public int furColor;
public int legs;
public void setFurColor(int color){
this.furColor = color;
}
public int getFurColor(){
return this.furColor;
}
public static void main(String[] args){
}
}
Cat.java
< /tbody>
package animal;
import com.my.liujun.*;
public class Cat {
public static void main(String[] args){
Mouse m = new Mouse();
System.out.println(m);
}
}
整个工程放在:E:\my_java_project\lensson1\src
也就是说:Cat.java放在目录:E:\my_java_project\lensson1\src \animal Mouse.java放在目录:E:\my_java_project\lensson1\src\com \my\liujun
编译Cat.java和Mouse.java,分别生成:Cat.class 和Mouse.class
设置classpath:
cmd:
set classpath=.;E:\my_java_project\lensson1\src
E:\my_java_project\lensson1\src\animal>java Cat
Exception in thread "main" java.lang.NoClassDefFoundError: Cat (wrong name: anim
al/Cat)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(Unknown Source)
at java.security.SecureClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.defineClass(Unknown Source)
at java.net.URLClassLoader.access$000(Unknown Source)
at java.net.URLClassLoader$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClassInternal(Unknown Source)
Could not find the main class: Cat. Program will exit.
明明我是设置了classpaht的,为啥找不到呢?
正确的方法应该是:java com.animal.Cat
这是因为classpath设置为:E:\my_java_project \lensson1\src以后,jvm去这个目录和当前目录下找Cat.class执行,结果没有找到。所以这个时候需要写全路径。
还有一种常用的方法是将要调用的class文件全部打包,比如打包为 test.jar,然后:set classpath=.;E:\my_java_project\lensson1\src\test.jar
E:\my_java_project\lensson1\src>jar cvf test.jar com/标明清单(manifest)
增加:com/(读入= 0) (写出= 0)(存储了 0%)
增加:com/my/(读入= 0) (写出= 0)(存储了 0%)
增加:com/my/liujun/(读入= 0) (写出= 0)(存储了 0%)
增加:com/my/liujun/Mouse.class(读入= 455) (写出= 296)(压缩了 34%)
增加:com/my/liujun/Mouse.java(读入= 285) (写出= 158)(压缩了 44%)
C:\Documents and Settings\Administrator>java com.my.liujun.Mouse
C:\Documents and Settings\Administrator>
3COME考试频道为您精心整理,希望对您有所帮助,更多信息在http://www.reader8.com/exam/