0

I am writing a code that accepts a list of files as an input. I am doing stress testing and an error occurs if there are a lot of files as an input.

My main function accepts an array of Strings as an input.

public static void main(String[] args)

I have around 200 files as an input. My args accepts input in this format:

-f <file path>

At one point on the list of files, Java will throw a File Not Found exception because it gets an incorrect path. There is always only one character missing. And the preceding file entries are read correctly.

I tried to get the length of the string when one character got missing, and it is always on 8090th character.

Example: If I have a list of files in a nested directory. My input will be something like this. -f test\test1\test1_test2\test1_test2_test3\test3_test4.txt

Repeated inputs of this kind would result to:

-f test\test1\test1_test2\test1_test2_test3\test3_test4.txt
...
-f test\test1\test1_**tst2**\test1_test2_test3\test3_test4.txt
...
-f test\test1\test1_test2\test1_test2_test3\test3_test4.txt

There is a missing "e" which should be the 8090th character. But the next file entries are being read correctly. What am I missing?

chris yo
  • 1,187
  • 4
  • 13
  • 30
  • 1
    did you try quoting with " "? – nachokk Jan 03 '14 at 14:08
  • Hi A4L, In my input, it is weird that one character got missing, but the next characters are read correctly. Would it be more appropriate for OS to just throw a "Command too long" error? – chris yo Jan 03 '14 at 14:20

2 Answers2

3

Quoting MS Support

In Command Prompt, the total length of the following command line that you use at the command prompt cannot contain more than either 2047 or 8191 characters (as appropriate to your operating system)

So this means you cannot pass arguments to your program longer than 8191 characters. But workaround could be storing your arguments into the file and pass that file through command line to your program.

Petr Mensik
  • 26,874
  • 17
  • 90
  • 115
0

Make a second main class, where the main reads a file with the arguments.

public class MainWithArgsFile {
    public static void main(String[] fileArgs) {
        List<String> args = new ArrayList<>();
        // Fill args:
        for (String fileArg: fileArgs) { // One or more files.
            try (BufferedReader in = new BufferedRead(new InputStreamReader(
                    new FileInputStream(new File(fileArg)), "UTF-8"))) {
                for (;;) {
                    String line = in.readLine();
                    if (line == null) {
                        break;
                    }
                    //args.add(line); // One arg per line (for instance).
                    Collections.addAll(args, line.split(" +"));
                }
            }
        }
        OriginalMain.main(args.toArray(new String[args.size()]);
    }
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138