1

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.

2 Answers 2

2

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.

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

Comments

1

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.

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.