The provided answers describe how to build a jar that can be executed using java -jar command. The following article explains how to take such an 'executable' jar and convert it to a 'program' file executable by sh (so it can be invoked with ./program and not with java -jar program.jar): https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
The idea is that a zip archive (and a jar is basically a flavor of a zip archive) may have an arbitrary prologue. The prologue is everything from the beginning of a file till zip file signature (4 characters, first two of which are 'PK'). When java reads a jar archive, it skips that prologue (everything before the signature).
So from the point of view of java, such a jar will be a valid jar. But if that prologue is a shell script, such a jar will also be a file that you can execute using a shell interpreter.
That means that you can prepend usual jar content with a shell script that will just invoke that java -jar with that same jar archive as an argument. And java will happily execute that jar ignoring the prologue.
The prologue in the article looks like this:
#!/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
It looks for java and starts it with the needed parameters. This is just an example, you can tune the algorithm used to find java command to make it suit your needs.
And the resulting file will consist of the concatenation of this launcher shell script and that jar file (containing main class and manifest) that should be run using java -jar.
Videos
The provided answers describe how to build a jar that can be executed using java -jar command. The following article explains how to take such an 'executable' jar and convert it to a 'program' file executable by sh (so it can be invoked with ./program and not with java -jar program.jar): https://coderwall.com/p/ssuaxa/how-to-make-a-jar-file-linux-executable
The idea is that a zip archive (and a jar is basically a flavor of a zip archive) may have an arbitrary prologue. The prologue is everything from the beginning of a file till zip file signature (4 characters, first two of which are 'PK'). When java reads a jar archive, it skips that prologue (everything before the signature).
So from the point of view of java, such a jar will be a valid jar. But if that prologue is a shell script, such a jar will also be a file that you can execute using a shell interpreter.
That means that you can prepend usual jar content with a shell script that will just invoke that java -jar with that same jar archive as an argument. And java will happily execute that jar ignoring the prologue.
The prologue in the article looks like this:
#!/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
It looks for java and starts it with the needed parameters. This is just an example, you can tune the algorithm used to find java command to make it suit your needs.
And the resulting file will consist of the concatenation of this launcher shell script and that jar file (containing main class and manifest) that should be run using java -jar.
I think your better option is to rewrite the other main method of the existing program to accept CLI args, but if you want to do so, either way, you need to compile the program to a class or jar file
And you run it like any other Java program from the CLI, but give it arguments like -h here. Note: you'll need to include a classpath for Apache Commons here.
java - cp <classpath> fully.qualified.CommandLineParameters -h
Or, if you have a JAR with a Main class attribute in its manifest and also includes Apache Commons (via an uber jar), there's no need to set the class name or classpath. Still, give the arguments, though
java -jar jarFile.jar -h
I want it to be an independent programme, accessed from the command line, such as
ls
Open your bashrc and put in an alias or function. The fact of the matter is, you must invoke java, or you can use a different JVM scripting language like Groovy
$ cat my-ls
#!/usr/bin/env groovy
println "Run my other program"
$ chmod +x my-ls
$ ./my-ls
Run my other program
I'm working on a Java application that requires a command-line interface to allow users to interact with it easily. I want to implement a system that can read user input, process commands, and respond accordingly. My goal is to create a simple yet effective CLI that can handle basic commands like 'start', 'stop', and 'status'. I've looked into using the Scanner class to read input from the console, but I'm unsure how to structure my code for command processing and error handling. Specifically, I want to know how to design a loop that continues to accept commands until the user chooses to exit the program. Additionally, any tips on organizing my code for better readability and maintainability would be highly appreciated.