简体   繁体   English

如何将 JRE 捆绑到 Java 应用程序的 EXE 中? Launch4j 说“运行时丢失或损坏。”

[英]How do I bundle a JRE into an EXE for a Java Application? Launch4j says “runtime is missing or corrupted.”

I am new to programming in Java but am generally familiar with how everything works.我是 Java 编程的新手,但通常熟悉一切工作原理。 I would like to be able to put both a jar file and a jre into a windows executable(exe) so that when I distribute it, the client needn't have a JRE installed.我希望能够将 jar 文件和 jre 放入 Windows 可执行文件(exe)中,以便在分发它时,客户端不需要安装 JRE。 What program should I use?我应该使用什么程序?

I have launch4j and it seems to do exactly what I want but when I try to run the app, I get "This application was configured to use a bundled Java Runtime Environment but the runtime is missing or corrupted."我有launch4j,它似乎完全符合我的要求,但是当我尝试运行该应用程序时,我得到“此应用程序配置为使用捆绑的Java运行时环境,但运行时丢失或损坏。”

I want my app to just be a runnable exe, not an installer.我希望我的应用程序只是一个可运行的 exe,而不是一个安装程序。 At the very least, can anyone show me how to correctly bundle a JRE with launch4j?至少,谁能告诉我如何正确地将 JRE 与 launch4j 捆绑在一起?

The only way I could bundle a JRE was to use Launch4J and Inno Setup Compiler .我可以捆绑 JRE 的唯一方法是使用Launch4JInno Setup Compiler

First, create a jre6 folder (for example) in the same directory as your output file (.exe).首先,在与输出文件 (.exe) 相同的目录中创建一个jre6文件夹(例如)。

Then copy the JRE from your system into your jre6 folder.然后将 JRE 从您的系统复制到您的jre6文件夹中。

Then you open Launch4J and set the Bundled JRE path - just type in jre6 .然后打开 Launch4J 并设置Bundled JRE 路径- 只需输入jre6 Then click the Build button (obviously, after you've entered all the other parameters - but the only value you need to enter on the JRE tab itself is the Bundled JRE path value.)然后单击 Build 按钮(显然,在您输入所有其他参数之后 - 但您需要在JRE选项卡上输入的唯一值是Bundled JRE 路径值。)

I would have expected that to work, but if you then move the .exe to a new location (so it is no longer co-located with your jre6 folder) you get the This application was configured to use a bundled Java Runtime Environment but the runtime is missing or corrupted error when you try to run the application...我本来希望它可以工作,但是如果您随后将 .exe 移动到一个新位置(因此它不再与您的jre6文件夹位于同一位置),您会得到此应用程序被配置为使用捆绑的 Java 运行时环境,但是当您尝试运行应用程序时,运行时丢失或损坏错误...

I've been playing around with this all day and there was no way I could get Launch4J to include the JRE in the .exe file.我一整天都在玩这个,我无法让Launch4J将 JRE 包含在 .exe 文件中。 Really poor in my opinion, as their documentation does not seem to allude to this issue at all.在我看来真的很糟糕,因为他们的文档似乎根本没有提到这个问题。

So what I did to solve was to use Inno Setup Compiler (ISC) .所以我要解决的是使用Inno Setup Compiler (ISC) This app is used to wrap your .exe as a Windows Installer file.此应用程序用于将您的 .exe 包装为 Windows Installer 文件。 So I added a setting to the ISC script that copies the JRE into the installer package.所以我在 ISC 脚本中添加了一个设置,将 JRE 复制到安装程序包中。 The line I added to the script (in the [Files] section) was:我添加到脚本中的行(在[Files]部分)是:

Source: "M:\Netbeans\MyApp\jre6\*"; DestDir: "{app}\jre6\"; Flags: recursesubdirs createallsubdirs

...a bit of workaround, but it did the trick. ...一些解决方法,但它确实成功了。

Repeat all the above steps, and you should be sorted.重复以上所有步骤,你应该被排序。

the easy method to package jre to exe that lanch4j packaged is use wrap.将 jre 打包为 lanch4j 打包的 exe 的简单方法是使用 wrap。

warp-packer --arch windows-x64 --input_dir mycrt --exec run.bat --output single.exe

and then stop cmd windows when launch exe.然后在启动exe时停止cmd窗口。

editbin /subsystem:windows

warp : https://github.com/dgiagio/warp经线https : //github.com/dgiagio/warp

editbin : installed by VS editbin : 由 VS 安装

Demo:演示:

在此处输入图片说明

在此处输入图片说明

I've never used the Launch4J product, good luck in getting it configured correctly.我从未使用过 Launch4J 产品,祝你能正确配置它。

Looks like you might be able to go to the Discussion Forum on Sourceforge for other hints here看起来你也许可以去论坛Sourceforge上其他提示这里

Other Suggestions:其他建议:

Most of the products I've seen from IBM (Websphere) and Oracle just extract a JRE under the installation directory and configure the startup batch command to use the installed JRE.我从IBM(Websphere)和Oracle看到的大部分产品只是在安装目录下提取一个JRE,并配置启动批处理命令以使用已安装的JRE。 Essentially the JRE and your jar file would be installed in one shot.基本上,JRE 和您的 jar 文件将一次性安装。

The installation exe usually checks to see if it's already installed and skips that step if it finds it already there.安装 exe 通常会检查它是否已经安装,如果发现它已经安装,则跳过该步骤。 This is useful for upgrades of just the jar file.这对于仅升级 jar 文件很有用。

Having the local installation also solves the issue of the customer installing their own JRE which may be incompatible or contain bugs.本地安装还解决了客户安装自己的 JRE 的问题,这些 JRE 可能不兼容或包含错误。 This way your dealing with a known JRE version.这样您就可以处理已知的 JRE 版本。

The excelsior route is OK if you don't have a graphical component to your application (It's been a while, that restriction may have changed).如果您的应用程序没有图形组件,那么 excelsior 路线就可以了(已经有一段时间了,该限制可能已更改)。 There are other restrictions as well, but you probably just better off just distributing a JRE with your code in a single executable installer.还有其他限制,但您可能最好只在单个可执行安装程序中分发带有代码的 JRE。

Bundled JRE Solution for Inno Setup Inno Setup 的捆绑 JRE 解决方案

In order to implement a bundled JRE solution with an application jar, I created an Inno Setup script that:为了使用应用程序 jar 实现捆绑的 JRE 解决方案,我创建了一个 Inno Setup 脚本:

1) copies the JRE into the install exe 1)将JRE复制到安装exe中

2) executes the equivalent of terminal command: java -jar myjar.jar using the bundled JRE 2) 使用捆绑的 JRE 执行等效的终端命令:java -jar myjar.jar

Firstly I copy the JRE:首先我复制JRE:

#define MyJREPath "C:\Program Files\Java\jre1.8.0_191"

[Files]
Source: "{#MyJREPath}\*"; DestDir: "{app}\runtime\jre\";  \
        Flags: ignoreversion recursesubdirs createallsubdirs;  

I follow the directory structure convention shown here: https://docs.oracle.com/javase/8/docs/technotes/guides/deploy/self-contained-packaging.html我遵循此处显示的目录结构约定: https : //docs.oracle.com/javase/8/docs/technotes/guides/deploy/self-contained-packaging.html

To run the equivalent of java -jar myjar.jar:要运行 java -jar myjar.jar 的等效项:

[Run]
Filename: "{app}\runtime\jre\bin\javaw.exe"; Parameters: " -jar myjar.jar"; \
          WorkingDir: "{app}\app\"; \
          Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; \
          Flags: postinstall skipifsilent   

(nb java.exe runs with a terminal and javaw.exe runs without a terminal) (nb java.exe 带终端运行,javaw.exe 不带终端运行)

Desktop shortcut needs to have the right filename, parameters and working directory:桌面快捷方式需要有正确的文件名、参数和工作目录:

[Icons]
Name: "{commondesktop}\{#MyAppName}"; \
      IconFilename: "{app}\app\{#MyAppIcoName}"; \
      Filename: "{app}\runtime\jre\bin\javaw.exe"; \
      Parameters: " -jar myjar.jar"; \
      WorkingDir: "{app}\app\"; \
      Tasks: desktopicon

[Tasks]
Name: "desktopicon"; \
    Description: "{cm:CreateDesktopIcon}"; \
    GroupDescription: "{cm:AdditionalIcons}"; \
    Flags: unchecked

For the icing on the cake, in order make my script handle both bundled JRE and none bundled options I use the Preprocessor IF statement (duplicated in each script section) to test whether the script has an empty MyJREPath or not.锦上添花,为了让我的脚本处理捆绑的 JRE 和不处理捆绑的选项,我使用预处理器 IF 语句(在每个脚本部分中重复)来测试脚本是否有空的 MyJREPath。 If MyJREPath is not empty and so a bundled JRE solution is required I use the coding above;如果 MyJREPath 不为空,因此需要捆绑的 JRE 解决方案,我使用上面的编码; alternatively if a bundled solution is not required then I use more "normal" coding shown the Inno Setup examples or generated by their wizard.或者,如果不需要捆绑解决方案,那么我使用更多“正常”编码,显示 Inno Setup 示例或由他们的向导生成。 Here's the IF statement:这是 IF 语句:

#if MyJREPath != ""
    ; bundled JRE required

#else
    ; bundled JRE not required
#endif

Here's most of my script put together:这是我的大部分脚本放在一起:

        ; Script generated by the Inno Setup Script Wizard.
        ; SEE THE DOCUMENTATION FOR DETAILS ON CREATING INNO SETUP SCRIPT FILES!


        ; some more #defines here

        #define MyAppExeName "javaw.exe"
        #define MyJREPath "C:\Program Files\Java\jre1.8.0_191" 
        ;#define MyJREPath ""




        [Setup]
        ; NOTE: The value of AppId uniquely identifies this application.
        ; Do not use the same AppId value in installers for other applications.
        ; (To generate a new GUID, click Tools | Generate GUID inside the IDE.)
        AppId=XXXXXXXXXX
        AppName={#MyAppName}
        AppVersion={#MyAppVersion}
        ;AppVerName={#MyAppName} {#MyAppVersion}
        AppPublisher={#MyAppPublisher}
        DefaultGroupName={#MyAppPublisher}
        AppPublisherURL={#MyAppURL}
        AppSupportURL={#MyAppURL}
        AppUpdatesURL={#MyAppURL}   
        DefaultDirName={pf}\{#MyDefaultDirName}
        DisableProgramGroupPage=yes
        LicenseFile={#MyInnoSetupDir}\system\{#MyLicenseFile}
        OutputDir={#MyInnoSetupDir}

        #if MyJREPath != "" 
          ; run app with bundled JRE
          OutputBaseFilename={#MyAppName}-{#MyAppVersion}-bundledJRE-setup
        #else
          ; run app without bundled JRE
          OutputBaseFilename={#MyAppName}-{#MyAppVersion}-setup          
        #endif 

        SetupIconFile={#MyInnoSetupDir}\{#MyAppIcoName}
        Compression=lzma
        SolidCompression=yes  
        AppComments={#MyAppName}
        AppCopyright={#MyAppCopyright}  
        UninstallDisplayIcon={#MyInnoSetupDir}\{#MyAppIcoName}
        UninstallDisplayName={#MyAppName}
        WizardImageStretch=No
        WizardSmallImageFile={#MyInnoSetupDir}\{#MyAppBmpName}



        [Languages]
        Name: "english"; MessagesFile: "compiler:Default.isl"
        ;Name: "german"; MessagesFile: "compiler:Languages\German.isl"

        [Tasks]
        Name: "desktopicon"; \
                Description: "{cm:CreateDesktopIcon}"; \
                GroupDescription: "{cm:AdditionalIcons}"; \
                Flags: unchecked
        Name: "quicklaunchicon"; \
                Description: "{cm:CreateQuickLaunchIcon}"; \
                GroupDescription: "{cm:AdditionalIcons}"; \
                Flags: unchecked; OnlyBelowVersion: 0,6.1

        [Files]
        ; bundle JRE if required
        #if MyJREPath != "" 
            Source: "{#MyJREPath}\*"; DestDir: "{app}\runtime\jre\";  \
                Flags: ignoreversion recursesubdirs createallsubdirs;       
        #endif      

        ; copy jar and all files  
        Source: "{#MyInnoSetupDir}\*"; DestDir: "{app}\app\"; \        
                Flags: ignoreversion recursesubdirs createallsubdirs


        [Icons]
        #if MyJREPath != ""
          ; set up icons with bundled JRE
          Name: "{commonprograms}\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\runtime\jre\bin\{#MyAppExeName}"; \
                  Parameters: " -jar {#MyJarName}"; \
                  WorkingDir: "{app}\app\"
          Name: "{commondesktop}\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\runtime\jre\bin\{#MyAppExeName}"; \
                  Parameters: " -jar {#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Tasks: desktopicon            
          Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\runtime\jre\bin\{#MyAppExeName}"; \
                  Parameters: " -jar {#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Tasks: quicklaunchicon
        #else
          ; set up icons without bundled JRE
          Name: "{commonprograms}\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\app\{#MyJarName}"; \
                  WorkingDir: "{app}\app\"
          Name: "{commondesktop}\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\app\{#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Tasks: desktopicon            
          Name: "{userappdata}\Microsoft\Internet Explorer\Quick Launch\{#MyAppName}"; \
                  IconFilename: "{app}\app\{#MyAppIcoName}"; \
                  Filename: "{app}\app\{#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Tasks: quicklaunchicon 
        #endif

        [Run]
        #if MyJREPath != ""
          ; run app with bundled JRE
          Filename: "{app}\runtime\jre\bin\{#MyAppExeName}"; Parameters: " -jar {#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; \
                  Flags: postinstall skipifsilent          
        #else
          ; run app without bundled JRE
          Filename: "{app}\app\{#MyJarName}"; \
                  WorkingDir: "{app}\app\"; \
                  Description: "{cm:LaunchProgram,{#StringChange(MyAppName, '&', '&&')}}"; \
                  Flags: shellexec postinstall skipifsilent          
        #endif 

Hope this helps.希望这可以帮助。

Its easy to bundle jre into launch4j..很容易将 jre 捆绑到 launch4j 中。

just copy the jre into the same output folder只需将 jre 复制到同一个输出文件夹中

In the bundle jre environment text box, just give the jre folder itself在bundle jre environment文本框中,只给出jre文件夹本身

In the environment variable text box (present in the same page below), give until bin在环境变量文本框中(出现在下面的同一页面中)中,给出直到 bin

Then create exe.. It works as expected without jre in machine.然后创建 exe .. 它在机器中没有 jre 的情况下按预期工作。

Thanks谢谢

It seems you need native code compiler.看来您需要本机代码编译器。 These compilers produce native code which doesn't require JRE.这些编译器生成不需要 JRE 的本机代码。

Check this article - https://www.excelsior-usa.com/articles/java-to-exe.html#aot .检查这篇文章 - https://www.excelsior-usa.com/articles/java-to-exe.html#aot

The Java Packager included with JDKs from JDK 7 Update 6, is the official way to embed JRE along with your code in a native executable file. JDK 7 Update 6 中的 JDK 附带的 Java Packager 是将 JRE 与代码一起嵌入本机可执行文件的官方方法 Some maven plugin seems to package the task of packaging and generating native executable for major plateforms.一些maven插件似乎打包了主要平台的打包和生成本地可执行文件的任务。

From JDK11 java packager would need to be downloaded according to java-packager-with-jdk11 witch demonstrates packaging using gradle从 JDK11 java packager 需要根据java-packager-with-jdk11下载,使用 gradle 演示打包

What you're asking for isn't going to be easy to do (if it's doable at all.) If I were you I would look into creating an executable JAR file:你所要求的不会很容易做到(如果它完全可行的话。)如果我是你,我会考虑创建一个可执行的 JAR 文件:

java eclipse create executable jar java eclipse 创建可执行jar

Another option would be to use Java Web Start.另一种选择是使用 Java Web Start。 This was assuming you're using a modern browser the JNLP will automatically prompt the user to install the correct version of Java.这是假设您使用的是现代浏览器,JNLP 将自动提示用户安装正确版本的 Java。

http://docs.oracle.com/javase/tutorial/deployment/webstart/index.html http://docs.oracle.com/javase/tutorial/deployment/webstart/index.html

There are several reason why launch4j wont run smoothly. launch4j 无法顺利运行有几个原因。 some of the reason is:部分原因是:

1) user do not run the application as Administrator 1) 用户不要以管理员身份运行应用程序

2) User do not setup icon image properly. 2) 用户没有正确设置图标图像。 must strictly .ico必须严格.ico

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

相关问题 如何为使用 JDK 11 编译的 java 程序将 JRE 与 Launch4j 捆绑在一起? - How to bundle a JRE with Launch4j for my java program compiled with JDK 11? 无法运行使用launch4j转换的Java应用程序(exe) - Unable to run java application (exe) converted using launch4j 我的launch4j .exe文件无法像我的java文件那样工作。 我想念什么吗? - My .exe file from launch4j does not work like my java file. Am I missing something? 我用 launch4j 将 jar 文件转换为 exe,谷歌说文件 hava 病毒 - I converted jar file to exe with launch4j and google says file hava virus 我用 Launch4j 创建的 .exe 没有运行 - The .exe I created with Launch4j doesn't run Launch4J - 如何将依赖jar附加到生成的exe - Launch4J - how to attach dependent jars to generated exe 使用 Launch4J 将 .java 包装到 .exe 时出现问题,但问题可能是版本合规性 - Problem wrapping .java to .exe using Launch4J but problem is likely version compliance Maven Launch4j插件 - 如何配置以排除.exe使用的jar? - Maven Launch4j Plugin - how to configure to exclude jars used by the .exe? 我有一个Java应用程序在JRE 6中运行,但不在JRE 7中运行。如何在JRE7中运行该应用程序 - I have a Java App running in JRE 6 but not in JRE 7. How do i run the application in JRE7 在jar上将jar转换为exe时出现绝对布局问题 - probleme with absolute layout while converting jar into exe on launch4j
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM