简体   繁体   English

如何从包含 JRE 的 jar for mac os(使用 windows 或 linux)创建可执行文件

[英]How to create an executable file from a jar for mac os (using windows or linux) with a JRE included

Good evening everyone.各位晚上好。

I am searching for many days a simple and non-deprecated answer to my question:我正在寻找很多天对我的问题的简单且不被弃用的答案:

I've created a java program, on my computer (Windows).我在我的电脑(Windows)上创建了一个 java 程序。 From that program I've created a runnable jar, which perfectly runs on my computer.通过该程序,我创建了一个可运行的 jar,它可以在我的计算机上完美运行。 Now, I want to convert that jar into a runnable file format for MAC OS, and include a JRE inside my app (so my client won't have to install java on each of his computer).现在,我想将 jar 转换为 MAC OS 的可运行文件格式,并在我的应用程序中包含一个 JRE(这样我的客户就不必在他的每台计算机上安装 java)。

After some research, the only solutions that I've found are too old (unusable) or very complicated (and not enough documented).经过一些研究,我发现的唯一解决方案太旧(无法使用)或非常复杂(并且没有足够的文档记录)。

I've heard about jar2app, but the part about the JRE that you have to include is not really clear.我听说过 jar2app,但是您必须包含的有关 JRE 的部分并不是很清楚。 A lot of people also answer with launch4j, but it only creates Windows executable.很多人也用 launch4j 回答,但它只创建 Windows 可执行文件。

I have both Linux and Windows, but can't access to a MAC OS computer to develop anything (just test).我同时拥有 Linux 和 Windows,但无法访问 MAC OS 计算机来开发任何东西(只是测试)。 I am using java 1.8.我正在使用 java 1.8。

Given below is an excerpt from this article and I hope, it should work for Java-8 too:下面是本文的摘录,我希望它也适用于 Java-8:

Since this example defines the runtime sub-element using JAVA_HOME , make sure it is configured correctly for your environment.由于此示例使用JAVA_HOME定义运行时子元素,因此请确保为您的环境正确配置它。 For example, in your .bashrc file , define JAVA_HOME as follows:例如,在您的.bashrc file中,定义JAVA_HOME如下:

export JAVA_HOME=/usr/libexec/java_home

Use the following steps to modify the build.xml file at the top of the project directory:使用以下步骤修改项目目录顶部的build.xml文件:

  1. Specify an environment property, named env:指定一个名为 env 的环境属性:

    <property environment="env" />

  2. In the target that creates the bundle, specify the location of the JRE on your system, using the env property:在创建包的目标中,使用 env 属性指定 JRE 在系统上的位置:

    <runtime dir="${env.JAVA_HOME}" />

The resulting build.xml file should look like the following:生成的build.xml文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<project name="ButtonDemo" default="default" basedir=".">
    <import file="nbproject/build-impl.xml"/>

<property environment="env" />

<taskdef name="bundleapp"
         classname="com.oracle.appbundler.AppBundlerTask"
         classpath="lib/appbundler-1.0ea.jar" />

<target name="bundle-buttonDemo">
    <bundleapp outputdirectory="dist"
        name="ButtonDemo"
        displayname="Button Demo"
        identifier="components.ButtonDemo"
        mainclassname="components.ButtonDemo">
        <runtime dir="${env.JAVA_HOME}" />
        <classpath file="dist/ButtonDemo.jar" />
    </bundleapp>
</target>

</project>

Create a fresh version of ButtonDemo.app , using the ant bundle-buttonDemo command.使用ant bundle-buttonDemo命令创建一个新版本的ButtonDemo.app The resulting version includes the JRE in the app package.生成的版本包括应用程序 package 中的 JRE。 You can confirm this by examining the Contents/PlugIns directory inside of the app package.您可以通过检查应用程序 package 内的 Contents/PlugIns 目录来确认这一点。

Consider using this Runtime Plugin for non-modular apps.考虑将此运行时插件用于非模块化应用程序。 Or if it's modular, there is a similar plugin .或者如果它是模块化的,有一个类似的插件 The github page is active, and issues are addressed promptly in my experience. github 页面处于活动状态,根据我的经验,问题很快得到解决。 It builds for Windows, Linux and OS X.它适用于 Windows、Linux 和 OS X。

Even though it uses the recent Java feature of jpackage , you should still be able to compile your code in an earlier JDK version while using jpackage from a newer JDK version.即使它使用了jpackage的最新 Java 功能,您仍然应该能够在使用较新 JDK 版本的jpackage时在较早的 JDK 版本中编译您的代码。

I recently updated a project to use it, which you are welcome to look at (give feedback if you do).我最近更新了一个项目来使用它,欢迎您查看(如果您这样做,请提供反馈)。 There are also other examples from the plugin's site.插件网站上还有其他示例。

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

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