3

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.

Running javac Main.java fails with

Main.java:8: cannot find symbol
symbol  : class SimplePlayer
location: class Main
        OthelloPlayer p1 = new SimplePlayer();
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:

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

but TimeoutException.java is in the same directory as Main.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();
  }
}
2
  • public Main() { } isn't necessary. Not every class needs a constructor, and since it does nothing, it's just cluttering your code. Commented Sep 18, 2009 at 4:53
  • legacy code from a class a couple years ago. i'm glad java doesn't require that. :) Commented Sep 18, 2009 at 6:57

5 Answers 5

3

You run

javac -classpath .:OthelloLib.jar Main.java

to compile, then

java -classpath .:OthelloLib.jar Main

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: 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?

Sign up to request clarification or add additional context in comments.

2 Comments

i tried that second command... java complains about a different problem... seems like it found the classes buried in the jar, though.
Ah... yeah, at this stage I'd suggest doing things using a text editor and the command line. Once you understand the process, then head back to Eclipse.
2

Note: You can do all this within a good IDE like eclipse or netbeans by adding the library to your project. The rest gets handled automagically.

Comments

2

Have you set a reference to OthelloLib.jar or invoking the javacompiler with the library as a parameter?

java -classpath .:OthelloLib.jar -g Main

Comments

1

Did you import all the libraries?

like

import a.b.c. OthelloPlayer;

Comments

0

Did you specify the classpath when you call your program?

Something like the following might work:

java -cp .:OthelloLib.jar Main

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.