为什么这个静态嵌套类不能在一台计算机上的 Java 中运行?
在过去的一个小时里,这让我发疯。我有两台计算机,其中一台主要运行 linux mint 11 和以下版本的 JDK:
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1~9.10.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)
现在,在我的 Windows 计算机上,我尝试使用在 Linux 计算机上编译和运行的相同代码。 Windows 运行 XP 时使用以下 java:
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
我知道版本不同,但这真的会对像嵌套类这样简单的东西产生影响吗?我真的希望我只是在下面的代码中犯了一个错误:
public class test {
public static class nClass
{
public void testFunc()
{
System.out.println("Test worked.");
}
}
public static void main(String args[]) {
test.nClass t = new test.nClass();
t.testFunc();
}
}
此代码可以在 Linux 计算机上编译并运行良好。当我把它带到 Windows 上时,它会编译得很好,但会产生:
NoClassDefFoundError test$nClass at test.main(test.java:10)
我完全被难住了,完全沮丧。
This has been driving me nuts for the past hour. I have two computers, one I work on primarily running linux mint 11 and the following version of the JDK:
java version "1.6.0_20"
OpenJDK Runtime Environment (IcedTea6 1.9.5) (6b20-1.9.5-0ubuntu1~9.10.1)
OpenJDK Client VM (build 19.0-b09, mixed mode, sharing)
Now on my windows computer I'm trying to use the same code I have compiled and ran on the linux one. The windows one is running XP with the following java:
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03)
Java HotSpot(TM) Client VM (build 20.1-b02, mixed mode, sharing)
I know the versions are different but should that really make a difference with something as simple as a nested class? I really hope I just made a mistake in the following code:
public class test {
public static class nClass
{
public void testFunc()
{
System.out.println("Test worked.");
}
}
public static void main(String args[]) {
test.nClass t = new test.nClass();
t.testFunc();
}
}
This code compiles and runs fine on the linux computer. When I bring it over to the windows one it will compile fine but produces:
NoClassDefFoundError test$nClass at test.main(test.java:10)
I'm completely stumped and entirely frustrated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是,您只复制了
test.class
文件 - 您还需要复制test$nClass.class
...或者在 Windows 上重新编译。(请注意,这些名称不遵循 Java 命名约定。这与问题无关,但即使对于示例代码,遵循约定也是一个好主意。)
My guess is that you only copied the
test.class
file - you need to copytest$nClass.class
as well... or recompile on Windows.(Note that these names don't follow Java naming conventions. It's not relevant to the question, but it's a good idea to follow conventions even for sample code.)