make an Eclipse project executable at any time

Asked 1 years ago, Updated 1 years ago, 35 views

Thank you for your help.I'd like to make a project made with Eclipse ready to run (like launching with a double click instead of a command prompt), but I don't know what to do because it's my first time.I looked it up myself and tried to make it .jar from the export, but it didn't start when I double-clicked it.Thank you for your cooperation.

java

2022-09-30 21:28

2 Answers

If you do not create the appropriate manifest file, it will not become an executable jar file.

Have you tried using keywords like "executable jar" or "executable jar"?There will be a lot of information.

For example, how about this page?

http://sunjava.seesaa.net/article/57546363.html


2022-09-30 21:28

Assume Windows 10.Also, the commands listed are running on PowerShell.

The sample code is as follows.I'm using Swing because I want to open it with a double click, so it's probably an application with a GUI.

Executable-jar here refers to a file that works as expected with the following commands:

java-jar <jar filename >

exceedable-jar has approximately the following properties:

  • The jar file contains all the .class files needed to run
  • Main-Class attributes in manifest

(Actually, creating executable-jar is not a must to solve this question, but I think it would be better to do so to simplify the problem.)

(*This method is no longer mainstream.Try creating with Maven as described below.)

You can right-click this project in Project Explorer and create it from Export>Runnable JAR file.
At this time, make sure to select "Extract required libraries into generated JAR" as the Library handling option.

Enter a description of the image here

If it's a Maven package, it's better to use Maven plugin than to create it from the Eclipse menu.
For example, using maven-associated-plugin, you can write:

<!--https://maven.apache.org/plugins/maven-assembly-plugin/usage.html-->
  <plugin>
    <artifactId> maven-assembly-plugin</artifactId>
    <version> 3.2.0</version>
    <configuration>
      <descriptorRefs>
        <descriptorRef>jar-with-dependencies</descriptorRef>
      </descriptorRefs>
      <archive>
        <manifest>
          <mainClass>com.github.yukihane.App</mainClass>
        </manifest>
      </archive>
    </configuration>
    <executions>
      <execution>
        <id>make-assembly</id>
        <phase>package</phase>
        <goals>
          <goal>single>/goal>
        </goals>
      </execution>
    </executions>
  </plugin>

When the mvn package command is executed, files with -with-dependencies suffixes are generated at the same time as regular jar files.
This is executable-jar.

Up to Java 8, you could choose to install public JRE by installing it with the JDK/JRE installer, which you can download from the Oracle site.

Enter a description of the image here

If you had installed public JRE, you would have automatically configured the program to run on the JVM when you double-click the jar file.
In this case, the desired behavior is unnoticed.

Enter a description of the image here

You can also use launch4j to wrap the jar into an exe file so that it looks the same as the native executable.

<!--https://github.com/lukaszlenart/launch4j-maven-plugin/blob/master/src/main/resources/README.adoc-->
  <plugin>
    <groupId>com.akathist.maven.plugins.launch4j</groupId>
    <artifactId>launch4j-maven-plugin</artifactId>
    <executions>
      <execution>
        <id>l4j-gui</id>
        <phase>package</phase>
        <goals>
          <goal>launch4j</goal>
        </goals>
        <configuration>
          <headerType>gui</headerType>
          <outfile>target/myapp.exe</outfile>
          <jar>target/execjava-0.0.1-SNAPSHOT-jar-with-dependencies.jar</jar>
          <classPath>
            <mainClass>com.github.yukihane.App</mainClass>
          </classPath>
          <jre>
            <minVersion>1.8.0</minVersion>
          </jre>
        </configuration>
      </execution>
    </executions>
  </plugin>

This question was asked about two years ago, but I don't think this was a problem at that time.
Currently, however, the availability is limited.
This is because the current mainstream Java distribution no longer includes public JRE.

https://jdk.java.net/jpackage/

Early access (i.e., not yet official), but jpackage is also a product for this purpose.

The repository code at the beginning can be created with the following commands:

 mvn clean package
mkdir fatjar
mv.\target\execjava-0.0.1-SNAPSHOT-jar-with-dependencies.jar.\fatjar\
package --package-type app-image-d target-ifatjar-n myjpkg`
  --main-class com.github.yukihane.App`
  --main-jar execjava-0.0.1-SNAPSHOT-jar-with-dependencies.jar

target\myjpkgThe following files are for distribution:
myjpkg.exeDouble-click to run.

The public JRE method described above requires the deployment of public JRE in the environment of the running user.
On the other hand, since jpackage includes runtime, there is no such assumption.(Although the size of the handout will increase accordingly)

https://www.graalvm.org/

Native-image with the native-image command.

native-image-jar.\fatjar\execjava-0.0.1-SNAPSHOT-jar-with-dependencies.jar -- no-fallback

Unfortunately, the current version of 19.2.1 is not yet compatible with the Windows Swing application, but it may be included in the selection in the future.


2022-09-30 21:28

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.