| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 651 人关注过本帖
标题:[求助]运行有package关键字的程序时出现错误
只看楼主 加入收藏
叮叮当
Rank: 1
等 级:新手上路
帖 子:24
专家分:0
注 册:2007-1-2
收藏
 问题点数:0 回复次数:1 
[求助]运行有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 关键 运行 
2007-09-07 16:42
hwoarangzk
Rank: 4
来 自:冰封王座
等 级:贵宾
威 望:12
帖 子:1894
专家分:0
注 册:2007-7-17
收藏
得分:0 

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


I'm here, as always...
2007-09-07 16:47
快速回复:[求助]运行有package关键字的程序时出现错误
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.014826 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved