简体   繁体   中英

How to install Java application to my linux system

I have written a Java application that analyzes my phone bills and calculates the average. At the moment I execute it like:

$ java -jar analyze.jar bill_1.pdf bill_2.pdf

But I want to install the application to my system. So I can just fire up a terminal type in the name of the application and the arguments and hit enter. Like any other "normal" program.

$ analyze bill_1.pdf bill_2.pdf bill_3.pdf

I know I can write a shell script and install it to "/usr/bin/" but I can't believe that there is no "native" way.

So please help, sorry for dump question.

Thank's in advance

What you did so far is basically "the native" way.

You have to keep in mind: Java applications are compiled to byte code . There simply is no binary for your application that you could invoke. You do need this detour of calling some JVM installation with a pointer to the main class you want to run. In owhther words; that is what the vast majority of java applications are doing.

Theoretically, there are products out that there that you could use to actually create a "true" binary from your application; but that isn't an easy path (see here for first starters); and given your statement that your just looking for more "convenience" it is definitely inappropriate here.

One neat little trick is that you can append a basic shell script to the start of the jar file which will run it appropriately. See here for the full example but the basics are:

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

Then do...

cat stub.sh helloworld.jar > hello.run && chmod +x helloworld.run 

And you should be all set! Now you can just call the script-ified jar directly.

./helloworld.run

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM