我在mac上编写了一个程序,并通过终端编译:
cd user/desktop/Code/Program/src/program
javac Program.java
**java Program**
然后我收到此错误:
Exception in thread "main" java.lang.NoClassDefFoundError: Program (wrong name: program/Program)
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:791)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:423)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:356)
at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:480)
有谁知道如何解决这个问题?
源代码:
package learning;
public class Learning{
public static void main(String[] args){
String[] wordListOne = {"you are","I know","I eat"};
String[] wordListTwo = {"a poo","a funny","a posh"};
String[] wordListThree = {"Sandwich","person","object"};
int oneLength = wordListOne.length;
int twoLength = wordListTwo.length;
int threeLength = wordListThree.length;
int rand1 = (int) (Math.random() * oneLength);
int rand2 = (int) (Math.random() * twoLength);
int rand3 = (int) (Math.random() * threeLength);
String phrase = wordListOne[rand1]+" "+wordListTwo[rand2]+" "+wordListThree[rand3];
System.out.println(phrase);
}
}
答案 0 :(得分:8)
尝试使用它。
javac learning/Learning.java
java learning.Learning
您的程序名为Learning
,而不是Program
。您必须从learning
文件夹所在的目录中执行此操作。例如:
C:/path/to/files/learning/Learning.class
然后使用C:/path/to/files/
命令导航到cd
:
cd C:/path/to/files/
然后执行你的陈述。
答案 1 :(得分:0)
将文件名更改为Learning.java
然后编译并运行它(Learning.java not program.java)
文件名应与静态类的名称相同。
答案 2 :(得分:0)
NoClassDefFoundError通常意味着在编译时可用的类在运行时不可用。例如,您的编译和运行时类路径不匹配。这是一篇博文,更详细地解释了它:http://javarevisited.blogspot.ca/2011/06/noclassdeffounderror-exception-in.html?m=1