Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Java decide where to look for the main class?

Tags:

java

classpath

I have a bunch of .jar files in a folder called "staged".

/target
  /staged
    - akka-slf4j_2.10.0-RC1-2.1.0-RC1.jar
    - play_2.10-2.1-RC1.jar
    - etc...

While my current directory is "target", I try to run the command

$ java -cp ./staged/* play.core.server.NettyServer ./..
Error: Could not find or load main class ..staged.akka-slf4j_2.10.0-RC1-2.1.0-RC1.jar

It's bizzarre that Java is looking for a main class in staged.akka-slf4j_2.10.0-RC1-2.1.0-RC1.jar. The NettyServer class is inside a completely different .jar file called play_2.10-2.1-RC1.jar. How does Java decide which .jar files to search in order to find the main method?

like image 870
Mark Avatar asked Feb 18 '23 02:02

Mark


1 Answers

Java doesn't search a specific jar-file. It simply searches the resulting classpath for the class that you have specified on the command line.

Edit: Unless you specify '-jar', in which case it uses the Main-Class directive of the MANIFEST.MF file.

like image 184
NilsH Avatar answered Feb 28 '23 09:02

NilsH