How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file )

launch4j
jar to exe
how to change a jar file to an executable jar file
how to run java program with jar files in command prompt
how to create executable jar file in linux command line
how to convert class file to jar file using cmd
shell script to execute jar file with parameters
install jar command in linux

I have been searching for many similar posts about this but I still can't find my answer. I want to convert a .java program into a Linux executable file, without the .jar extension. How can I do it? I am trying to use Launch4j java wrapper, JWrapper, IzPack, making a .sh, making a .bat, running it using java -jar myFile.jar etc. but none of them worked. Some procedures are complicated and difficult to debug. Is there any straightforward way to convert a .java file or .jar file into a Linux executable file?

I need to pass this program as a Linux executable as a whole into another program that takes this program as an argument.

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
java_args=-Xmx1g
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 > helloworld.run && chmod +x helloworld.run 

That's all!

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.

Credits: Paolo Di Tommaso

Source: https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable

How to convert a .java or a .jar file into a Linux executable file, How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file ). Question. I have been searching for  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, h

I found a solution, which is exactly what I want after hours of searching and trying. It is to use the gcj command in linux.

It works like gcc for C and g++ for C++ which compile C or C++ programs into executables.

First install gcj by using the terminal by typing:

sudo apt-get install gcj-jdk

After that, cd into the directory of where the .jar file is located, let's say the .jar file is myFile.jar, then do:

gcj myFile.jar -o newNameofTheFile --main=theMainClassNameofTheFile

to compile the .jar file. And it should work like an executable by just running it in the command line like this:

./newNameofTheFile

Making Jar Files Executable, It is a good idea to test your jar file before turning it in to be graded! If possible, collect all your .java files and .class files (and anything else you want to include) together in a single directory. program or batch file means that you don't have your system configured properly to run Java. For a file extension, enter .jar. With Java programs it’s quite common to combine several classes into one JAR archive.Java libraries are typically distributed this way as well. On Linux platforms, people are quite used to using command line programs, but sometimes it’s handy to distribute a java program as an executable file that can be run by a simple double-click instead of opening a terminal and typing java -jar

What you can do is make a tiny little C-program, that calls the one of the exec functions (see man 3 exec) to the "java" binary, passing "-jar", "xxx.jar" as arguments, see also Forking a new process in C++ and executing a .jar file

How to Create and Execute a .Jar File in Linux Terminal, A JAR (Java ARchive) is platform-independent file format used to aggregate many Java class files and It also supports packaging for extensions. In application and bundle it into a JAR file, and demonstrate how to execute a .jar file from the Linux terminal. You can also subscribe without commenting. A file with the .JAR file extension is a Java Archive file used for storing Java programs and games in a single file. Some contain files that make them work as standalone apps and others hold program libraries for other programs to use.

Another simple trick to convert a jar to a Linux executable is just putting a shebang before the jar file.

Say we have a hello.jar file.

$ java -jar hello.jar
hello world!

You can create an executable in following steps.

$ echo "#! /usr/bin/env java -jar" > hello
$ cat hello.jar >> hello
$ chmod +x hello

Then, you will be able to execute the hello file directly.

$ ./hello
hello world!

Creating and Opening JAR Files Using the Java Programming , In a nutshell, these files contain JAVA applications in a package environment. Jar extension (.jar) is an archiving format designed to structure and compiled Java code can run on all platforms that support it without the need or change directory and inputting the directory where your java files are kept. jar. Java ARchive. This format is a Java archive and is presented as a typical ZIP file, in which part of the program is written in Java language. It was gradually replaced by new mobile platforms, since JAR is used for launching applications and games on early generation mobile phones.

Java jar command examples, How to use the jar command in JDK with various examples. The archive format is ZIP but the file name usually has .jar extension. Type jar command without any arguments to see its syntax and options, or see the A normal jar file is the non-executable one, such as a library jar file or an applet jar file. How to make an executable jar file in Java. The jar (Java Archive) tool of JDK provides the facility to create the executable jar file. An executable jar file calls the main method of the class if you double click it. To create the executable jar file, you need to create .mf file, also known as manifest file.

Java Runtime Environment/How to run java applications , Many Java applications come packaged as a file with the ".jar" extension This means you can simply double-click the file on many operating systems. Some Java applications (for example, mkgmap.jar) cannot run without terminals. note that the opengl option can cause problems with window managers on Linux. jsf_VWP5.5.1 wrote: Hi, How to convert .class into .jar file From a command prompt, cd to the location of your .class file(s). If you want to create a simple jar, use: jar -cf Whatever.jar Whatever.class If you want to compile all .class files in a directory into a jar, use *.class instead.

How to convert a .java or a .jar file into a Linux executable file, How to convert a .java or a .jar file into a Linux executable file ( without a .jar extension, which means it's not a .jar file ) - find answer. want convert .java program linux executable file, without .jar extension. how can it? trying use launch4j java  how do you convert a jar file into a java file ? I am new to Java ,but have a little experience in C++ and Visual Basic. I want to edit and maybe create my own mobile games that are written or converted into jar files. At the moment I am using Java NetBeans , and Easy Java( the java pad).

Comments
  • why you wanna make .sh file for linux only? i guess the beauty of java is cross platform you just need to install JRE and one single jar will run perfectly OK on any OS.
  • I have tried making a .sh file but it doesn't work. The reason is because I have a main program that is fixed and written by someone else and I can only pass in my program as a Linux executable (not a .jar) to it. And I am developing it using Java.
  • @pythonhiew : i guess you can also run jar using command like, java -jar fileName.jar. why don't you make a bash which run command and up your JAR.
  • I don't see why .sh script would be any different than "linux executable", it's executable as well. Large portion of linux executables are actually scripts - if that doesn't work then I don't see how any other executable would work either. Also: Launch4j is for creating native windows executables, I don't see how that would help you in Linux environment.
  • Make sure your .sh file is really executable (chmod +x yourfile.sh), maybe that's the problem. If the tool you are passing the shell script to doesn't like the .sh ending, just rename it omitting the .sh ending.
  • What is $java_args ?
  • It could be any JVM arg, like -Xmx, -Xms, etc... Example updated.
  • What is the point of this shell wrapper if the user still needs to have JRE installed on his machine. We need something like pyinstaller for java
  • Hi @pouya, the point is avoid to need to prefix every call to a java program (JAR file) with "java -jar your.jar", so you can just put your executable script on the system path and call it as any normal command/binary from anywhere. In particular, many domestic users don't understand what is a JAR file, and in my option, they don't need to use a software wrote in Java.
  • gcj has been considered obsolete for years.
  • Yeah chrylis however it works after all the methods I tried. Like running it with java -jar myFile.jar etc. or using .bat, .sh, all failed. Yet this works successfully. Because I have to pass this compiled program into another program as an argument and the other program does not take any form of argument, namely .jar, .sh, .bat, except after what is compiled by gcj.