我是 Java 初学者,但不是 OOP 我有一些 C、C++ 和 PHP5 经验 简而言之,我有“hello world”程序用于测试
package com.tutorial.helloworld;
public class helloWorld {
public static void main(String[] args) {
System.out.print("hello world!!!\n");
}
}
当我在控制台中使用 javac 编译时没有错误,但当我运行 Java helloWorld 时 说
Exception in thread "main" java.lang.NoClassDefFoundError: helloWorld (wrong name:
com/tutorial/helloworld/helloWorld) and much more code
在eclipse中运行正常。如果我删除包语句并手动编译将运行正常。但如果我保留包声明会引发该错误。 我应该将类文件放在子目录 com/tutorial/helloworld 中吗?可以吗?我应该如何从终端和哪个目录运行?
我使用的是 mac os x,我习惯在编辑中键入代码并从控制台编译和运行 而不是在冰上奔跑。我无法使 eclipse 适用于 c++ (c++ ide),因此我尝试使用我知道或学习的所有语言坚持使用控制台。
最佳答案
在Java中,类名由包名+类的“名字”组成。因此写
java com.tutorial.helloworld.helloWorld
您还必须知道 .class
文件的位置。您必须位于包含 com
目录的目录中才能正常工作,其中 .class
文件位于 com/tutorial/helloworld
目录中.
关于Java 在主线程 java.lang.NoClassDefFoundError 中抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21293045/