代码之家  ›  专栏  ›  技术社区  ›  Robert Karl

运行Java程序

  •  3
  • Robert Karl  · 技术社区  · 15 年前

    我看了一些其他的问题,没有找到任何解决我问题的方法…我有一个main.java文件(如下)和一个没有关联源文件的othellolib.jar文件。

    运行 javac Main.java 失败了

    Main.java:8: cannot find symbol
    symbol  : class SimplePlayer
    location: class Main
            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();
      }
    }
    
    5 回复  |  直到 15 年前
        1
  •  3
  •   David Z    15 年前

    你跑吧

    javac -classpath .:OthelloLib.jar Main.java
    

    编译,然后

    java -classpath .:OthelloLib.jar Main
    

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

    编辑 :看起来您添加了一些关于 TimeoutException 自从我写了这个…你记得要编译吗 TimeoutException.java 是吗?而且是 TimeoutException.class 文件在同一目录中 Main.class ?

        2
  •  2
  •   Stefan Kendall    15 年前

    注意:通过将库添加到项目中,您可以在Eclipse或NetBeans这样的好的IDE中完成所有这些工作。其余的则自动处理。

        3
  •  2
  •   Dale    15 年前

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

    java -classpath .:OthelloLib.jar -g Main
    
        4
  •  1
  •   Winston Chen    15 年前

    你导入了所有的库吗?

    喜欢

    import a.b.c. OthelloPlayer;
    
        5
  •  0
  •   pythonquick    15 年前

    调用程序时指定了类路径吗?

    类似下面的内容可能会起作用:

    java -cp .:OthelloLib.jar Main