4

I wondered whether there was any connection between the main method's parameter String[] args and the possibility of opening files with a specified program. Considering that I wrote a simple program that writes down every String of args, then opened some files with this program (I am using windows).
This is what happened:
No matter what kind of file I opened with my program (right click -> open with...), argscontained only one String which was the file's complete path.
When I ran the program on itself, args was of length 0.

Now my question is: are there any other Strings that might be contained in args or would the following code always work without doubt? (I want to use this on windows, not play around with it like java MyProgram 1 2 3 "test"

public static void main(String[] args) {

    initProgram();

    if (args.length != 0) {            //file opened with program
        loadFile(new File(args[0]));
    }
}

Thank you for your answers and please be patient with my english.

2
  • 1
    You will only receive one argument while opening one file, tryed to drag two files on top of a Jar file to see if it receives many? Aug 6, 2018 at 21:25
  • 1
    If you try to start many files at once (not a list as argument) windows will start a new instance of the program for each, you should look if you can listen for a second file on the same proccess (like GIMP does for many files) Aug 6, 2018 at 21:26

1 Answer 1

1

Just like Marcos Vasconcelos assumed: by opening multiple files at once args will contain all the files' paths tried to open, so args can be larger than just one String. Its length depends on the amount of files that want to be opened with the program.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.