当前位置: 代码迷 >> Java相关 >> [求助]运行有package关键字的程序时出现错误
  详细解决方案

[求助]运行有package关键字的程序时出现错误

热度:152   发布时间:2007-09-07 16:42:03.0
[求助]运行有package关键字的程序时出现错误

大家好,我的问题是这样的:已经设置过环境变量(貌似没设置错的样子),可以用package打包,并用import导入自定义包中的方法,运行没问题的。但是如果试图运行带有package关键字的文件就会得到“Exception in thread "main" java.lang.NoClassDefFoundError:(后边省略了)”这样的错误提示。。大多数package打包文件里没有main()方法,出现那样的错误似乎正常吧,可是有一个文件既有package同时又有main()方法,运行这样的文件也会出现错误信息,比如下面的书上范例,它可以通过编译,不能正确运行:

//: reusing/CADSystem.java
// Ensuring proper cleanup.
package reusing; //如果注释掉这一行则可以正常运行
import static net.gondi.util.Print.*;

class Shape {
Shape(int i) { print("Shape constructor"); }
void dispose() { print("Shape dispose"); }
}

class Circle extends Shape {
Circle(int i) {
super(i);
print("Drawing Circle");
}
void dispose() {
print("Erasing Circle");
super.dispose();
}
}

class Triangle extends Shape {
Triangle(int i) {
super(i);
print("Drawing Triangle");
}
void dispose() {
print("Erasing Triangle");
super.dispose();
}
}

class Line extends Shape {
private int start, end;
Line(int start, int end) {
super(start);
this.start = start;
this.end = end;
print("Drawing Line: " + start + ", " + end);
}
void dispose() {
print("Erasing Line: " + start + ", " + end);
super.dispose();
}
}

public class CADSystem extends Shape {
private Circle c;
private Triangle t;
private Line[] lines = new Line[3];
public CADSystem(int i) {
super(i + 1);
for(int j = 0; j < lines.length; j++)
lines[j] = new Line(j, j*j);
c = new Circle(1);
t = new Triangle(1);
print("Combined constructor");
}

public void dispose() {
print("CADSystem.dispose()");
// The order of cleanup is the reverse
// of the order of initialization:
t.dispose();
c.dispose();
for(int i = lines.length - 1; i >= 0; i--)
lines[i].dispose();
super.dispose();
}

public static void main(String[] args) {
CADSystem x = new CADSystem(47);
try {
// Cond and exception handling...
}
finally {
x.dispose();
}
}
}

错误提示是:

Exception in thread "main" java.lang.NoClassDefFoundError: CADSystem (wrong name
: reusing/CADSystem)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:12
4)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)

运行《Java编程思想》的源码中的相同文件(CADSystem.java)一样显示那个错误。这是为什么呢?请大家帮帮忙,谢谢~~

[此贴子已经被作者于2007-9-7 16:44:31编辑过]

搜索更多相关的解决方案: package  关键  运行  

----------------解决方案--------------------------------------------------------

它是说你CADSystem类没有找到,你再看看import写得是不是你那个类在的目录。有个要注意的是(随便假设的包):
import java.*;
import java.abc.*;
上面的第一句话不包括第二句话的类,也就是说第一句话能够引用java.abc,但是不能够用java.abc.*的类了,你可以查一下相关资料


----------------解决方案--------------------------------------------------------
  相关解决方案