Just the path of the file is passed, inside your program use the Java File class to handle it

This takes the first parameter as the file path:

import java.io.File;

public class SomeProgram {
    public static void main(String[] args) {
        if(args.length > 0) {
            File file = new File(args[0]);

            // Work with your 'file' object here
        }
    }
}
Answer from victor hugo on Stack Overflow
🌐
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 › javase › tutorial › essential › environment › cmdLineArgs.html
Command-Line Arguments (The Java™ Tutorials > Essential Java Classes > The Platform Environment)
To sort the data in a file named friends.txt, a user would enter: ... When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of Strings. In the previous example, the command-line arguments passed to the Sort application in an array that contains a single String: "friends.txt".
🌐
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 $ Note: If you are using Java 11 or higher, you don’t need to compile the java source file explicitly.
Top answer
1 of 4
33

Just the path of the file is passed, inside your program use the Java File class to handle it

This takes the first parameter as the file path:

import java.io.File;

public class SomeProgram {
    public static void main(String[] args) {
        if(args.length > 0) {
            File file = new File(args[0]);

            // Work with your 'file' object here
        }
    }
}
2 of 4
8

in Java, the main method receives an array of String as argument, as you probably have noticed. you can give another name to the parameter args, but this is the most used one.

the array args contains the values of what the user has typed when launching your program, after the class name. for example, to run a class named Foo, the user must type:

[user@desktop ~]$ java Foo

everything the user types after the class name is considered to be a parameter. for example:

[user@desktop ~]$ java Foo bar baz

now your program has received two parameters: bar and baz. those parameters are stored in the array args. as a regular Java array, the first parameter can be retrieved by accessing args[0], the second parameter can be retrieved by accessing args[1], and so on. if you try to access an invalid position (when the user didn't type what you expected), that statement will throw an ArrayIndexOutOfBoundsException, just like it would with any array. you can check how many parameters were typed with args.length.

so, back to your question. the user may inform a file name as a command line parameter and you can read that value through the argument of the main method, usually called args. you have to check if he really typed something as an argument (checking the array length), and if it's ok, you access args[0] to read what he's typed. then, you may create a File object based on that string, and do what you want to do with it. always check if the user typed the number of parameters you are expecting, otherwise you'll get an exception when accessing the array.

here's a full example of how to use command line parameters:

public class Foo {
    public static void main(String[] args) {
        if (args.length == 0) {
            System.out.println("no arguments were given.");
        }
        else {
            for (String a : args) {
                System.out.println(a);
            }
        }
    }
}

this class will parse the parameters informed by the user. if he hasn't type anything, the class will print the message "no arguments were given." if he informs any number of parameters, those parameters will be shown on the screen. so, running this class with the two examples I've given on this answer, the output would be:

[user@desktop ~]$ java Foo
no arguments were given.
[user@desktop ~]$ java Foo bar baz
bar
baz

🌐
GeeksforGeeks
geeksforgeeks.org › java › command-line-arguments-in-java
Command Line Arguments in Java - GeeksforGeeks
Command-line arguments in Java are values passed to a program during execution through the command prompt. These arguments are stored as String values in the main() method parameter. Used to provide input without hardcoding values in the program.
Published   May 28, 2026
🌐
TechVidvan
techvidvan.com › tutorials › java-command-line-arguments
Java Command Line Arguments with Examples - TechVidvan
April 9, 2020 - Let’s learn how to use command-line arguments in our Java program. To understand the command-line argument we will create a file named CommandLine.java and write the following code to display all the arguments that we will pass through the command-line:
🌐
Edureka
edureka.co › blog › java-command-line-argument
Java Command Line Arguments With Examples | Java Tutorial | Edureka
August 27, 2024 - The command-line arguments are stored in the String args of the main() method of the program. Related Learning: Java Interview Questions for 3 Years Experience
🌐
Medium
naveen-metta.medium.com › mastering-command-line-arguments-in-java-a-comprehensive-guide-for-versatile-application-24612562bea9
Mastering Command Line Arguments in Java: A Comprehensive Guide for Versatile Application Customization | by Naveen Metta | Medium
January 14, 2024 - In Java, these arguments manifest as strings separated by spaces and are instrumental in enhancing the flexibility and adaptability of a program. ... Accessing command line arguments in Java is facilitated through the main method’s args parameter, which serves as the entry point for the application.
Find elsewhere
🌐
University of Pennsylvania
cis.upenn.edu › ~bcpierce › courses › 629 › papers › Java-tutorial › java › cmdLineArgs › cmdLineArgs.html
Command-Line Arguments
numberOfArgs = args.length; In C and C++, the system passes the entire command line to the program as arguments, including the name used to invoke it. For example, if you invoked a C program as shown below the first argument in the argv parameter is diff: diff file1 file2 In Java, you always know the name of the application because it's the name of the class where the main method is defined.
🌐
Squash
squash.io › java-command-line-arguments-how-to-use-them
How to Use the Java Command Line Arguments - Squash Labs
August 8, 2023 - This command will pass the file path "input.txt" and the word "hello" as command line arguments to the program. The output will be the number of occurrences of the word "hello" in the file. Related Article: How to Print an ArrayList in Java
🌐
Scaler
scaler.com › home › topics › java command line arguments
Java Command Line Arguments - Scaler Topics
November 22, 2023 - To pass command line arguments ... the directory where you saved your program. Compile your program using the command javac filename.java....
🌐
Whitman College
whitman.edu › mathematics › java_tutorial › java › cmdLineArgs › cmdLineArgs.html
Command Line Arguments
numberOfArgs = args.length; In C and C++, the system passes the entire command line to the program as arguments, including the name used to invoke it. For example, if you invoked a C program as shown below the first argument in the argv parameter is diff: diff file1 file2 In Java, you always know the name of the application because it's the name of the class where the main method is defined.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › command line arguments in java
Command Line Arguments in Java Explained with Examples
May 5, 2025 - Right-click on the file > Run As > Run Configurations. Go to the Arguments tab. Enter arguments in the Program arguments field. Run > Edit Configurations > Program arguments. This helps users not comfortable with the terminal to test argument-based ...
🌐
GitHub
github.com › learn-co-curriculum › java-command-line-args
GitHub - learn-co-curriculum/java-command-line-args
Introduce passing arguments through the command-line. Let's step away from File IO for a minute to talk about a feature of Java we have been ignoring, but might find helpful for working with files: command-line arguments.
Author   learn-co-curriculum
🌐
Tutorialspoint
tutorialspoint.com › java › java-command-line-args.htm
Java - Command Line Arguments
Consider the below syntax of passing command-line arguments: ... Here we've compiled one java file namely tester.java and while running the tester class using java, we're passing three arguments which are separated by space. We can pass any number of command line arguments to java program.
🌐
Coderanch
coderanch.com › t › 571030 › java › command-line-arguments-file
command line arguments for a file (Beginning Java forum at Coderanch)
March 21, 2012 - ... You can create a prompt to request a file name. You can use a file chooser. Lots of ways to do it. ... The code on line 12 is not doing what the comment on lines 10 and 11 say. The args array that is passed to the main method (line 4) contains the command line parameters.
🌐
Florida State University
cs.fsu.edu › ~jtbauer › cis3931 › tutorial › essential › attributes › cmdLineArgs.html
Command-Line Arguments
To sort the data in a file named friends.txt, you would run it like this: java Sort friends.txt 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.
🌐
MIT
web.mit.edu › 6.031 › www › sp19 › projects › crossword › commandline.html
Running Java Programs with Command-Line Arguments
java -cp "bin;lib/parserlib.jar" some.package.Main argument1 argument2 … · You can also specify command-line arguments in Eclipse using the menu command Run → Run Configurations, choosing the class containing your main() method in the dialog box, then selecting the Arguments tab. Enter your arguments in the Program Arguments box, not the VM Arguments box. Eclipse can also export your program as a “runnable JAR file” that contains all the code in a single file.
🌐
University of Hawaii
www2.hawaii.edu › ~walbritt › ics211 › examples › ReadFromFile.java
ReadFromFile.java
import java.util.Scanner; import java.io.File; import java.io.FileNotFoundException; /** * Shows How To Read from a File. * * @author William Albritton */ public class ReadFromFile { /** * The "main" Method Starts The Program. * * @param args The First Command Line Argument Is The Input File Name, dude */ public static void main(String[] args) { /* To avoid scope issues, initialize all variables at the top of each method.