问题描述
我在从一个 JAR 文件运行多个不同的类时遇到问题.我知道我可以将 JAR 中的一个类设置为将在命令 java -jar myjar.jar
之后运行的 Main 类,但我想要的是:
I'm having problems with running multiple different classes from one JAR file. I know that I can set one of the classes inside JAR to by Main class that will be run after command java -jar myjar.jar
, but what I want is something like:
java -jar myjar.jar MyClass
是否可以这样做,或者我是否必须创建多个 JAR(每个 JAR 用于一个可运行的类),或者最好创建管理器"类来运行我的其他类并传递给它们命令行参数?
我正在寻找文档或参考资料,但找不到.
Is it possible to do this that way, or do I have to create multiple JAR (each for one runnable class), or is it better to create 'manager' class that will run my other classes passing to them command line arguments?
I was looking for documentation or reference, but I couldn't find any.
推荐答案
可执行 Jar 文件格式只允许您指定一个主类.为了让您能够执行不同的应用程序,您需要按照您的建议创建一个管理器",或者改用类路径:
The executable Jar file format only allows you to specify one main class. In order for you to be able to execute different applications, you'll need to either create a "manager" as you suggest, or to use the classpath instead:
java -cp myjar.jar MyClass
但是,这种方法会忽略您在 Jar 的清单文件中配置的类路径.
However, this approach will ignore the classpath you have configured in the Jar's manifest file.
这篇关于JAR 中的多个可运行类,如何运行它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!