0

I need to pass a file path to main method from windows command line.

c:\scanner> java -jar scanner.jar path-to-file-with-unicode-characters

The file path contains a unicode characters and those characters are being removed/replaced after being passed to java main

Anthony J.
  • 375
  • 1
  • 5
  • 14
  • 2
    I’m pretty sure this is a duplicate of https://stackoverflow.com/questions/7660651/passing-command-line-unicode-argument-to-java-code. As old as that question and most of its answers are, I don’t think the situation has changed. – VGR Jul 19 '18 at 14:45
  • [Possible Duplicate: pass unicode parameter to Java command line] (https://stackoverflow.com/questions/39937997/pass-unicode-parameter-to-java-command-line) – Rafael Palomino Jul 19 '18 at 14:45
  • 2
    @RafaelPalomino Dealing with Unicode in Windows is quite different from dealing with it in Unix and Linux, unfortunately. – VGR Jul 19 '18 at 14:46

1 Answers1

1

The problem lies with the command line and its encoding.

The solution might be to store the file path is a separate small text file.

Under Linux the convention for this is to prefix with a @:

public static void main(String[] args) {
    for (int i = 0; i < args.length; ++i) {
        if (args[i].startsWith("@")) {
            Path path = Paths.get(args[i].substring(1));
            args[i] = new String(Files.readAllBytes(path), StandardCharsets.UTF_8).trim();
        }
    }
    RealClass.main(args);
}
Joop Eggen
  • 107,315
  • 7
  • 83
  • 138