简体   繁体   English

从另外2个可执行jar创建一个可执行jar

[英]Creating an executable jar from 2 other executable jar

I want to create one executable jar file from 2 other executable jar files. 我想从其他2个可执行jar文件中创建一个可执行jar文件。 So i'm working on a betting project and i have 2 projects, Client and Monitor. 所以我正在做一个投注项目,我有2个项目,即Client和Monitor。 I created their jar files and everything is fine. 我创建了他们的jar文件,一切都很好。 But is there a way to combine their jar files so i can run both from 1 jar file so i don't have to run each time both client.jar and monitor.jar? 但是有没有一种方法可以合并他们的jar文件,这样我就可以从1个jar文件中运行这两个文件,这样我就不必每次都运行client.jar和monitor.jar了?

Create another project and include that two jar files in the third one's class path. 创建另一个项目,并将这两个jar文件包含在第三个项目的类路径中。

Now you can copy the same code from main methods from jar A and Jar B, and paste that in main method of jar C. now you can run only the Jar C. 现在,您可以从jar A和Jar B的main方法中复制相同的代码,并将其粘贴到jar C的main方法中。现在您只能运行JarC。

But is there a way to combine their jar files so i can run both from 1 jar 但是有没有一种方法可以合并他们的jar文件,这样我就可以从1个jar都运行

There is no easy way but to unzip and then get all of the contents of all of the jar files to be extracted, and then zipped up again into one jar. 除了解压缩然后获取要提取的所有jar文件的所有内容,然后再次压缩到一个jar中,没有别的简单方法。

You need to cautious in the merge of your Manifest.MF file which defines your entry point of main class having main method. 在Manifest.MF文件的合并中需要谨慎,该文件定义了具有main方法的主类的入口点。

Note: But I am not sure if you will be able to have 2 main class in your manifest file. 注意:但是我不确定清单文件中是否能够包含2个主类。 You might have to execute via classpath in such case. 在这种情况下,您可能必须通过classpath执行。

Im using netbeans for java. 我正在使用netbeans for java。 The easiest way that you want is to create new project and add the 2 jar files as library to your project 您想要的最简单方法是创建新项目并将2个jar文件作为库添加到您的项目中

It is possible to combine those two jar files to one jar file, however, every jar file can only have one main class that gets run if no main class is specified via command line. 可以将这两个jar文件合并为一个jar文件,但是,如果未通过命令行指定任何主类,则每个jar文件只能具有一个要运行的主类。

This means you would have to either create a new main class which executes both previous main classes, or end up running the same jar file twice with different arguments. 这意味着您将必须创建一个新的主类来执行之前的两个主类,或者最终必须使用不同的参数两次运行相同的jar文件。

However, I would not recommend combining those two. 但是,我不建议将两者结合使用。 There will be a reason those two are seperate projects, so why would you combine them both? 这两个项目是分开的项目是有原因的,那么为什么要将两者结合? Also, if one jar file crashes, the second will still run. 另外,如果一个jar文件崩溃,第二个仍将运行。

If you still want to combine those two I see two options: 如果您仍然想将两者结合起来,我会看到两个选择:

  • Either you create a third project (I assume maven?) which depends on both projects and contains the new main class 您要么创建第三个项目(我假设是Maven?),这取决于两个项目并包含新的主类
  • Or, if you run into class loading problems, as the first approach will put the jars into a jar file, you could use the maven shade plugin to get one flat jar file. 或者,如果遇到类加载问题,因为第一种方法会将jar放入jar文件中,则可以使用maven shade插件来获取一个平坦的jar文件。 This is sometimes necessary, if you use third party libs where you cannot control how resources are loaded. 如果您使用无法控制资源加载方式的第三方库,则有时这是必要的。

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

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