简体   繁体   English

如何使用bash在jar中执行多个java类文件

[英]How to execute multiple java class files inside a jar using bash

I know that we can invoke a class in a jar file providing the Main-class attribute in the Manifest file. 我知道我们可以在jar文件中调用一个类,在Manifest文件中提供Main-class属性。 But how can we invoke multiple files in a jar in that way. 但是我们如何以这种方式调用jar中的多个文件。 Or can we invoke a class in a jar file without specifying in the Manifest file using bash. 或者我们可以在jar文件中调用一个类,而无需使用bash在Manifest文件中指定。

The Main-Class property in a manifest file makes that JAR file a runnable JAR . 清单文件中的Main-Class属性使该JAR文件成为可运行的JAR You then can invoke that JAR with the command: 然后,您可以使用以下命令调用该JAR:

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. 请注意,您必须在类路径中包含JAR文件,以便Java可以在其中找到类。 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: 另外需要注意:如果JAR的清单文件中没有Class-Path属性,但是您的类依赖于其他JAR中的其他类,则必须在类路径中包含所有这些JAR:

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

Note, in Linux systems the path separator is a colon, not semicolon. 请注意,在Linux系统中,路径分隔符是冒号,而不是分号。

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 除了@Seelenvirtuose建议之外,另一个选择是使Main类成为一种Front Controller,并将要调用的类的名称作为参数传递

java -jar app.jar SomeClass

And based on this argument dispatch the request to the corresponding class. 并基于此参数将请求分派给相应的类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM