How can I make executable file from jar with embedded JVM? - java

For example, I have someFile.jar. I can run it simply using java -jar someFile.jar.
But I want to create some files (for example .deb file) to install this jar to another machine. This machine may not have jre15 which is required for running this jar. So I want to create some executable file that will contain jar and JVM
I tried to use jpackage --name testName --input . --main-jar someFile.jar --linux-shortcut. This command generates `.deb file but it won't create terminal command testName (to run this jar) but it creates a desktop app that I can run as an application but no one terminal command created. So, how can I create a deb file from the jar with embedded JVM to be able to run it as a command from terminal?

Investigate the content of the deb, or investigate the filesystem once the package is installed. Per default you should find it in /opt/. Underneath that you find a bin directory with only one file. That is the one you can invoke from command line.
JPackage also creates a launcher, which resides in /opt//lib/*.desktop. Check that text and you see that the executable is just the file in the bin directory, which means the GUI will run that exact command when the icon is clicked.

Related

starting jar with VM-options

I have a runnable jar-file which I want to start from a batch file. However the jar-file has to be started with a VM-option. The following batch file starts the jar file (in a static way).
java -Djava.security.policy=C:\Users\uname\
\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://C:/Users/uname/Documents/Folder
/anotherFolder/target/classes/ -jar %~dp0jarfile.jar %*
pause
btw: I know that
\src\main\java\rmi\client.policy
is not in a jar file yet, but I'm assuming that everybody has this file structure already on his machine.
However, I want to be able to start the jar file with a relative path, so that every Windows10 (x64) user can use my jar file system-independently. How to achieve that via batch?
Replace each reference to user home C:\Users\... with %userprofile% variable as per this answer explanation.
java -Djava.security.policy=%userprofile%\src\main\java\rmi\client.policy
-Djava.rmi.server.codebase=file://%userprofile%/Documents/Folder/anotherFolder/target/classes/
-jar %~dp0teamFour-1.0-SNAPSHOT.jar %*
or switch to %userprofile% directory with cd before executing java and depend on relative paths.

.jar file is not working after being published from Eclipse [duplicate]

There was a program that I used that made runnable .jar files.. All the ones I'm finding now are ones that make .exe files.. I remember it also has the option to make the file a .sh script as well. Anyone knows its name? I've been searching for hours with no avail :/
The command line
java -jar file.jar
Will run your jar file if it has a Main-Class defined as explained here.
You can use that command in a shell script.
You can create a runnable jar using NetBeans IDE or Eclipse IDE by just providing the main class to run. Rest of the things it will take automatically. That class must be having a main() method in it. Then you can run that jar file using java -jar yourjarfile.jar
Do you mean actually coding java and then compiling to .jar? If you do try
eclipse code editor
I used eclipse to make minecraft mods. It will work if you want to make .jar programs.
If you want to have a jar that you can execute using the usual syntax ./app.jar (instead of java -jar), here is a post explaining the process: how to create executable jars.
Basically, JAR is a variant of ZIP, which allows random bytes to be pre/appended to the JAR without corrrupting it. This means it is possible to prepend a launcher script at the beginning of the jar to make it "executable".
Here is a simple example of the process:
# Append a basic launcher script to the jar
cat \
<(echo '#!/bin/sh')\
<(echo 'exec java -jar $0 "$#"')\
<(echo 'exit 0')\
original.jar > executable.jar
# Make the new jar executable
chmod +x executable.jar
With this, you can now run ./executable.jar instead of java -jar original.jar. This works on all unix like systems including Linux, MacOS, Cygwin, and Windows Linux subsystem.

How do I configure install4j to run a java executable jar file which contains other jar files?

I have java an executable jar file that has many other jar files embedded in it. It uses simon tuffs onejar to accomplish this. The jar runs correctly when I double-click it, or issue 'java -jar myApp.jar' from the command prompt. The jar file has a manifest.mf containing:
Manifest-Version: 1.0
Main-Class: com.simontuffs.onejar.Boot
One-Jar-Expand: expand,doc
Install4j correctly copies this jar file to my bin folder, but the 'myApp.exe' that it creates fails to launch. I get:
java.lang.ClassNotFoundException: com.ndi.foreCee.programs.MyApp
or:
java.lang.ClassNotFoundException: com.simontuffs.onejar.Boot
depending on what I plug into install4j's Configure Java invocation page 'Main class'. I've set 'Class path:' to 'Archive bin\myApp.jar'.
Can anyone tell how to configure install4j to build an exe that just issues 'java -jar myApp.jar', using the embedded jre?
Add a "Run executable or batch file" action to the "Installation screen" and set the following properties:
"Executable" property: ${installer:sys.javaHome}/bin/java
"Arguments" property: -jar; filename.jar (in the editor one argument per line)
Install4j : How to execute command line java -jar filename.jar at the end of installation
Can anyone tell how to configure install4j to build an exe that just >issues 'java -jar myApp.jar', using the embedded jre?
The launcher generated by install4j does not execute javaat all, it uses JNI to create the JVM.
You can start the executable from the command line with the parameter /create-i4j-log to get a log file that shows the parameters that are passed to the JVM.
I solved my problem by updating my ant build and removing simon tuffs one-jar and replacing it with:
as described in Including external jar-files in a new jar-file build with Ant
. After that, the Install4J launcher worked as advertised (and as M2E67 described).

Bash script run jar with external files included does not work

I have a jar file that I want to launch from a bash script. This jar includes references to an external folder that contains images.
When I am running the jar from command line with the absolute path to the jar, all works OK. The problem appears when I run it from a bash script. Apparently the folder that contains the images is not found.
Launching from command line:
java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar
Bash script:
#! /bin/bash -x
cmd="java -Djava.library.path=/opt/opencv/build/lib -Xmx1g -jar /home/version4/Podo.jar"
eval $cmd
The directory where are my images are is:
/home/version4/img
The Java code for accesing the images:
String img_header="./img/HEADER.png";
String img_body="./img/BODY.png";
BufferedImage header,body;
header=ImageIO.read(new File(img_header));
body=ImageIO.read(new File(img_body));
The output error:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
Can anyone tell me what I am doing wrong? Thank you.
You should run your bash script from the same directory as you run your jar file. Still, it can be located in another place, the only meaning detail is your working directory. The better way you have is to store the images inside jar, see Getting a BufferedImage as a resource so it will work in JAR file
In any case, you should not use absolute paths to retrieve the images as this solution just destroys any portability of your jar.
Try giving the absolute path of the image (in your java code) if you are running the bash script from a location other than your jar location.

Writing a Unix installer for a Jar file

I have written a Java application, and I have created an executable Jar file that successfully runs my application with java -jar myJar.jar. I have an executable shell script called launchMyProgram that wraps the launching of this Jar (and offers various flags like --help etc.). My target output directory looks like this:
$ ls /path/to/MyProject/target/
archive-tmp/ classes/ myJar.jar
What is the standard method for me to write an installer for my Unix-only application? I assume that I would be correct to drop the launchMyProgram executable in /usr/local/bin. But where do I put the Jar file? Should I make my own subdirectory somewhere for my program's files? Do I need to copy the classes directory from the above output into the same directory as the Jar? This will run via a Makefile so of course users may override my choices.
Basically, I want a user to be able to run make && make install, and be able to run my application with launchMyProgram, and I want place my files (one jar, a 'classes' folder, and a shell script) in the most typical places possible.
One of the best ways to do it has been reinvented many times but is unfortunately not yet standard.
Since a JAR is a ZIP file which is allowed to have an arbitrary prefix, you can prepend a launcher shell script to your jar, mark it executable, and treat that as a standalone binary.
$ echo '#!/bin/bash' > launchMyProgram
$ echo 'exec java -cp "${0}" com.example.program.Main "${#}"' >> launchMyProgram
$ cat myJar.jar >> launchMyProgram
$ chmod +x ./launchMyProgram
$ ./launchMyProgram
Hello, world!
See Simple & Easy Executable Jars for more details.
You should be able to pack everything in the classes/ folder into your jar and still have things work.
Also, if you want to provide RPMs or DEB packages or something for users, fpm makes that really easy.

Categories

Resources