Question

I know that we can invoke a class in a jar file providing the Main-class attribute in the Manifest file. But how can we invoke multiple files in a jar in that way. Or can we invoke a class in a jar file without specifying in the Manifest file using bash.

Was it helpful?

Solution

The Main-Class property in a manifest file makes that JAR file a runnable JAR. You then can invoke that JAR with the command:

java -jar <jar-file>

But you also can directly invoke the main class with the traditional way:

java -cp <jar-file> your.pkg.MainClass

Notice, that you must include your JAR file in the class path, so that Java can find the classes inside it. An additional note: If you don't have a Class-Path property in the JAR's manifest file but your classes depend on other classes in other JARs, you must include all those JARs in the class path:

java -cp <jar-file>;<lib1>;<lib2>;... your.pkg.MainClass

Note, in Linux systems the path separator is a colon, not semicolon.

OTHER TIPS

Another option, besides what @Seelenvirtuose suggested, would be to make the Main class a sort of Front Controller and pass the name of a class you want to invoke as an argument

java -jar app.jar SomeClass

And based on this argument dispatch the request to the corresponding class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top