Baeldung
baeldung.com › home › java › core java › command-line arguments in java
Command-Line Arguments in Java | Baeldung
December 27, 2025 - The code above expects the file path as a command-line argument. If no file path is provided, it throws an exception and prints the usage instructions instead of attempting to read the file. Let’s place a file named hello.txt in our resources folder and pass its absolute path as an argument in our IDE run configuration: "/home/baeldung/tutorials/core-java-modules/core-java-lang/src/main/resources/hello.txt"
Oracle
docs.oracle.com › en › java › javase › 21 › docs › specs › man › java.html
The java Command
January 20, 2026 - The example illustrates that the class can be in a named package, and does not need to be in the unnamed package. This use of source-file mode is informally equivalent to using the following two commands where hello.World is the name of the class in the package: javac -d <memory> HelloWorld.java java -cp <memory> hello.World · In source-file mode, any additional command-line options are processed as follows:
Parsing arguments to a Java command line program - Stack Overflow
0 Parsing a file passed as argument to Java from command line · 2 Running jshint with Rhino shell using configure option More on stackoverflow.com
How do I put a command-line argument when running from an IDE
Go to Run -> Edit Configurations... and look at your run configuration. There is a field for "Program arguments". More on reddit.com
What’s the use of doing command line argument in Java?
If you'd like the user to be able to provide different "options" to your program, you can use command-line arguments. For example: Perhaps your program has different "modes" of some kind Perhaps your program reads from a file, and you'd like to be able to pass in different filenames You could also prompt the user for input once the program is running, but sometimes this is not convenient. More on reddit.com
Is there any good library to handle command-line arguments?
You may have a look at https://hackage.haskell.org/package/optparse-applicative More on reddit.com
Videos
07:52
Java command line arguments using CMD and Eclipse terminal - YouTube
17:04
Command Line Arguments in Java | Java Tutorial For Beginners | ...
Command Line Arguments in Java | Simple Programs
06:21
Command Line Arguments in Java - YouTube
09:05
Command line arguments/parameters in Java - YouTube
13:02
How to use command line arguments in Java - YouTube
Oracle
docs.oracle.com › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
The Echo example displays each of its command-line arguments on a line by itself: public class Echo { public static void main (String[] args) { for (String s: args) { System.out.println(s); } } } 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.
Oracle
docs.oracle.com › javacomponents › jmc-5-5 › jfr-command-reference › command-line-options.htm
1 Command-Line Options - Java
The following java command unlocks commercial features for an application named MyApp and starts a recording: java -XX:+UnlockCommercialFeatures -XX:StartFlightRecording MyApp · Example 1-2 Unlock Commercial Features for a Running Application
JRebel
jrebel.com › blog › jvm-options-cheat-sheet
JVM Options Cheat Sheet | JRebel by Perforce
If you’re desperate to understand when your garbage collector is doing its job, but don’t want the full verbosity of -verbose:gc, try the -Xlog:gc command (available for Java versions 9 and newer). Are you curious when or if your classes are being loaded? If so, you might want to check out the next option.
Opensource.com
opensource.com › article › 21 › 8 › java-commons-cli
Parse command options in Java with commons-cli | Opensource.com
August 13, 2021 - Finally, add some conditionals to analyze the options provided by the user as command-line input (discovered and stored in the cmd variable). This sample application has two different types of options, but in both cases, you can check whether the option exists with the .hasOption method plus the short option name.
GeeksforGeeks
geeksforgeeks.org › java › command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
Command-line arguments are passed ... main(String[] args) method. ... Example: In this example, we are going to print a simple argument in the command line....
Published May 28, 2026
CodeJava
codejava.net › java-core › tools › examples-of-using-java-command
java command examples
August 4, 2019 - We can use the -Dproperty=value option to specify a system property when running a program: ... We can override the predefined system properties. For example, the following command overrides the system property java.io.tmpdir:
Top answer 1 of 10
83
Use the Apache Commons CLI library commandline.getArgs() to get arg1, arg2, arg3, and arg4. Here is some code:
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.Option.Builder;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.ParseException;
public static void main(String[] parameters)
{
CommandLine commandLine;
Option option_A = Option.builder("A")
.required(true)
.desc("The A option")
.longOpt("opt3")
.build();
Option option_r = Option.builder("r")
.required(true)
.desc("The r option")
.longOpt("opt1")
.build();
Option option_S = Option.builder("S")
.required(true)
.desc("The S option")
.longOpt("opt2")
.build();
Option option_test = Option.builder()
.required(true)
.desc("The test option")
.longOpt("test")
.build();
Options options = new Options();
CommandLineParser parser = new DefaultParser();
String[] testArgs =
{ "-r", "opt1", "-S", "opt2", "arg1", "arg2",
"arg3", "arg4", "--test", "-A", "opt3", };
options.addOption(option_A);
options.addOption(option_r);
options.addOption(option_S);
options.addOption(option_test);
try
{
commandLine = parser.parse(options, testArgs);
if (commandLine.hasOption("A"))
{
System.out.print("Option A is present. The value is: ");
System.out.println(commandLine.getOptionValue("A"));
}
if (commandLine.hasOption("r"))
{
System.out.print("Option r is present. The value is: ");
System.out.println(commandLine.getOptionValue("r"));
}
if (commandLine.hasOption("S"))
{
System.out.print("Option S is present. The value is: ");
System.out.println(commandLine.getOptionValue("S"));
}
if (commandLine.hasOption("test"))
{
System.out.println("Option test is present. This is a flag option.");
}
{
String[] remainder = commandLine.getArgs();
System.out.print("Remaining arguments: ");
for (String argument : remainder)
{
System.out.print(argument);
System.out.print(" ");
}
System.out.println();
}
}
catch (ParseException exception)
{
System.out.print("Parse error: ");
System.out.println(exception.getMessage());
}
}
2 of 10
35
You could just do it manually.
NB: might be better to use a HashMap instead of an inner class for the opts.
/** convenient "-flag opt" combination */
private class Option {
String flag, opt;
public Option(String flag, String opt) { this.flag = flag; this.opt = opt; }
}
static public void main(String[] args) {
List<String> argsList = new ArrayList<String>();
List<Option> optsList = new ArrayList<Option>();
List<String> doubleOptsList = new ArrayList<String>();
for (int i = 0; i < args.length; i++) {
switch (args[i].charAt(0)) {
case '-':
if (args[i].length < 2)
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
if (args[i].charAt(1) == '-') {
if (args[i].length < 3)
throw new IllegalArgumentException("Not a valid argument: "+args[i]);
// --opt
doubleOptsList.add(args[i].substring(2, args[i].length));
} else {
if (args.length-1 == i)
throw new IllegalArgumentException("Expected arg after: "+args[i]);
// -opt
optsList.add(new Option(args[i], args[i+1]));
i++;
}
break;
default:
// arg
argsList.add(args[i]);
break;
}
}
// etc
}
Oracle
docs.oracle.com › javase › 7 › docs › technotes › tools › windows › java.html
java
If the Java VM is run as a service (for example, the servlet engine for a web server), then it can receive CTRL_LOGOFF_EVENT but should not initiate shutdown because the operating system will not actually terminate the process. To avoid possible interference such as this, the -Xrs command-line option ...
Coderanch
coderanch.com › t › 548987 › java › Java-Command-Line-Options
Java Command Line Options (Beginning Java forum at Coderanch)
August 11, 2011 - While using Java Options, i found they are of two types. One that has "-" as a prefix and another as "+". For example: -XX:-UseParallelGC XX:+UseThreadPriorities Is there some reason why we have both "+" as well as "-". Initially, i thought that + would mean enabling while - would mean disabling. But, if we want any option disabled than why even pass it along the command line...
IBM
ibm.com › docs › en › ztpf › 1.1.2023
Command-line options
You can specify the options on the command line while you are starting Java. They override any relevant environment variables. For example, using -cp with the Java command completely overrides setting the environment variable CLASSPATH= .
OpenJDK
openjdk.org › jeps › 293
JEP 293: Guidelines for JDK Command-Line Tool Options
The use of the '=' form will also make it easier to pass options from a tool other than the launcher to the underlying VM using -J. For example, you can use a single J option, as in -J--<name>=<value>, instead of having to use two -J options as before: -J-<name> -J<value>. The = form also makes ...
Princeton
cs.princeton.edu › courses › archive › spr96 › cs333 › java › tutorial › java › cmdLineArgs › index.html
Command Line Arguments
C:\> java Sort ListOfFriends In the Java language, when you invoke an application, the runtime system passes the command line arguments to the application's main method via an array of Strings. Each String in the array contains one of the command line arguments. In the previous example, the command line arguments passed to the Sort application is an array that contains a single string: "ListOfFriends".
Eclipse Foundation
eclipse.dev › openj9 › docs › cmdline_specifying
Eclipse OpenJ9 command-line options
Use single or double quotation marks for command-line options only when explicitly directed to do so. Single and double quotation marks have different meanings on different platforms, operating systems, and shells. Do not use '-X<option>' or "-X<option>". Instead, you must use -X<option>. For example, do not use '-Xmx500m' and "-Xmx500m". Write this option as -Xmx500m. The sequence of the Java ...
DigitalOcean
digitalocean.com › community › tutorials › command-line-arguments-in-java
Command Line Arguments in Java | DigitalOcean
August 3, 2022 - $ java com/journaldev/examples/CommandLineArguments.java "A" "B" "C" Number of Command Line Argument = 3 Command Line Argument 0 is A Command Line Argument 1 is B Command Line Argument 2 is C $ java com/journaldev/examples/CommandLineArguments.java 1 2 3 Number of Command Line Argument = 3 Command Line Argument 0 is 1 Command Line Argument 1 is 2 Command Line Argument 2 is 3 $
Programiz
programiz.com › java-programming › command-line-arguments
Java Command-Line Arguments
For example, ... Here apple, ball, and cat are arguments passed to the program through the command line. Now, we will get the following output. ... In the above program, the main() method includes an array of string named args as its parameter. ... The String array stores all the arguments ...
Picocli
picocli.info
picocli - a mighty tiny command line interface
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: java -cp "picocli-4.7.7.jar:checksum.jar" CheckSum --algorithm SHA-1 hello.txt · You may want to package your application in such a way that end users can invoke it with a short command like this: ... See the Packaging Your Application section for ideas on how to accomplish this. Command line arguments can be separated into options and positional parameters.