Reputation: 57
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.
Upvotes: 2
Views: 594
Reputation: 1
On Linux (specifically), you could use the /proc
filesystem (see proc(5) man page) and its binfmt_misc (actually the /proc/sys/fs/binfmt_misc/register
pseudo-file and other pseudofiles under /proc/sys/fs/binfmt_misc/
) to register java
as the handler for .class
or .jar
files. Read the Documentation/binfmt_misc.txt file in the kernel source for gory details.
Then any executable .jar
file would be interpreted by java
(or jexec
)
I'm not sure it is worth the effort. I find that wrapping your Java program in some shell script is much more easy (and more portable, because few Linux systems actually use binfmt_misc
, and your customer may need some sysadmin skills to enable it).
Upvotes: 0
Reputation: 500257
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.
Upvotes: 9
Reputation: 13890
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.
Upvotes: 2