运行 终端中的 java class 失败

Running the java class in the terminal failing

我尝试 运行 我的 java class 在终端中使用 netbeans 10.0 生成如下:

 ~/Desktop/JavaLesson5/build/classes/javalesson5$ java javalesson5.JavaLesson5

但我一直收到以下错误:

Error: Could not find or load main class javalesson5.JavaLesson5
Caused by: java.lang.ClassNotFoundException: javalesson5.JavaLesson5

我的java代码如下:

package javalesson5;
import java.util.*;
import java.io.*;

public class JavaLesson5{

    public static double myPI = 3.14159; //Class variable

    public static int addThem(int a, int b){
      double smallPI = 3.140;//Local variable
      System.out.println("Local "+myPI);

      int c = a + b;
      return c;
    }


    public static void main(String[] args) {
      System.out.println(addThem(1,2));
    }

}

尝试:

java -cp ~/Desktop/JavaLesson5/build/classes javalesson5.JavaLesson5

您使用 classes 作为 class 路径,因为您已指定 javalesson5 作为 class 名称的一部分。对于 class 路径中的每个目录(本例中只有一个),Java 将查找一个名为 javalesson5 的目录,然后在中查找 JavaLesson5 class那个目录。