Anthony J.
Anthony J.

Reputation: 395

Passing unicode string to java from windows command promot

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

Upvotes: 0

Views: 138

Answers (1)

Joop Eggen
Joop Eggen

Reputation: 109547

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);
}

Upvotes: 1

Related Questions