Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you run a Java program in Linux using the name only, without the "java" command?

If I am writing HelloWorld, is there a way I can run the program from any directory by just typing HelloWorld? Sort of the same way once you set up Ant, you can just run Ant from any directory?

Just for some details, we are creating a CLI based toolkit for a customer, and just curious if we can compile it, install it, and just have them run it using the toolkit name.

like image 819
blockHead Avatar asked Mar 27 '26 20:03

blockHead


2 Answers

You can always create a shell script, call it HelloWorld and make it run java with your JAR.

You'll then need to chmod the script to make it executable, and place it somewhere in your $PATH.

The script would like something like:

#!/bin/bash
cd /path/to/helloworld
java -jar HelloWorld.jar "$@"

or

#!/bin/bash
java -jar /path/to/helloworld/HelloWorld.jar "$@"

depending on your exact requirements.

like image 166
NPE Avatar answered Mar 29 '26 08:03

NPE


Common solution for your problem is to create a separate launcher application, which is non-java application that runs your Java program. Launcher can be written in some compilable language such as C/C++ and compiled into native executable. Also it can be written in some interpreted language such as Unix shell, perl, python etc and made executable by adding #!/path/to/interpreter line at the beginning of launcher file and setting executable flag on it. Also there are several utilities that can generate launcher for your program such as launch4j or jsmooth.

like image 30
Mikhail Vladimirov Avatar answered Mar 29 '26 08:03

Mikhail Vladimirov