线程"main"中的异常java.lang.NoClassDefFoundError:program(错误的名称:program/Program)

1 java compilation

我在我的mac上编写了一个程序,并通过终端编译:

cd user/desktop/Code/Program/src/program
javac Program.java
**java Program**
Run Code Online (Sandbox Code Playgroud)

然后我收到这个错误:

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)
Run Code Online (Sandbox Code Playgroud)

有谁知道我怎么解决这个问题?

源代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Doo*_*nob 8

试试这个.

javac learning/Learning.java
java learning.Learning
Run Code Online (Sandbox Code Playgroud)

你的程序被调用Learning,而不是Program.您必须从learning文件夹所在的目录执行此操作.例:

C:/path/to/files/learning/Learning.class
Run Code Online (Sandbox Code Playgroud)

然后导航到C:/path/to/files/使用cd命令:

cd C:/path/to/files/
Run Code Online (Sandbox Code Playgroud)

然后执行你的陈述.