0

我在我的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);
    }
}
4

3 回答 3

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/

然后执行你的语句。

于 2013-01-19T01:52:24.070 回答
0

将文件名更改为 Learning.java

然后编译并运行它(Learning.java不是program.java)

文件名应与静态类的文件名相同。

于 2013-01-19T01:52:40.840 回答
0

NoClassDefFoundError 通常意味着在编译时可用的类在运行时不可用。例如,您的编译和运行时类路径不匹配。这是一篇更详细地解释它的博客文章:http: //javarevisited.blogspot.ca/2011/06/noclassdeffounderror-exception-in.html ?m=1

于 2013-01-19T01:57:32.543 回答