- How to make a JAR file Linux executable
- Extracting a bin file and a tar file
- How to Convert a Java File to an Executable? (Windows, macOS, Linux)
- Creating A Project On IntelliJ IDEA
- 1. Installation Setup
- Windows Install
- Linux Install
- Mac Install
- 2. Creating a Project Out of the Java File
- Building Java Applications
- 1. Converting a Java File to JAR
- 2. Convert JAR to Windows exe
- 3. Convert JAR to macOS Executable
- 4. Convert JAR to Linux Executable
- Conclusion
How to make a JAR file Linux executable
Every Java programmer knows — or should known — that it is possible to create a runnable Java package archive (JAR), so that in order to launch an application it is enough to specify the jar file name on the Java interpreter command line along with the -jar parameter. For example:
There are plenty of tutorials showing how to implement this feature using Ant, Maven, Eclipse, Netbens, etc.
Anyway in its basic form, it just requires to add a MANIFEST.MF file to the jar package. The manifest must contain an entry Main-Class that specifies which is the class defining the main method for your application. For example:
$ javac HelloWorld.java $ echo Main-Class: HelloWorld > MANIFEST.MF $ jar -cvmf MANIFEST.MF helloworld.jar HelloWorld.class
But this still requires your users to invoke the Java interpreter with the -jar option. There are many reasons why it would be preferable to have your app runnable by simply invoking it on the terminal shell like any other command.
Here comes the protip!
This technique it is based on the ability to append a generic binary payload to a Linux shell script. Read more about this here: http://www.linuxjournal.com/content/add-binary-payload-your-shell-scripts
Taking advantage of this possibility the trick is just to embed a runnable jar file into a Bash script file. The script when executed will launch the Java interpreter specifying itself as the jar to run. Too complex? Much more easier to do in practice, than to explain!
Let’s say that you have a runnable jar named helloworld.jar
Copy the Bash script below to a file named stub.sh
#!/bin/sh MYSELF=`which "$0" 2>/dev/null` [ $? -gt 0 -a -f "$0" ] && MYSELF="./$0" java=java if test -n "$JAVA_HOME"; then java="$JAVA_HOME/bin/java" fi exec "$java" $java_args -jar $MYSELF "$@" exit 1
Than append the jar file to the saved script and grant the execute permission to the file resulting with the following command:
cat stub.sh helloworld.jar > hello.run && chmod +x helloworld.run
Now you can execute the app just typing helloworld.run on your shell terminal.
The script is smart enough to pass any command line parameters to the Java application transparently. Cool! Isn’t it ?!
In the case your are a Windows guy, obviously this will not work (except you will run a Linux compatibility layer like Cygwin).
Extracting a bin file and a tar file
posted 13 years ago
What is the difference between extracting a bin file and a tar.gz file? For example when I downloaded java, I got them as binaries and I user a sh ./java.xxx.bin to unpack the contents. When I downloaded Maven, it was of the tar.gz form and used a tar utility to unpack the contents. What exactly is the difference between these two formats?
SCJP 1.4, SCWCD 1.4 — Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
author and jackaroo
posted 13 years ago
Precise differences are unlikely to be forthcoming, as there is no precise definition for a «bin» format file (at least in the way that Sun are using it here — there are other «bin» files that have more precise definitions).
The tar format (tape archive) just provides a way of storing one or more files and directories into a single archive file (or device). That is all it allows — it does not specify licensing terms or anything else that you might typically find in a distribution.
For something like Maven, this is fine. The license is contained as a file within the archive, and you can read it at your leisure anytime after installing it.
For the Java distribution provided by Sun, this is not the case. They want you to explicitly look at the license agreement and accept it before you can install it. In theory, some software being distributed might also need to have a special user or group installed on your computer, and/or install daemons.
To meet these needs, it makes sense to have a file that can work on any Linux system, regardless of what has already been installed. In the Microsoft world, this might be considered an executable file (*.exe or *.msi) that contains the distribution within itself.
So that really is all the bin file is — an archive (possibly a tar archive) that is contained within an executable that knows how to process it and potentially do other work as well.
As an example, consider the following file:
(You can run this without any problems on your computer — it doesn’t actually do anything other than list the contents of a tar file that is contained within lines 5 — 7. If you want to try it, just copy all 8 lines into a file and name it something like «example.sh». Then run «sh ./example.sh» — you should see output showing you the contents of my file).
If that was all I was doing, I could have just provided you with the tar file directly. However I wanted you to press «Enter» first, and that is what lines 1 & 2 get you to do.
Hopefully you can see from this simple example how this can be used to create a distribution that can easily be sent to a customer that can do more than just provide the raw distribution. In theory the archive contained in lines 5 — 7 could contain something that my script could have executed after extracting it.
(If you really want to see what is in that archive, change the «tzvf» in line 4 to an «xzvf» (where the ‘x’ is for extract) and re-run it. This will create the «txt» file in the same directory where you run the script)
The Sun Certified Java Developer Exam with J2SE 5: paper version from Amazon, PDF from Apress, Online reference: Books 24×7 Personal blog
How to Convert a Java File to an Executable? (Windows, macOS, Linux)
Java distributions can be a significant pain to figure out — especially when you have no clue how these files work on the backend of your native operating system. While there are multiple ways to package Java files, most are too arduous for beginners.
Now, you could pay someone to do it on your behalf, but that’s no fun — especially when you’re a programmer.
Since converting java files to executables isn’t a one-step process, you’d have to delve deep into the technical stuff to understand it truly.
With that out of the way, the best method of converting Java files into an executable is by using the built-in functionality of an IDE like IntelliJ IDEA. While this shaves off some technical flexibility, it’s plenty for beginners and regulars.
Hence, it’s recommended you download and configure the IntelliJ IDEA by creating and opening your project from there. This IDE is available on Windows, macOS, and Linux, thereby making it perfect for our use case:
Operating System | IDEA Source |
Windows | Download |
macOS | Download |
Linux | Download |
This article will primarily focus on creating executable JAR files for your specific operating systems. For simplicity’s sake, we will use built-in IntelliJ IDEA methods to achieve this goal.
Table of Contents
Creating A Project On IntelliJ IDEA
1. Installation Setup
Once you’ve downloaded the above file, it’s finally time to install it and give the IDEA a go.
Windows Install
Here’s what you need to do for Windows :
- For starters, click on the setup and press Yes to the Windows Prompt.
- Next, select your settings, and don’t forget to add IntelliJ IDEA to the path . Here’s what it’ll look like:
- Add the bin folder to the PATH and hit Next > .
- Now, press Install, and wait for the process to finish.
Linux Install
The installation steps you’d need to take will differ vastly if you’re on Linux. Luckily, we have those listed for you down below:
- Once the directory has been changed, enter the following command to extract the installation file from the downloads directory.
- Replace the with the actual path of the downloaded installation file. For example, an IDEA file stored in the Downloads folder will look something like this:
sudo tar -xvzf ~/Downloads/ideaIU-2020.3.tar.gz
Mac Install
The installation process on macOS is pretty simple compared to the rest. Just drag the .dmg file from the Downloads Folder to the Applications Folder, and you’re good to go!
2. Creating a Project Out of the Java File
Once you’ve downloaded and opened IntelliJ IDEA, it’s time to create a new project, so you can load all your files up to convert them to an executable later on.
With that said, follow the steps below for all platforms:
- Launch IntelliJ IDEA, and choose New Project in the Welcome Screen. If it doesn’t show up for you, select File > New > Project instead.
- Next, wait for the New Project menu to open up and choose your configurations. You can also select a language that does not come packaged with IDEA.
- Now, select the Build System , and choose a JDK from the list. If you don’t have one, click the download button.
Once the project has been built, open its path and add your code files as needed. This will work on Windows, macOS, and Linux since IntelliJ IDEA is the same on all platforms.
Building Java Applications
1. Converting a Java File to JAR
Once your project is ready and running, it’s time to export it into the JAR config, which can be later processed into a .exe file.
With that said, here’s how you can do it:
- Lastly, select PATH , and any optional Class , and extract it to the target JAR .
Doing so will make a JAR file that can not only help you convert regular JAVA files to .exe, but also the JAVA Class ones.
2. Convert JAR to Windows exe
You will need to download an application called Launch4J to make this work. Just click on this link , and press the download button.
Once the application has been downloaded, follow the below steps:
- To start, open Launch4J and select the output file location.
- Next, enter the PATH to your JAR file and fill the rest with your preferred configurations.
- Now, click on the Gear button, and name your file to click save.
That’s it! Just open the new exe file you’ve created, and you’re pretty much done.
3. Convert JAR to macOS Executable
Just like Windows, you’d need the aid of some third-party packages to make this work.
Here are all the steps you need to follow:
cd jar2app sudo ./install.sh /usr/local /usr/local/jar2app ~/Downloads/pathg.jar // replace jar path with your jar
4. Convert JAR to Linux Executable
Unlike Windows, Linux does not feature executable files. Rather, you get files that are just a bunch of compiled code or scripts run with interpreters’ help.
To make JARs work on Linux, you’d have to create a bunch of hashbang commands.
$ echo '#!/usr/bin/java -jar' > myBin
- Now that we have written the hashbang as a string to the new file myBin. It’s time to write the jar to the file:
- Lastly, set the myBin file to an executable by using the following commands and run the program:
Conclusion
To sum up, in this article, we showed you how to convert a Java file to an executable on Windows, macOS, and Linux.
Essentially, it would help if you could convert your JAVA project into a JAR file. Once that’s done, it’s about issuing the commands in their respective operating systems. As mentioned, Linux doesn’t have executables, so you’d be creating a gimmick instead.
Luckily, the process is pretty simple once you get the hang of it since most of the complex stuff is pre-handled by IntelliJ IDEA. Alternatively, you could try your luck at Eclipse if IntelliJ IDEA is too much for you.
Save the resultant JAR file in an accessible PATH, so you won’t have to find it later.
If the steps mentioned above cause an error, try rebooting your device or computer or perform a clean reinstall of the error-causing software.
Lastly, let us know in the comments:
- Were you able to create a JAR file from your JAVA project?
- Have you been able to find a more straightforward be-all-end-all fix?
- Which kind of software are you trying to make?
- Are there any other points or helpful information you believe we should’ve mentioned?
Feel free to share this article amongst your friends and peers, so they don’t have to rack their brains to convert JAR or Java files into executables.