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.

Answer from Roman Puchkovskiy on Stack Overflow
🌐
Medium
medium.com › @rostyslav.ivankiv › build-a-simple-java-cli-app-in-minutes-with-picocli-e606f8a2bce1
Build a simple Java CLI app in minutes with Picocli | by Rostyslav Ivankiv | Medium
April 22, 2023 - We’ll guide you through the process of building a simple Java CLI app using Picocli[1] and creating native image using GraalVM[2]. ... The example consist of 2 simple files: pom.xmland Main.java.
Top answer
1 of 4
7

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.

2 of 4
1

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
🌐
Picocli
picocli.info
picocli - a mighty tiny command line interface
Before we run our CheckSum application, let’s create an example file whose checksum we want to print. For example: ... Now, assuming we created a jar named checksum.jar containing our compiled CheckSum.class, we can run the application with java -cp <classpath> <MainClass> [OPTIONS]. For example:
🌐
Baeldung
baeldung.com › home › java › core java › command-line arguments in java
Command-Line Arguments in Java | Baeldung
December 27, 2025 - First, we need to right-click on the main method, then choose Run ‘CliExample.main()’: This will run our program, but it will also add it to the Run list for further configuration. So, then to configure arguments, we should choose Run > Edit Configurations… and edit the Program arguments textbox: After that, we should hit OK and rerun our application, for example with the run button in the toolbar.
🌐
DZone
dzone.com › coding › java › java command-line interfaces (part 1): apache commons cli
Java Command-Line Interfaces (Part 1): Apache Commons CLI
June 22, 2017 - In this post, I look at one of the best known of these Java command line parsing libraries: Apache Commons CLI. I have blogged on Apache Commons CLI before, but that post is over eight years old and describes Apache Commons CLI 1.1. Two classes that I demonstrated in that post, GnuParser and PosixParser, have since been deprecated. The examples in this current post are based on Apache Commons CLI 1.4 and use the newer DefaultParser that was introduced with CLI 1.3 to replace GnuParser and PosixParser.
🌐
GitHub
github.com › maciejwalkowiak › java-cli-project-template
GitHub - maciejwalkowiak/java-cli-project-template: Project template for bootstrapping Java & Picocli based CLI · GitHub
Project template for bootstrapping Java & Picocli based CLI - maciejwalkowiak/java-cli-project-template
Starred by 75 users
Forked by 5 users
Languages   Java
Find elsewhere
🌐
Atextor
atextor.de › 2020 › 07 › 27 › building-a-decent-java-cli.html
Building a decent Java CLI - Andreas Textor
If you need a complete text-based user interface with input forms, buttons, checkboxes etc., you can check out lanterna, which is similar to ncurses, except it’s written in Java and requires no native libraries. Let’s be blunt: When you have written a cool new CLI tool called foo, a user wants to run foo --help, not java -jar ~/somewhere/foo-1.0.jar --help.
🌐
Oracle
docs.oracle.com › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The following example shows how a user might run Echo. User input is in italics. ... Note that the application displays each word — Drink, Hot, and Java — on a line by itself. This is because the space character separates command-line arguments.
🌐
javaspring
javaspring.net › blog › java-cli
Mastering Java CLI: A Comprehensive Guide — javaspring.net
For example, to set the maximum heap size to 512MB: ... The classpath is a list of directories and JAR files that the JVM uses to locate classes. You can set the classpath using the -cp or -classpath option. java -cp path/to/classes:path/to/library.jar MyJavaProgram · When building a Java CLI application, you often need to parse the command-line arguments to perform different actions.
🌐
Opensource.com
opensource.com › article › 21 › 8 › java-commons-cli
Parse command options in Java with commons-cli | Opensource.com
August 13, 2021 - To use the commons-cli library in your code, you must import it. For this simple option parsing example, you can populate a file called Main.java with the standard minimal code:
🌐
Reddit
reddit.com › r/javahelp › how can i implement a simple command-line interface (cli) for my java application?
r/javahelp on Reddit: How can I implement a simple command-line interface (CLI) for my Java application?
December 16, 2025 -

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.

🌐
GitHub
gist.github.com › nickname55 › 880addec70a8303b2359680376d5d066
apache commons cli example · GitHub
apache commons cli example · Raw · Application.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
TutorialsPoint
tutorialspoint.com › commons_cli › commons_cli_usage_example.htm
Apache Commons CLI - Usage Example
java CLITester usage: CLITester [-g] [-n <arg>] [-p] options.addOption("p", "print", false, "Send print request to printer.") .addOption("g", "gui", false, "Show GUI Application") .addOption("n", true, "No. of copies to print"); System.out.println("usage: CLITester " + formatter.toSyntaxOptions(options)); In this example, we're printing usage using HelpFormatter if no argument is passed.
🌐
Zignar
zignar.net › 2022 › 05 › 14 › can-you-write-a-cli-using-java
Can you write a CLI using Java? - zignar.net
May 14, 2022 - Benchmark 1: java -jar app/build/libs/app.jar Time (mean ± σ): 92.0 ms ± 13.4 ms [User: 85.7 ms, System: 19.1 ms] Range (min … max): 63.6 ms … 108.2 ms 27 runs · Thats pretty bad compared to the c example and close to the upper boundary.
🌐
Study.com
study.com › computer science courses › computer science 109: introduction to programming
Basics of Command-Line Input in Java - Lesson | Study.com
March 28, 2023 - If not, check to see if it is installed (usually in Java sub-directory), and if not, install it. If it is, set the PATH environment variable to the correct location. To compile this program, use thejavac command. If your program is in the local directory, you can type the following command (otherwise, you need to give the full path of the file): ... After you issue this command, your directory should contain an Example.class file.
🌐
CodeJava
codejava.net › java-core › tools › examples-of-using-java-command
java command examples
August 4, 2019 - It must be multiple of 1024 and ... the following command launches a program with initial heap size 32MB and maximum heap size 1024MB: java -Xms32M -Xmx1024M MyProgram Other Java Tools Tutorials:...
🌐
GitHub
github.com › mirswamp › java-cli
GitHub - mirswamp/java-cli: The Java CLI is a Java library and a command line interface that provides many common operations to a SWAMP instance: get a list of projects, packages (versions), assessments, tools, & platforms. Users can also create/upload packages (versions), configure/start an assessment, check the status of an assessment, & download SCARF results.
Example: ./bin/swamp package --upload --pkg-archive /home//swamp/api-dev/java-cli/scripts/resources/test_packages/railsgoat-9052b4fcf0/railsgoat-9052b4fcf0.zip -pkg-conf /home//swamp/api-dev/java-cli/scripts/resources/test_packages/railsgoat-9052b4fcf0/package.conf --os-deps 'ubuntu-16.04-64=libsqlite3-dev libmysqlclient-dev' --new-pkg ·
Starred by 6 users
Forked by 3 users
Languages   Java 88.8% | Python 9.5% | Shell 1.7% | Java 88.8% | Python 9.5% | Shell 1.7%
🌐
InfoQ
infoq.com › articles › java-native-cli-graalvm-picocli
Build Great Native CLI Apps in Java with Graalvm and Picocli - InfoQ
March 7, 2020 - Let’s take a concrete example of a command line utility that we will write in Java and compile to a single native executable. Along the way we will look at some features of the picocli library that help make our utility easy to use. We will build a checksum CLI utility, that takes a named option -a or --algorithm, and a positional parameter, which is the file whose checksum to compute.
🌐
INNOQ
innoq.com › en › blog › 2022 › 01 › java-cli-libraries
Libraries for command-line applications — Processing arguments and options in Java – INNOQ
January 23, 2022 - However, Java does not understand the concept of options. In the case of the cURL request described above, an array with the four strings --request, get, -v, and https://innoq.com are provided. It is now the job of the application to interpret these appropriately. As this can quickly become complicated, the use of an appropriate library is recommended. One of the oldest libraries I know for dealing with arguments and options is Apache Commons CLI.