Java – Using command-line argument for passing files to a program


How can I receive a file as a command-line argument?

Best Solution

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:


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