Java Launch4J - 如何将依赖的 jars 附加到生成的 exe

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19917960/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:17:53  来源:igfitidea点击:

Launch4J - how to attach dependent jars to generated exe

javajarlaunch4j

提问by norbi771

I have a simple java project, which requires external jars. I build this with netbeans and after Clean and Build command, I can find in dist directory the following structure:

我有一个简单的 java 项目,它需要外部 jars。我用 netbeans 构建它,在 Clean and Build 命令之后,我可以在 dist 目录中找到以下结构:

-myApp.jar
-lib/
     library1.jar
     library2.jar

typical, I would say.

典型的,我会说。

Now, I'd like to distribute myApp.jar with dependent libraries as one exe. Is this possible? I am trying to use Launch4J. In the GUI I create the config file, there are some options in cpsection

现在,我想将带有依赖库的 myApp.jar 作为一个 exe 分发。这可能吗?我正在尝试使用 Launch4J。在GUI中我创建了配置文件,cp部分有一些选项

<cp>lib/swing-layout-1.0.4.jar</cp>

but it seems to be classpath, and it is the only place I can refer to my extra jars.

但它似乎是类路径,它是我唯一可以引用我的额外 jar 的地方。

After exe file is created, I can't find dependend libs in the exe (exe can be opened with winrar) and thus my application crashes.

创建exe文件后,我在exe中找不到依赖库(exe可以用winrar打开),因此我的应用程序崩溃了。

How can I make the exe file properly then?

那么如何正确制作exe文件呢?

Thanks for your help.

谢谢你的帮助。

采纳答案by norbi771

As it often happens being unable to solve the problem I published it on StackOverflow ... and pretty soon after publishing the question I got an idea.

由于经常发生无法解决问题的情况,因此我将其发布在 StackOverflow 上……在发布问题后不久我就有了一个想法。

So the answer to my question is:

所以我的问题的答案是:

Put all the dependent jars into one main jar.

将所有依赖的 jar 放入一个主 jar 中。

It took me some time to find info how can I do that.

我花了一些时间来查找信息,我该怎么做。

To help people I decided to publish detailed instruction here - they are based on Netbeans 7.4.

为了帮助人们,我决定在此处发布详细说明 - 它们基于 Netbeans 7.4。

  1. Following article from http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/I created the ant script that build one-jar-app for me. I could then manually create exe via Launch4J

  2. I then decided that I want more automated task, and I did that, Ant builds exe for me (via Launch4J)

  3. Then I realized that I must do "clean and build" before my automated task (in point 2)/ I decided that I want clean and build to be done automatically before the exe build

  1. 根据http://mavistechchannel.wordpress.com/2010/08/17/how-to-build-a-single-jar-file-with-external-libs/ 的文章,我创建了构建 one-jar 的 ant 脚本-应用程序为我。然后我可以通过 Launch4J 手动创建 exe

  2. 然后我决定我想要更自动化的任务,我做到了,Ant 为我构建了 exe(通过 Launch4J)

  3. 然后我意识到我必须在我的自动化任务之前进行“清理和构建”(第 2 点)/我决定在 exe 构建之前自动完成清理和构建

Putting all together I am attaching my ant build script consisting of points 1,2,3:

总而言之,我附上了我的 ant 构建脚本,包括 1,2,3 点:

It is required to edit build.xml and put the content found below before "project" end tag

需要编辑 build.xml 并将下面找到的内容放在“项目”结束标记之前

<target name="package-for-launch4j" depends="clean,compile,jar">
    <property name="launch4jexe.dir" location="C:\Program Files (x86)\Launch4j" />
    <taskdef name="launch4j"
             classname="net.sf.launch4j.ant.Launch4jTask"
             classpath="${launch4jexe.dir}/launch4j.jar
            :${launch4jexe.dir}/lib/xstream.jar" />
    <property name="launch4j.jar.name" value="MyAppJarName"/>
    <property name="launch4j.dir" value="exe"/>
    <property name="launch4j.jar" value="${launch4j.dir}/${launch4j.jar.name}.jar"/>
    <echo message="Packaging ${application.title} into a single JAR at ${launch4j.jar}"/>
    <delete dir="${launch4j.dir}"/>
    <mkdir dir="${launch4j.dir}"/>
    <jar destfile="${launch4j.dir}/temp_final.jar" filesetmanifest="skip">
        <zipgroupfileset dir="dist" includes="*.jar"/>
        <zipgroupfileset dir="dist/lib" includes="*.jar"/>
        <manifest>
        <attribute name="Main-Class" value="${main.class}"/>
        </manifest>
    </jar>
    <zip destfile="${launch4j.jar}">
        <zipfileset src="${launch4j.dir}/temp_final.jar"
            excludes="META-INF/*.SF, META-INF/*.DSA, META-INF/*.RSA"/>
    </zip>
    <delete file="${launch4j.dir}/temp_final.jar"/>
    <launch4j configFile="misc/l4j-myapp.xml" />
</target>

then in Netbeans rightclick on the build.xml and choose: Run Target / Other Targets / package-for-launch4j

然后在 Netbeans 中右键单击 build.xml 并选择:Run Target / Other Targets / package-for-launch4j

exe file is ready in exe folder :-)

exe 文件已在 exe 文件夹中准备就绪 :-)

回答by Felipe Volpato

I have spent hours on this issue. So here is my contribution.

我在这个问题上花了几个小时。所以这是我的贡献。

The problem here: how to sucessfully put your external jars that your .JAR program needs INSIDE the .exe that you generate.

这里的问题是:如何成功地将 .JAR 程序需要的外部 jar 放入生成的 .exe 中。

We assume that you already, and correctly, configured the external jars on eclipse/netbeans and ALL WORK FINE with the command: java -jar yourprogram.jar.

我们假设您已经正确地在 eclipse/netbeans 上配置了外部 jar,并且使用以下命令一切正常:java -jar yourprogram.jar。

So the real problem is how to ensure that this .EXE file will contain the external jars, otherwise it will not work properly.

所以真正的问题是如何确保这个 .EXE 文件将包含外部 jars,否则它将无法正常工作。

1) First of all, you need to forget Launch4J and anyother program.

1) 首先,您需要忘记 Launch4J 和任何其他程序。

2) Install JSmooth, I recommend that you use the windows version.

2)安装JSmooth,我建议你使用windows版本。

3) On the left menu there is a button "Application". Click on it.

3) 在左侧菜单上有一个按钮“应用程序”。点击它。

4) You will see a tab "Classpath" panel. Click on the plus (+) and add your external .jar's files. And that is it!!

4)您将看到一个选项卡“类路径”面板。单击加号 (+) 并添加外部 .jar 文件。就是这样!!

Don't forget to put your .jar application marking checkbox "use am embedded jar" and choose the main class properly. It will work.

不要忘记将您的 .jar 应用程序标记复选框“使用嵌入式 jar”并正确选择主类。它会起作用。

回答by Salman Saleh

I also faced the same issue while migrating my .jar to exe. I also had many dependent libraries as well. So These were the steps I performed :

我在将 .jar 迁移到 exe 时也遇到了同样的问题。我也有很多依赖库。所以这些是我执行的步骤:

  1. Download and Install launch4j.

  2. Open your project in netbeans. Clean and build the project.

  3. Make sure you have a folder named 'dist' in the project directory. It will have your jar files with lib folder(containing the dependent libraries).
  4. Open launch 4j.
  5. Create output file in the dist folder. For example : OutputFile : D:******\My_App\dist\my_application.exe
  6. Browse your jar file in the next row. For example : Jar : D:******\My_App\dist\my_application.jar
  7. Go to classpath tab. Tick CustomClasspath. Press browse icon, and browse to your jar file which is located in the dist folder.
  8. Specify Min Jre version in the JRE tab.
  9. Save the configration.
  10. Build the wrapper(by clicking the settings icon)
  11. Your exe file will be generated in the dist folder. Thats it :)
  1. 下载并安装launch4j。

  2. 在 netbeans 中打开您的项目。清理并构建项目。

  3. 确保您在项目目录中有一个名为“dist”的文件夹。它将包含带有 lib 文件夹(包含依赖库)的 jar 文件。
  4. 打开发射 4j。
  5. 在 dist 文件夹中创建输出文件。例如:输出文件:D:******\My_App\dist\my_application.exe
  6. 在下一行浏览您的 jar 文件。例如: Jar : D:******\My_App\dist\my_application.jar
  7. 转到类路径选项卡。勾选自定义类路径。按浏览图标,然后浏览到位于 dist 文件夹中的 jar 文件。
  8. 在 JRE 选项卡中指定 Min Jre 版本。
  9. 保存配置。
  10. 构建包装器(通过单击设置图标)
  11. 您的 exe 文件将在 dist 文件夹中生成。就是这样 :)

回答by Kahler Rockwell

To include external libraries with Launch4j you have to have the ".jar" files of the external libraries near your .exe (mine are just in the same folder) then in "Classpath" you put in the path to the .jar files into "Edit Item: "

要在 Launch4j 中包含外部库,您必须在 .exe 附近放置外部库的“.jar”文件(我的只是在同一文件夹中),然后在“Classpath”中将 .jar 文件的路径放入“编辑项目:“

回答by MBK

In launch4j go to the classpath tab. Tick the custom classpath field. In the edit field, enter the full path of each jar you want included and press accept. When finished type just the name of the main class into the separate field (ie MyProg). All the jars will now be included in the exe.

在 launch4j 中,转到类路径选项卡。勾选自定义类路径字段。在编辑字段中,输入要包含的每个 jar 的完整路径,然后按接受。完成后,只需将主类的名称键入单独的字段(即 MyProg)。现在所有的 jars 都将包含在 exe 中。

PS I have all the jars in the same directory. I'm using version 3.12

PS我在同一个目录中的所有罐子。我正在使用 3.12 版

回答by SaFteiNZz

When you are converting your .jar file

转换 .jar 文件时

  1. Go to classpath tab
  2. Check custom classpath
  3. On main class select your .jar from your dist folder after building the project
  4. On the classpath textarea add your libraries, you add them right below that textarea writting the full path to the lib (of course including the lib, ie "C:\folder\lib\file.jar")
  1. 转到类路径选项卡
  2. 检查自定义类路径
  3. 构建项目后,在主类上从 dist 文件夹中选择您的 .jar
  4. 在类路径 textarea 添加您的库,您将它们添加到 textarea 正下方写入 lib 的完整路径(当然包括 lib,即“C:\folder\lib\file.jar”)

Example

例子