• 13
• 17
• 27
• 9
• 9

Command line Java: Could not find or load main class

This topic is 498 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hey, everyone.  I'm trying to get started writing Java bots for Starcraft: Brood War using BWMirror.  The starter project runs fine in Eclipse, but I can't get it working on the command line.

I'm trying to compile and run a class TestBot1 that extends a class in a JAR.  It compiles fine, but when I run it I get 'Error: Could not find or load main class TestBot1'.

Here's an MWE.  TestBot1.java:
import bwapi.*;

public class TestBot1 extends DefaultBWListener
{
public static void main(String[] args)
{
System.out.println("It works!");
}
}

I execute both the compile and the run command in the same directory as TestBot1.java (and TestBot1.class when it generates). I compile with:
javac TestBot1.java -classpath .;../lib/bwmirror_v2_5.jar


I attempt to run with:
java TestBot1 -classpath .;../lib/bwmirror_v2_5.jar


As is evident from the above, my directory structure is as follows:
Folder TestBot1 contains folders src and lib; src contains TestBot1.java, and lib contains bwmirror_v2_5.jar.

This run fails with a 'could not find or load main class' error.  If I remove 'extends DefaultBWListener', though, everything compiles and runs fine.  I'm on Windows 7, using Java 1.8.0

I'm not a Java expert, and most of my experience building large codebases is with C/C++/Fortran and make.  I understand that I could just use Eclipse.  I'd like to figure out my misunderstanding here, though. Edited by before-it-was-popular

Share on other sites
You could try the same thing without the import and the library
That would give you information whether the "main" function is the problem, or whether the library fails in some way

Just for test, add double quotes around your class path, maybe the semi-colon splits the line in some way? Edited by Alberth

Share on other sites
Probably it is failing to load your class because it can't find the jar with DefaultBWListener in.

According to this the command is:

java [options] class [arguments]
Where arguments come after the classname and get passed to your main method. While options come first and actually configure the JVM.
So I think the call you should be making is actually this:

java -classpath .;../lib/bwmirror_v2_5.jar TestBot1
Edited by dmatter

Share on other sites

IIRC you have two options while exporting to JAR in Eclipse. One is a "lib" JAR, and other is called a "Runnable JAR". Thats the one you want. I believe that one doesn't requires for you to specify the main class (actually, you have to choose a "Run Configuration" when exporting, that tells Eclipse which 'main' to point the JAR to).

Share on other sites

Probably it is failing to load your class because it can't find the jar with DefaultBWListener in.

According to this the command is:

java [options] class [arguments]
Where arguments come after the classname and get passed to your main method. While options come first and actually configure the JVM.
So I think the call you should be making is actually this:

java -classpath .;../lib/bwmirror_v2_5.jar TestBot1

This was it. Thanks a million!