如何从Jar运行一个类,它不是其Manifest文件中的Main-Class [英] How to run a class from Jar which is not the Main-Class in its Manifest file

查看:952
本文介绍了如何从Jar运行一个类,它不是其Manifest文件中的Main-Class的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含4个类的JAR,每个类都有Main方法。我希望能够根据需要运行其中的每一个。我试图在Linux机器上从命令行运行它。

I have a JAR with 4 classes, each one has Main method. I want to be able to run each one of those as per the need. I am trying to run it from command-line on Linux box.

E.g. The name of my JAR is MyJar.jar

它具有主类的目录结构,如下所示: / p>

It has directory structure for the main classes as follows:

com/mycomp/myproj/dir1/MainClass1.class
com/mycomp/myproj/dir2/MainClass2.class
com/mycomp/myproj/dir3/MainClass3.class
com/mycomp/myproj/dir4/MainClass4.class

我知道我可以在我的Manifest文件中指定一个类作为main。但有没有什么方法可以在命令行上指定一些参数来运行我希望运行的任何类?

I know that I can specify one class as main in my Manifest file. But is there any way by which I can specify some argument on command line to run whichever class I wish to run?

我试过这个:

jar cfe MyJar.jar com.mycomp.myproj.dir2.MainClass2 com/mycomp/myproj/dir2/MainClass2.class /home/myhome/datasource.properties /home/myhome/input.txt

我收到此错误:

com/mycomp/myproj/dir2/MainClass2.class : no such file or directory

(在上面的命令中,'/ home / myhome / datasource.properties'和'/home/myhome/input.txt'是命令行参数。)

(In the above command, '/home/myhome/datasource.properties' and '/home/myhome/input.txt' are the command line arguments).

推荐答案

你可以在Manifest文件中创建没有Main-Class的jar。然后:

Your can create your jar without Main-Class in its Manifest file. Then :

java -cp MyJar.jar com.mycomp.myproj.dir2.MainClass2 /home/myhome/datasource.properties /home/myhome/input.txt

这篇关于如何从Jar运行一个类,它不是其Manifest文件中的Main-Class的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆