I know how to convert "jar" to windows executable file(.exe). But I want to know how to convert "jar" to Linux executable file(.?). I have searched google but didn't get exact answer what i want, help to do this.
-
What do you mean by "Linux execrable file?" – Michael Markidis Jun 08 '17 at 05:38
-
You can execute the `jar` file by shell command just like `.sh` file in linux or `.bat` file in windows. – dabaicai Jun 08 '17 at 05:39
-
I know how to run "jar" file in Linux, But I need to convert it to (.sh) linux application file. Like (.exe) file in windows – lekshmi Jun 08 '17 at 05:41
-
Have you tried: `sudo chmod +x myFile.jar` – Abhishek Agarwal Jun 08 '17 at 05:45
-
Why not just use a script (.sh) that triggers the jar-file, if you have to mask the application? Why can't you use the `java -jar
` command anyway? – vegaasen Jun 08 '17 at 05:46 -
I have to hide "jar" file from the user – lekshmi Jun 08 '17 at 05:55
-
Possible duplicate of [How to make an executable jar file?](https://stackoverflow.com/questions/5258159/how-to-make-an-executable-jar-file) – Rahul Sharma Jun 08 '17 at 06:19
-
I have got exact answer from the following Link [Converted Jar file to Linux executable file using gcj](https://stackoverflow.com/questions/29429976/how-to-convert-a-java-or-a-jar-file-into-a-linux-executable-file-without-a) – lekshmi Jun 12 '17 at 07:35
-
As of Java 14, the JDK comes with [jpackage](https://docs.oracle.com/en/java/javase/14/jpackage/basic-packaging.html). See also the answers [here](https://stackoverflow.com/questions/147181/how-can-i-convert-my-java-program-to-an-exe-file). – Matthias Braun Jan 11 '22 at 09:36
6 Answers
I want to know how to convert "jar" to Linux executable file(.?).
Linux does not have executable files the same way that Windows does. In Linux we have binaries and scripts. Scripts are ran with an interpreter; languages like Ruby and Python. Binaries are files of compiled code, they can be libraries or entire programs. Both binaries and scripts can be executable.
To make a program executable in Linux, type this into the command line.
$ chmod +x myProgram
Alternatively you can open file preferences and set executable in the permissions section.
Since Linux does not have .exe
files or an analogue, we'll have to work something else out. Linux and other Unix like Operating system have a shell called bash; often called the command line or terminal in reference to Linux and Mac. We want to create a file that can be run as our entire program, instead of having to call $ java -jar myProgram.jar
. To tell bash to start a script environment for a file we use a hashbang. This is the first line of the file which instructs bash were to look for the interpreter to send the rest of the file to. For a bash script, like Batch Script on Windows, we would start the file with #!/bin/bash
. The path after the #!
(hashbang) tells bash were to look for the interpreter. For a .jar
make the hashbang #!/usr/bin/java -jar
and then cat the .jar
to the file with the hashbang. This can be done all from the terminal in Linux.
Create a file with the java jar hashbang.
$ echo '#!/usr/bin/java -jar' > myBin
We have written the hashbang as a string to the new file myBin.
Write the jar to the file.
$ cat my.jar >> myBin
The >>
appends the jar to the receiving file.
This will create a file that has the bash hashbang and the jar appended to it. Next set myBin
to executable and try to run the program.
$ chmod +x myBin
$ ./myBin

- 3,726
- 2
- 33
- 47

- 2,044
- 2
- 14
- 30
Create a sh wrapper file with following content and make it executable:
#!/bin/bash
java -jar <your-jar>
Optionally add some vm arguments.
Perhaps there are some tools that could generate such a file, but is is little effort to do it manually I reckon.

- 624
- 4
- 13
-
Though the top answer is a little more thorough, it's also somewhat pedantic, correspondingly confusing to some and quite off-topic. This answer is the most accurate and immediately useful. I would only clarify by enhancing the actual command thus: "java -jar
$*" That allows the script to work from any generic bin directory and conveys any arguments to the JAR for specific handling. – Russ Bateman Oct 23 '20 at 17:37
You can always run a jar file by doing java -jar myFile.jar
.
However, to make the jar file itself executable, you need to set the executable bit, as the message hints. chmod +x /path/to/your/file/myFile.jar
will accomplish this.
After that you can do ./myFile.jar
to run it.
man chmod
will provide you with information about how chmod works.
Source: How can I make a .jar file executable? on AskUbuntu. Answer by Gary

- 1,190
- 2
- 19
- 38
-
2It doesn't work, at least on Ubuntu. Gives error like `cannot execute binary file: Exec format error` even if given the execute permission. – MasterMind Aug 21 '17 at 09:12
While making a shell script works, it sort of seems a bit like overkill for most situations. Why not just use an alias and throw it in your .bashrc
or .bash_aliases
file? Like so:
alias whatever='java -jar /path/to/whatever.jar'
(Since my solution didn't come up as an answer here, my "why" is a somewhat genuine question: Is there some hidden hazard or feature I'm missing? Or is it more of a matter of adhering to the strictest interpretation of the OP's question?)

- 775
- 11
- 26
Probably what is more apt and exact in this case is to create a Desktop Configuration File.

- 34
- 2
- 8