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 )

前端 未结 4 2233
醉酒成梦
醉酒成梦 2020-12-15 13:42

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 e

相关标签:
4条回答
  • 2020-12-15 14:15

    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!
    
    0 讨论(0)
  • 2020-12-15 14:19

    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

    0 讨论(0)
  • 2020-12-15 14:19

    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
    
    0 讨论(0)
  • 2020-12-15 14:24

    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

    0 讨论(0)
提交回复
热议问题