运行Java程序

我查看了其他一些SO问题,没有找到解决我问题的任何问题……我有一个Main.java文件(下面)和一个没有相关源文件的OthelloLib.jar文件。

运行javac Main.java失败了

  Main.java:8:找不到符号
符号:类SimplePlayer
位置:class级主要
         OthelloPlayer p1 = new SimplePlayer();

还有一些错误。 SimplePlayer和BetterPlayer在jar中定义。 我怎么告诉java这个jar子? 这个命令: javac -classpath .:OthelloLib.jar -g Main.java不会导致错误,但我仍然不知道如何运行该程序。 如果我运行java -classpath .:OthelloLib.jar Main ,java抱怨:

Exception in thread "main" java.lang.NoClassDefFoundError: TimeoutException

但TimeoutException.java与Main.java位于同一目录中。

我不知道在哪里查找像这样的基本Java东西,所以我在这里!

 public class Main { public Main() { } public static void main(String[] args) { OthelloPlayer p1 = new SimplePlayer(); OthelloPlayer p2 = new BetterPlayer(); OthelloObserver o = new OthelloSimObserver(); // Create an untimed game OthelloGame g = new OthelloGame(p1, p2, o); System.out.println("Starting game"); g.run(); } } 

你跑

 javac -classpath .:OthelloLib.jar Main.java 

然后编译

 java -classpath .:OthelloLib.jar Main 

在每种情况下, -classpath .:OthelloLib.jar选项告诉Java在哪里可以找到所需的SimplePlayer和其他类; 它不知道自己查看JAR文件。 而且您需要告诉编译器和虚拟机在哪里查找类。

编辑 :看起来你添加了一些关于TimeoutException东西,因为我写了这个…你还记得编译TimeoutException.java吗? TimeoutException.class文件与Main.class在同一目录中吗?

注意:您可以通过将库添加到项目中,在eclipse或netbeans等良好的IDE中完成所有这些操作。 其余的自动处理。

您是否设置了对OthelloLib.jar的引用或使用库作为参数调用javacompiler?

 java -classpath .:OthelloLib.jar -g Main 

你导入了所有的库吗?

喜欢

 import abc OthelloPlayer; 

在调用程序时是否指定了类路径?

以下内容可能有效:

 java -cp .:OthelloLib.jar Main