2

Context: Windows 10, cmd.exe, javac 9.0.1.

I have unicode encoded source code. If I run javac -encoding UTF-8 ... and I have an error, I just can't get it to display the source correctly.

enter image description here

As you can see in the picture, the cli can print unicode chars just fine.

herr.kaste
  • 442
  • 3
  • 12
  • 1
    I wonder what's the reason for voting to close? This seems a reasonable question to me. – eis Dec 22 '17 at 15:30
  • What if you fix the missing `;` it is telling you about? Does the compiled code then work as you expect? – Andy Turner Dec 22 '17 at 15:30
  • That seems to be a funny community here ;-) – herr.kaste Dec 22 '17 at 15:31
  • 2
    @AndyTurner how's that related to the question? The question was not about the code working or not, it was about javac output problems. – eis Dec 22 '17 at 15:38
  • @eis if javac produces the expected class file, it's simply a display artefact. Otherwise, javac isn't interpreting the code correctly. – Andy Turner Dec 22 '17 at 15:39
  • I took it it was recognized by OP that this is a display issue on errors, and otherwise things seem to work. But I guess it could've been explicitly mentioned. – eis Dec 22 '17 at 15:42
  • This is a toy program just to test unicode. I removed the ';' intentionally to get an error on a line that has obviously a lot of unicode chars in it. – herr.kaste Dec 22 '17 at 15:45
  • This question has been addressed in almost excessive detail by andrewdotn in [what-encoding-code-page-is-cmd-exe-using](http://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using) – Tzalumen Dec 22 '17 at 18:57

2 Answers2

5

It would appear that javac is not using your terminal's character encoding.

You can specify the character encoding for a JVM using the flag:

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

(Or whatever encoding)

Javac is just a thin wrapper around a Java program. You can pass arguments directly to its JVM using the -J flag. So:

javac -J-Dfile.encoding=UTF-8 ...
Andy Turner
  • 137,514
  • 11
  • 162
  • 243
0

You can know your current(default) encoding by running

System.getProperty("file.encoding")

and you can change default encoding with this property.

For Windows it is usually - cp1252,

Long Story, queue from IBM KB:

Internally, the Java virtual machine (JVM) always operates with data in Unicode. However, all data transferred into or out of the JVM is in a format matching the file.encoding property. Data read into the JVM is converted from file.encoding to Unicode and data sent out of the JVM is converted from Unicode to file.encoding.

iMysak
  • 2,170
  • 1
  • 22
  • 35