简体   繁体   English

如何从jar运行特定的Main类

[英]How to Run a specific Main class from a jar

I have a jar file having 2 java classes both having Main methods. 我有一个jar文件有2个java类都有Main方法。 Is there a way I can specify while running the jar file, which class to call ? 有没有办法在运行jar文件时指定,要调用哪个类? I know that manifest file can be modified to specify the Main class but since only one class can be mentioned, that does not help. 我知道可以修改清单文件来指定Main类,但由于只能提到一个类,所以没有用。 I checked the below links, but those dint solve the problem: 我检查了下面的链接,但那些dint解决了这个问题:

https://stackoverflow.com/questions/5474666/how-to-run-a-class-from-jar-which-is-not-the-main-class-in-its-manifest-file https://stackoverflow.com/questions/5474666/how-to-run-a-class-from-jar-which-is-not-the-main-class-in-its-manifest-file

manifest-file execute a main class from a jar manifest-file 从jar中执行一个主类

It gives NoClassDefFoundError. 它给出NoClassDefFoundError。

java -cp target/your_file.jar your.package.name.YourClass

Have a class that acts as a controller. 有一个充当控制器的类。 In its main(), invoke other classes based on arguments passed. 在main()中,根据传递的参数调用其他类。 Add this as the Main-class in manifest 将其添加为清单中的Main-class

Write a Main class in which the main method calls the actual method depending on the command line argument passed. 编写一个Main类,其中main方法根据传递的命令行参数调用实际方法。 Add this Main class in the manifest. 在清单中添加此Main类。

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

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