Java jar make exe

Turn any Java program into a self-contained EXE

Double-click to run is one of the easiest ways to open a program.

If the person you are sharing code with already has the right version of Java installed, they can double-click on a jar file to run it. You wrote it once, they can run it there.

If they don’t have Java installed, then there are ways to create a runnable installer like jpackage, but now they have to click through an installer to be able to run your code.

You can use Native Image to turn your code into an exe which won’t require them to have anything installed, but now you have to abide by the closed world assumption and that’s not always easy or possible.

So this post is going to focus on a fairly oonga boonga approach that will work for any app, regardless of what dependencies you include or JVM features you make use of.

The code along with an example GitHub workflow can be found in this repo and final executables can be found here.

Prerequisites

Java 9+

 java --version jlink --version

Maven

NodeJS

Step 1. Compile and Package your code into a jar.

This toy program will create a basic window that has some text that you can toggle between being capitalized.

 package example; import org.apache.commons.text.WordUtils; import javax.swing.*; import java.awt.*; public class Main  public static void main(String[] args)  var label = new JLabel("Hello, World!"); label.setFont(new Font("Serif", Font.PLAIN, 72)); var uppercaseButton = new JButton("Uppercase"); uppercaseButton.addActionListener(e -> label.setText(WordUtils.capitalize(label.getText())) ); var lowercaseButton = new JButton("lowercase"); lowercaseButton.addActionListener(e -> label.setText(WordUtils.uncapitalize(label.getText())) ); var panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(label); panel.add(uppercaseButton); panel.add(lowercaseButton); var frame = new JFrame("Basic Program"); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); > >

The goal is to package up your code, along with its dependencies, into a jar. Jars are just zip files with a little extra structure.

For a Maven project the configuration will look like the following.

  version="1.0" encoding="UTF-8"?> project xmlns="http://maven.apache.org/POM/4.0.0"  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> modelVersion>4.0.0 example javaexe 1.0  UTF-8 18 18    org.apache.commons commons-text 1.9      org.apache.maven.plugins maven-shade-plugin 2.4.3   package  shade     implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer"> manifestEntries> example.Main 1.0          

Where the «shade» plugin will handle including the code from all of your dependencies into the jar. In this case, the only external dependency is org.apache.commons/commons-text .

Then for the purposes of this guide we will move that jar into a new directory where it will be separate from whatever other files are in target/ .

 mkdir build mv target/javaexe-1.0.jar build

Step 2. Create a Java Runtime Environment

In order to run the jar from the previous step, we will need to bundle it with a Java Runtime Environment. To do this we will use jlink .

Since the Java ecosystem hasn’t embraced modules, you most likely haven’t heard of or used jlink .

The short pitch is that it can create «custom runtime images.» Say you are making a web server. You don’t need AWT or Swing, so including all the code for that is a tad wasteful. With jlink you can make a JRE that doesn’t include the java.desktop module at all.

This system works best if your application and all of its dependencies include compiled module-info.java files which let jlink know exactly what modules you want to include. You can also manually figure out the list of required modules by using jdeps and a bit of detective work.

Even without a modular project though, we can still use jlink to effectively clone our Java installation to a directory.

 jlink --add-modules ALL-MODULE-PATH --output build/runtime

Including every module gives confidence that libraries like org.apache.commons/commons-text will work as intended, even though we never figured out what modules they actually require.

Step 3. Bundle the Jar and the JRE into an executable

So with a jar containing our code and all of its dependencies in one hand and a JRE in the other, all that’s left is to stitch the two together.

The general technique for that is to

  1. Zip up the directory containing the JRE and your application jar.
  2. Attach a stub script to the top of that zip file which will extract the zip to a temporary directory and run the code.

There is a JavaScript library which does this called caxa. Its purpose is making NodeJS projects into executables, so it will also bundle whatever NodeJS installation is on the system. That step can luckily be skipped by passing the —no-include-node flag, so it will work just fine for this.

 npx caxa \ --input build \ --output application \ --no-include-node \ -- ">/runtime/bin/java" "-jar" ">/javaexe-1.0.jar"

This will create an executable called » application .» If you are doing this for Windows you should specify » application.exe .» When the executable is run the > s in the command will be substituted for to the temporary directory where the zip file was expanded.

I am aware of jdeploy — and it does handle stuff that I didn’t cover or would be relatively hard with this scheme like code signing or automatic updates — but as far as I can tell it still requires that users run an installer.

On code signing, there is an open issue with caxa to figure out how to do that. I can make another post or update this one if an approach is figured out. I don’t quite understand the issue, so I don’t feel qualified to comment.

If any mildly ambitious reader wants to try their hand at making caxa in a different language so this process isn’t dependent on the JS ecosystem I encourage it.

As always, comments and corrections welcome.

Источник

Кофе-брейк #148. Как превратить любую Java-программу в автономный EXE-файл

Java-университет

Кофе-брейк #148. Как превратить любую Java-программу в автономный EXE-файл - 1

Источник: Mccue.dev Сегодня вы узнаете, как создать из Java-программы исполняемый EXE-файл для запуска в операционной системе Windows. Двойной щелчок для запуска — один из самых простых способов открыть программу. Если у человека, которому вы хотите показать свое приложение, уже установлена ​​правильная версия Java, для запуска он может дважды щелкнуть файл jar. Если же у него не установлена ​​Java, то есть способы создать исполняемый установщик, такой как jpackage. После этого для запуска кода нужно лишь нажать на этот установщик. Также можно использовать Native Image, чтобы превратить код в исполняемый файл, который не требует какой-либо дополнительной установки. В этой статье мы сосредоточимся на довольно простом подходе, который работает для любого приложения, независимо от того, какие зависимости вы включаете или какие функции JVM используете. Код, о котором сегодня пойдет речь, можно найти в репозитории GitHub, а исполняемые файлы с программой выложены здесь.

Используемый стек

Java 9+

Maven

NodeJS

Шаг 1. Скомпилируйте и упакуйте свой код в jar

Эта базовая программа создаст простое окно с текстом, который вы можете менять, нажимая на одну из кнопок в интерфейсе.

 package example; import org.apache.commons.text.WordUtils; import javax.swing.*; import java.awt.*; public class Main < public static void main(String[] args) < var label = new JLabel("Hello, World!"); label.setFont(new Font("Serif", Font.PLAIN, 72)); var uppercaseButton = new JButton("Uppercase"); uppercaseButton.addActionListener(e ->label.setText(WordUtils.capitalize(label.getText())) ); var lowercaseButton = new JButton("lowercase"); lowercaseButton.addActionListener(e -> label.setText(WordUtils.uncapitalize(label.getText())) ); var panel = new JPanel(); panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS)); panel.add(label); panel.add(uppercaseButton); panel.add(lowercaseButton); var frame = new JFrame("Basic Program"); frame.add(panel); frame.pack(); frame.setVisible(true); frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); > > 

Сейчас наша цель состоит в том, чтобы упаковать код вместе с его зависимостями в jar. JAR-файлы — это обычные ZIP-архивы с небольшой дополнительной структурой. Для проекта Maven конфигурация будет выглядеть следующим образом.

Здесь плагин “shade” будет обрабатывать включение кода из всех ваших зависимостей в jar. В данном случае единственной внешней зависимостью является org.apache.commons/commons-text.

Шаг 2. Создайте среду выполнения Java (Java Runtime Environment, JRE)

Чтобы запустить уже созданный нами jar-файл, нужно связать его со средой выполнения Java. Для этого мы будем использовать jlink. Поскольку в экосистеме Java не используются модули, то вы, скорее всего, не слышали о них и не использовали jlink. Короче говоря, jlink может создавать “настраиваемые исполняемые образы”. Например, вы делаете веб-сервер. Вам не нужны AWT или Swing, поэтому включать их в код будет лишним. С помощью jlink вы можете создать JRE, которая вообще не включает модуль java.desktop. Эта система работает лучше всего, если ваше приложение и все его зависимости включают скомпилированные файлы module-info.java, которые дают jlink точную информацию, какие модули вы хотите включить. Вы также можете вручную определить список необходимых модулей, используя jdeps. И даже без модульного проекта мы можем эффективно клонировать нашу инсталляцию Java в каталог с помощью jlink.

Включение каждого модуля по отдельности дает уверенность в том, что такие библиотеки как org.apache.commons/commons-text будут работать именно так, как задумано. Нужно лишь выяснить, какие модули нам требуются.

Шаг 3. Объедините Jar и JRE в исполняемый файл

  1. Заархивируйте каталог, содержащий JRE и jar вашего приложения.
  2. Прикрепите сценарий-заглушку (stub script) к верхней части этого zip-файла, который извлечет данные во временный каталог и запустит код.

npx caxa \ —input build \ —output application \ —no-include-node \ — «<>/runtime/bin/java» «-jar» «<>/javaexe-1.0.jar»

Это создаст исполняемый файл с именем “application”. Если вы создаете его для Windows, то нужно указать “application.exe”. Когда исполняемый файл запускается, <> будет заменен на временный каталог, в котором был развернут zip-файл. Учтите, что при создании исполняемых файлов используются также и такие механизмы, как подпись кода и автоматические обновления. Однако эти вещи требуют более глубокого изучения, которое трудно вместить в одну публикацию.

Источник

Конвертируем JAR в исполняемый (.exe) файл

Java-университет

Кратчайшая инструкция. Пишем простейшее тестовое приложение, допустим «Hi Amigo!» в файл HiAmigo.txt 100 раз.

 public class Main < public static void main(String[] args) throws IOException < File file = new File("C:\\temp\\HiAmigo.txt"); FileWriter fileWriter = new FileWriter(file); for (int i = 0; i < 100 ; i++) < fileWriter.write("Hi Amigo! \n"); >fileWriter.close(); > > 

Проверяем что все работает на этом этапе (это важно!) Конвертируем JAR в исполняемый (.exe) файл - 1Идем File -> Project Structure -> Artifacts -> + JAR -> From Modules with dependencies.. Конвертируем JAR в исполняемый (.exe) файл - 2Указываем путь к нашему Main.class: Конвертируем JAR в исполняемый (.exe) файл - 3Жмем ОК Жмем Build Artifacts — > Action -> Build Конвертируем JAR в исполняемый (.exe) файл - 4Появляется наш артефакт: Конвертируем JAR в исполняемый (.exe) файл - 5Удаляем наш тестовый файл «C:\\temp\\HiAmigo.txt» и запускаем JAR. Файл должен появиться еще раз. Если появился — все ок. Едем дальше. Качаем тулзу с сайта https://launch4j.sourceforge.net/ (Рекомендую версию 3.14, потом объясню почему). Устанавниваем запускаем. Нам нужно два поля. Указываем наш JAR и куда класть готовый .exe Конвертируем JAR в исполняемый (.exe) файл - 6Сейчас самое интересное. Представьте ситуацию что на машине где мы планируем использовать наше приложение не установлена JRE и нет возможности ее установить. Такое часто бывает по соображениям безопасности. К тому же хорошо бы обеспечить наше приложение более высоким уровнем автономности. Делаем следующее. Создаем отдельный каталог для нашего приложения, пусть OurApp. Внутри создаем папку JRE. Качаем JRE (в моем примере пусть будет jre-8u361-windows-i586) устанавливаем куда нибудь (если это готовый архив разархивируем) и выдергиваем из нее все файлы. Копируем все в \OurApp\JRE\ должно получиться примерно так: Конвертируем JAR в исполняемый (.exe) файл - 7В Launch4j переходим во вкладку JRE и в поле Bundled JRE paths: указываем имя каталога JRE Конвертируем JAR в исполняемый (.exe) файл - 8Жмем на шестеренку и выбираем любой файл для сохранения конфигурации типа file.xml Конвертируем JAR в исполняемый (.exe) файл - 9После нажатия на save появится наш exe. Конвертируем JAR в исполняемый (.exe) файл - 10Копируем exe и кладем рядом рядом с папкой JRE Конвертируем JAR в исполняемый (.exe) файл - 11Удаляем тестовый C:\temp\HiAmigo.txt. Запускаем exe и смотрим как создается новый. Вот и все. Теперь каталог можно OurApp можно архивировать и передавать куда угодно, exe отработает. Важно: Версия JRE в папке должна быть не ниже той на которой билдился артефакт. Проверяйте поле: Конвертируем JAR в исполняемый (.exe) файл - 12В моем случае возникли проблемы совместимости с x86 платформой и я использовал версию враппера 3.14, так как в ней можно явно указывать тип целевой архитектуры. В общем всем спасибо за внимание. Это была моя первая статья-инструкция, прошу не судить строго. Всем мир!

Источник

Читайте также:  Storg mx index php
Оцените статью