简体   繁体   English

在嵌入式jar中使用入口点创建可执行jar

[英]Create executable jar with entrypoint in an embedded jar

I have an Eclipse project that uses many .jar files as dependencies, but has no source files. 我有一个Eclipse项目,它使用许多.jar文件作为依赖项,但是没有源文件。 There is a run configuration set up that defines the main method (entry point) as a method within one of the included jars. 设置了运行配置,该配置将main方法(入口点)定义为其中一个jars中的方法。 It runs fine within Eclipse, but when I try to use the executable jar wizard, I get the error "Could not find main method from given launch configuration." 它在Eclipse中运行良好,但是当我尝试使用可执行jar向导时,出现错误“无法从给定的启动配置中找到主要方法”。

According to this tutorial , one can use the jar command line tool to manually set the entrypoint of a jar, but what should I use as the path to the .class file for the main method, since I don't directly have any .class files, just embedded jars? 根据本教程 ,可以使用jar命令行工具手动设置jar的入口点,但是我应该使用哪种方法作为main方法的.class文件的路径,因为我没有直接的任何.class。文件,只是嵌入式jars?

Alternatively, are there any other ways to get the entrypoint set correctly? 另外,还有其他方法可以正确设置入口点吗?

The standard JAR file format allows a manifest and a set of class/resource files. 标准JAR文件格式允许清单和一组类/资源文件。 Of course, you can package up JAR files inside a JAR file, but the standard Java class loader is unable to load classes (from the embedded JAR files) that way. 当然,您可以将JAR文件打包在JAR文件中,但是标准Java类加载器无法以这种方式(从嵌入式JAR文件)加载类。 Thus, you'll need to expand all the JAR files and repackage them according to the packages. 因此,您将需要扩展所有JAR文件并根据软件包对其进行重新打包。 Again, this is true if you are not writing your own class loader. 同样,如果您没有编写自己的类加载器,则为true。

If you are using Maven, fortunately, Maven provides a few plugins to do this repackaging task. 幸运的是,如果您正在使用Maven,则Maven提供了一些插件来执行此重新打包任务。 See the Maven JAR plugin , or the Maven Shade Plugin that help creating the so-called Uber JAR which can optionally use a Main-Class manifest attribute. 请参阅Maven JAR插件Maven Shade插件 ,它们可以帮助创建所谓的Uber JAR,后者可以选择使用Main-Class清单属性。 Alternatively, in the absence of Maven, you can always expand and rejar the classes by using a simple shell script. 另外,在没有Maven的情况下,您始终可以使用简单的shell脚本来扩展和重新封装类。

Once you do that, you can use either java -jar myjar.jar , or java -cp myjar.jar my.main.Class . 完成此操作后,可以使用java -jar myjar.jarjava -cp myjar.jar my.main.Class

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

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