4

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?

Mark
  • 5,286
  • 5
  • 42
  • 73

5 Answers5

4

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.

NilsH
  • 13,705
  • 4
  • 41
  • 59
2

You are getting this error because the class path should contain its entries separated by colons :, and not by white space. When the shell expands target/* it puts spaces between each file, which makes java "think" only the first jar file is in the class path, that the second jar file is the name of the class you want to run, and that the rest are command line parameters that should be passed to main.

If you quote the path, java expands * to the list of files with the correct separator:

java -cp "./staged/*" play.core.server.NettyServer ./..

See also other ways to create a classpath from all the files in a directory in this question.

Community
  • 1
  • 1
Joni
  • 108,737
  • 14
  • 143
  • 193
  • @Mark, if your version of the java command doesn't expand * in the classpath, there are many other ways to compute it. (See edit) – Joni Mar 21 '13 at 17:14
1

In the MANIFEST.MF you have to write a line like this:

Main-Class: MyPackage.MyClass

Then add this Manifest to your jar, and the jar knows where to look.

xoned
  • 2,701
  • 2
  • 31
  • 43
Loki
  • 4,065
  • 4
  • 29
  • 51
  • Note that this will only be used if you use the `-jar` option to run your Java program (to run an executable JAR). – Jesper Mar 21 '13 at 07:14
1

There's MANIFEST.MF file in your .jar placed in META-INF folder.

You can create a new one if it doesn't exists.

http://docs.oracle.com/javase/tutorial/deployment/jar/defman.html

Example content:

Manifest-Version: 1.0
Created-By: 1.7.0_06 (Oracle Corporation)
Main-Class: org.package.MainClass
Adrian Adamczyk
  • 3,000
  • 5
  • 25
  • 41
  • Note that this will only be used if you use the `-jar` option to run your Java program (to run an executable JAR). – Jesper Mar 21 '13 at 07:14
0

The shell expands your command line

java -cp ./staged/* play.core.server.NettyServer ./..

for example to

java -cp ./staged/play_2.10-2.1-RC1.jar ./staged/akka-slf4j_2.10.0-RC1-2.1.0-RC1.jar  ...  play.core.server.NettyServer ./..

Java now has ./staged/play_2.10-2.1-RC1.jar as the classpath and ./staged/akka-slf4j_2.10.0-RC1-2.1.0-RC1.jar the first argument, where it searches for the main class.

Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198