运行Java程序 [英] Running a Java Program

查看:45
本文介绍了运行Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看了一些其他的SO问题,没有找到解决我问题的任何东西...我有一个Main.java文件(如下)和一个OthelloLib.jar文件,没有关联的源文件.

I looked at some other SO questions, didn't find anything that solved my problem... I have a Main.java file (below) and a OthelloLib.jar file without associated source files.

运行 javac Main.java 失败,并显示

Main.java:8: cannot find symbol
symbol  : class SimplePlayer
location: class Main
        OthelloPlayer p1 = new SimplePlayer();

以及其他一些错误.SimplePlayer和BetterPlayer在jar中定义.我如何告诉Java这个罐子?此命令: javac -classpath.:OthelloLib.jar -g Main.java 不会导致错误,但是我仍然不知道如何运行该程序.如果我运行 java -classpath.:OthelloLib.jar Main ,则Java会抱怨:

and a few more errors. SimplePlayer and BetterPlayer are defined in the jar. How do I tell java about this jar? This command: javac -classpath .:OthelloLib.jar -g Main.java doesn't cause an error, but I still don't know how to run the program. If I run java -classpath .:OthelloLib.jar Main, java complains:

线程"main"中的异常java.lang.NoClassDefFoundError:TimeoutException

但是TimeoutException.java与Main.java在同一目录中.

but TimeoutException.java is in the same directory as Main.java.

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

I don't know where to look up basic Java stuff like this, so here I am!

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文件.而且您确实需要告诉编译器和虚拟机在哪里寻找这些类.

In each case the -classpath .:OthelloLib.jar option tells Java where to find SimplePlayer and other classes you need; it doesn't know to look in the JAR file on its own. And you do need to tell both the compiler and the virtual machine where to look for the classes.

EDIT :好像您添加了有关 TimeoutException 的东西,因为我写了这篇文章...您还记得编译 TimeoutException.java 吗? TimeoutException.class 文件是否与 Main.class 位于同一目录中?

EDIT: Looks like you added something about TimeoutException since I wrote this... did you remember to compile TimeoutException.java? And is the TimeoutException.class file in the same directory as Main.class?

这篇关于运行Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆