如何在Eclipse中创建可运行的JAR,就像在Eclipse中一样

时间:2023-01-13 13:22:41

My process for creating a runnable JAR from a project with many libraries with Eclipse has been.

我从使用Eclipse的许多库的项目创建可运行JAR的过程已经完成了。

Export > Runnable JAR > Select launch configuration > Package required libraries into generated JAR > Finish

导出> Runnable JAR>选择启动配置>将所需的包打包到生成的JAR>完成

This creates a single JAR in my export destination which I FTP to my server and run fine.

这会在我的导出目标中创建一个JAR,我将其FTP到我的服务器并运行正常。

I recently switched to IntelliJ for various reasons and it has been an improvement in all cases except for building my JARs which forces me to go back to eclipse temporarily.

我最近因为各种原因切换到IntelliJ,除了构建我的JAR之外,它在所有情况下都有所改进,这迫使我暂时回到日食。

With IntelliJ I:

使用IntelliJ I:

Open Project Structure > Artifacts and create a new JAR from modules with dependencies. I then have my output layout with the name of the JAR, a META-INF inside it and all my libaries with the format Extracted <lib.jar/> (sorry I can't upload screenshots on this VPN.)

打开项目结构>工件,并从具有依赖项的模块创建新的JAR。然后,我的输出布局包含JAR的名称,其中的META-INF以及所有我的库,其格式为Extracted (抱歉,我无法上传此VPN上的屏幕截图。)

I build my artifact (which is about 15MB bigger) and FTP it to my server, try to run it and I get the error:

我构建我的工件(大约大15MB)并将其FTP到我的服务器,尝试运行它,我得到错误:

Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes

How do I mimic how I do it in Eclipse?

我如何模仿我在Eclipse中的表现?

EDIT

编辑

The two JARs that are created differ considerably:

创建的两个JAR差别很大:

The valid jar that Eclipse creates looks like this: 如何在Eclipse中创建可运行的JAR,就像在Eclipse中一样

Eclipse创建的有效jar看起来像这样:

Whereas the invalid .JAR looks like this: 如何在Eclipse中创建可运行的JAR,就像在Eclipse中一样

而无效的.JAR看起来像这样:

4 个解决方案

#1


8  

You're facing two issues one seems major and one is minor:

你面临两个问题,一个似乎很重要,一个是次要问题:

  • Major: signature exception
  • 专业:签名例外
  • Minor: size of the jar is 15 MB greater than the jar produced by eclipse.
  • 轻微:jar的大小比eclipse生成的jar大15 MB。

The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar then add all the jar's from the available elements pan as shown in the figure.

两个问题的解决方案在于构建工件的方式。首先,删除所有提取的* .jar,然后从可用元素pan中添加所有jar,如图所示。

如何在Eclipse中创建可运行的JAR,就像在Eclipse中一样

It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.

很明显,仅添加包装(压缩)的罐子会减小尺寸。但它也解决了签署问题。有关更多说明,请查看此文章。我只会引用一行。

It's probably best to keep the official jar as is and just add it as a dependency in the manifest file......

最好保留官方jar,只需将其作为依赖项添加到清单文件中......

It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.

似乎某些依赖项已签名并且重新打包会混淆您项目的META-INF。

#2


1  

You can these steps:

你可以这些步骤:

1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies

1)文件 - >项目结构 - >项目设置 - >工件 - >罐子 - >来自具有依赖关系的模块

2) Check the Include in project build checkbox.

2)选中项目构建中的包括复选框。

3) Right mouse click on module -> build module 'ModuleName'

3)右键单击模块 - >构建模块'ModuleName'

#3


0  

you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.

你可以尝试使用gradle脚本。 eclipse会让你成为一个gradle项目。你可以使用任何ide的构建文件。它有一个内置的jar任务。

#4


0  

If you are using Maven you need to put META-INF directory under /main/resources folder instead of main/java.

如果您使用的是Maven,则需要将META-INF目录放在/ main / resources文件夹下,而不是main / java。

Reference: Intellij - Nikolay Chashnikov

参考:Intellij - Nikolay Chashnikov

Then you can execute the runnable jar normally with java -jar filename.jar

然后你可以使用java -jar filename.jar正常执行runnable jar

#1


8  

You're facing two issues one seems major and one is minor:

你面临两个问题,一个似乎很重要,一个是次要问题:

  • Major: signature exception
  • 专业:签名例外
  • Minor: size of the jar is 15 MB greater than the jar produced by eclipse.
  • 轻微:jar的大小比eclipse生成的jar大15 MB。

The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar then add all the jar's from the available elements pan as shown in the figure.

两个问题的解决方案在于构建工件的方式。首先,删除所有提取的* .jar,然后从可用元素pan中添加所有jar,如图所示。

如何在Eclipse中创建可运行的JAR,就像在Eclipse中一样

It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.

很明显,仅添加包装(压缩)的罐子会减小尺寸。但它也解决了签署问题。有关更多说明,请查看此文章。我只会引用一行。

It's probably best to keep the official jar as is and just add it as a dependency in the manifest file......

最好保留官方jar,只需将其作为依赖项添加到清单文件中......

It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.

似乎某些依赖项已签名并且重新打包会混淆您项目的META-INF。

#2


1  

You can these steps:

你可以这些步骤:

1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies

1)文件 - >项目结构 - >项目设置 - >工件 - >罐子 - >来自具有依赖关系的模块

2) Check the Include in project build checkbox.

2)选中项目构建中的包括复选框。

3) Right mouse click on module -> build module 'ModuleName'

3)右键单击模块 - >构建模块'ModuleName'

#3


0  

you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.

你可以尝试使用gradle脚本。 eclipse会让你成为一个gradle项目。你可以使用任何ide的构建文件。它有一个内置的jar任务。

#4


0  

If you are using Maven you need to put META-INF directory under /main/resources folder instead of main/java.

如果您使用的是Maven,则需要将META-INF目录放在/ main / resources文件夹下,而不是main / java。

Reference: Intellij - Nikolay Chashnikov

参考:Intellij - Nikolay Chashnikov

Then you can execute the runnable jar normally with java -jar filename.jar

然后你可以使用java -jar filename.jar正常执行runnable jar