Java Error

Java ' ' is an Invalid Character - Cause and Solution

Shou Arisaka
1 min read
Nov 18, 2025

In Java, when you paste copied code and run javac, you get this error.

PS C:\pg\java> javac .\Wind.java
.\Wind.java:6: エラー: この文字は、エンコーディングMS932にマップできません
frame.setSize(850,700); 縲?縲?縲?縲?縲?縲?縲?縲? 縲?//

.\Wind.java:6: エラー: 文ではありません
frame.setSize(850,700); 縲?縲?縲?縲?縲?縲?縲?縲? 縲?//
                        ^                         

The file encoding is properly UTF-8, and there's no BOM attached...

Looking into it, it seems good to specify the character encoding when encoding.

PS C:\pg\java> javac -encoding UTF-8 .\Wind.java
.\Wind.java:6: エラー: '\u3000'は不正な文字です
frame.setSize(850,700);           //

Huh.

The previous error is gone, but now there's a new error.

Looking into it, \u3000 seems to be a full-width space. I see.

In Atom, enable regular expressions and replace \u3000.

Once again,

PS C:\pg\java> javac -encoding UTF-8 .\Wind.java

It worked.

Shouldn't it work without specifying the character encoding?

PS C:\pg\java> javac .\Wind.java

It worked.

If there are many files containing such characters, it's good to batch replace them with sed or something.

That said, if the source contains Japanese, you need to add the aforementioned option when building to prevent garbled characters.

javac -encoding UTF-8 .\Wind.java

Share this article

Shou Arisaka Nov 18, 2025

🔗 Copy Links