Javac encoding for scratch files ignored

Answered

I have a simple scratch file with a Japanese text:

class Scratch {
public static void main(String[] args) {
System.out.println("世界こんにちは");
}
}
  • Both the project and global file encoding settings are set to UTF-8 (verified with a text editor).
  • The scratch file has been created after the project and global settings have been set (https://youtrack.jetbrains.com/issue/IDEA-200826).
  • I have also set "-Dconsole.encoding=UTF-8" as custom VM options.

Still, when running this Scratch file, the output in the consoloe is garbled.

荳也阜縺薙s縺ォ縺。縺ッ

It seems that javac for scratch files does not respect the specified encoding. If I compile and run the file manually with

> javac -encoding UTF-8 Scratch.java
> java Scratch

The output is as expected

世界こんにちは

If I run the java file compiled by Intellij manually from the command line with

> java Scratch

I get the same garbled result. Setting "-encoding UTF-8" in the Java compiler setting / Additional command line parameters has no effect.

I am using Intellij Ultimate 2019.3.4 with openjdk version "13.0.1".

Is this a bug in Intellij?

0
10 comments

Can't reproduce, IntelliJ IDEA adds the correct "-Dfile.encoding=UTF-8" option when running scratch files:

 

The same works with JDK 13.

Does it help if you add -Dfile.encoding=UTF-8 in Help | Edit Custom VM Options and restart IntelliJ IDEA?

0
Avatar
Permanently deleted user

Dear Serge,

thank you very much for your quick answer. This is strange - I should have all the correct settings applied (see screenshots below). I had also tried setting the -Dfile.encoding=UTF-8 as custom VM options and restarted IDEA, but then IDEA would not start up (the program died before the splash screen). I had to manually remove this options from the idea64.exe.vmoptions file.

VM Options:

File encodings:

Compiler:

But still this output:

If I compile the Scratch file manually with "encoding", it works:

Also, buidling and running a simple Java file within a project also works (same settings as above):

 

By the way, I realized when I edit the run configuration for a Scratch file, no matter which classpath I choose from "Use classpath of module" (even "<no module>"), the "-classpath" argument for the java command shown in the console output stays the same.

Does any information above provide you with some hints?

Thank you again.

0

What error do you get with -Dfile.encoding=UTF-8 added in Help | Edit Custom VM Options? Try running from the command line using idea.bat to get the error details.

Anyway, it looks like a bug that without this option it doesn't work properly for Scratch files, but works for normal classes.

I've asked the responsible developer to investigate it.

0
Avatar
Permanently deleted user

Dear Serge, again thank you.

Below is the output by running the bat file:

https://pastebin.com/raw/wWBUf9vc .

Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
j sun.awt.windows.WComponentPeer._setFont(Ljava/awt/Font;)V+0 java.desktop@11.0.6
j sun.awt.windows.WComponentPeer.setFont(Ljava/awt/Font;)V+7 java.desktop@11.0.6
j sun.awt.windows.WWindowPeer.initialize()V+56 java.desktop@11.0.6
j sun.awt.windows.WComponentPeer.<init>(Ljava/awt/Component;)V+83 java.desktop@11.0.6
j sun.awt.windows.WCanvasPeer.<init>(Ljava/awt/Component;)V+2 java.desktop@11.0.6
j sun.awt.windows.WPanelPeer.<init>(Ljava/awt/Component;)V+2 java.desktop@11.0.6
j sun.awt.windows.WWindowPeer.<init>(Ljava/awt/Window;)V+2 java.desktop@11.0.6
j sun.awt.windows.WToolkit.createWindow(Ljava/awt/Window;)Ljava/awt/peer/WindowPeer;+5 java.desktop@11.0.6
j java.awt.Window.addNotify()V+40 java.desktop@11.0.6
j java.awt.Window.show()V+8 java.desktop@11.0.6
j java.awt.Component.show(Z)V+5 java.desktop@11.0.6
j java.awt.Component.setVisible(Z)V+2 java.desktop@11.0.6
j java.awt.Window.setVisible(Z)V+2 java.desktop@11.0.6
j com.intellij.ui.Splash.initAndShow()V+17

 

 

0

It's a known issue: https://youtrack.jetbrains.com/issue/JBR-2210 .

It's fixed in jbr-11_0_6-windows-x64-b804 and later builds. You can try switching to this JetBrains Runtime build per https://intellij-support.jetbrains.com/hc/articles/206544879 .

0
Avatar
Permanently deleted user

Thank you Serge, I didn't know that.

Then I'll wait for the next fixed release, and I also hope that JB will find a solution to my initial issue.

 

0
Avatar
Permanently deleted user

Thank you Serge, will do.

0
Avatar
Permanently deleted user

Same problem from a long time ago and now.
Just:

0

The fix for this problem is available in 2020.2 EAP, please give it a try: https://www.jetbrains.com/idea/nextversion/ .

0

Please sign in to leave a comment.