Search code examples
javacmdcommand-linejardiacritics

Unable to open File with Umlauts (chcp 65001/1252) with JAR - JAVA


I have a File that contains Umlauts (erklärung1.txt) that I need to process using my JAR File.

I implemented my Code in IntelliJ, and when I run it from there, it can find and open the file without any problems.

The problem happens when I run it from the command line on Windows since I need to run it as a JAR File. I have used the chcp 1252 command, it did not recognize the Umlauts properly. Then, I used the chcp 65001 command, this one did recognize the Umlauts but unfortunately I am still unable to find the File, not sure why - since the console prints out the path correctly.

Here is my code:

String pathOne = "C:\\Users\\takibu\\Desktop\\Erklärungen\\erklärung1.txt";
File pathFile = new File(pathOne);
if(pathFile.exists()) {
    System.out.println("It exists!");
} else {
    System.out.println("Path does not exist: " + pathOne);
} 

And here is the output with the chcp 65001: enter image description here

I am wondering, and have been searching for a while now, unfortunately without any luck, is there a way to find&open a File containing Umlauts from JAR & the console? The part that confuses me - as You can see, the path is printed out correctly in the console with the chcp 65001 - but still, it is not found, although it is there?

Any help would be greatly appreciated!!

PS: With command chcp 1252, I get this output: Erklärungen\erklärung1.txt


Solution

  • Update; I solved it by running

    java -Dfile.encoding=UTF-8 -jar jarfile
    

    Key here was -Dfile.encoding=UTF-8