How can I receive a file as a command-line argument?
4 Answers
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
}
}
}

- 35,514
- 12
- 68
- 79
-
can you tell me more about what you mean with "arg[0]" I couldn't find the useful source for reading the command-line topic.:(((( – Johanna Jun 28 '09 at 17:36
-
Read the section "the main method" at http://java.sun.com/docs/books/tutorial/getStarted/application/index.html. It describes reasonably well what "args" is. The Tutorial itself is a good source for finding the answer to a number of questions you have had. – Kathy Van Stone Jun 28 '09 at 17:41
-
@Johanna: The args[0] is accessing the string array args at position 0 (the first element of the array). args is passed as a parameter to the main(String[] args) method, and is an array of strings which contains the command line arguments passed to the program. – Sean Jun 28 '09 at 17:42
-
@Johanna Kathy's suggestion is actually very good, I think you should start reading Java Tutorial to understand some basics. The variable 'args' is defined in the main method signature and when you type args[0] you access the first element of the array. Since the array is filled by the JVM with the command line arguments it args[0] will contain the first one – victor hugo Jun 28 '09 at 17:45
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

- 15,908
- 12
- 46
- 47
In my opinion, best practice to try to detect args == 0 and return a list of help commands on how to use the program if no arguments are provided.

- 774
- 1
- 9
- 19
-
1Welcome to StackOverflow! I think the poster wants to know how to solve it if args > 0, actually. – S.L. Barth is on codidact.com Nov 08 '12 at 11:01
I saw a couple other solutions, but I think they won't work if you. For example, provide C:\Program Files\file.dat
as command-line argument. Then it will not work, as you only take args[0]
when creating a new file.
So you should actually do something like the following, join all parts of the given argument and then make a file from it:
import java.io.File;
public class SomeProgram {
public static void main(String[] args) {
String current = "";
File lastFile = null;
for(String str : args){
File newFile = new File((current + " " + str).trim());
if(newFile.exists()){
lastFile = newFile;
}
current += " " + str;
}
File yourFile = lastFile;
}
}
I am currently working on a similar problem and this seems to do the trick.
-
This is a good solution iff the argument(s) being passed to the program is a file path that contains spaces. However, this is not a good approach if more than just a file path is being passed to the program. The proper approach is to surround the file path with quotes before sending it to the program. The program should then check that each argument is of the correct type, and if not display the correct use case and exit. – Jonny Henly Apr 20 '15 at 06:10