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

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

问题描述

我有一个包含 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

主要类的目录结构如下:

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

我知道我可以在我的清单文件中指定一个类作为主类.但是有什么方法可以在命令行上指定一些参数来运行我想运行的任何类吗?

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.然后:

You 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 运行一个类,该类不是其清单文件中的主类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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