简体   繁体   中英

How can I do Java/Kotlin to CLI?

I have a Java/Kotlin program, which gets arguments from String[] args , and I need to make it executable from everywhere from console, without prefixing it with java word. Like only the name of the program and its arguments. How can I do it? Like git or heroku:

name command

It depends on your operating system, but on Unix the following script would work:

#!/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}.jar "$@"
exit 1

You need to append this script at the start of your jar using cat, like the following

cat script.sh my.jar > my-program

And move my-program to some dir in your $PATH. After that, you'll be able to call my-program as usual program.

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