How to install Java application to my linux system

社会主义新天地 提交于 2019-12-02 07:54:14

问题


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


回答1:


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



回答2:


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.



来源:https://stackoverflow.com/questions/38852965/how-to-install-java-application-to-my-linux-system

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!