I have a problem with encoding Java System Output occurring only in Visual Studio Code.
Like you can see in the image below bullet print as ?
Eclipse and IntelliJ print the bullet point just fine.
My program is very simple:
Things I have tried/checked:
chcp:
UTF8 is set:
I only have the Java Extension Pack by Microsoft installed.
It is a fresh new file created from VScode.
The chcp inside VSCode terminal is Active code page: 65001
The UTF-8 displayed in the lower right corner of VS code is the encoding format of the current file, not the encoding format of the terminal.
You should check the encoding by typing chcp in the terminal of VS code.
The encoding format of the terminal in vscode may be different from the system's cmd and powershell encoding.
So please check the encoding format in the terminal in VS code, not in the cmd or powershell window of the system
Here is my test display:
my code:
public class App {
public static void main(String[] args) {
System.out.println("example ●");
}
}
The encoding format of the system cmd window is 65001
The encoding format of the system powershell window is 65001
But the terminal encoding in vscode is 437
Run the result directly ( Can't display symbols )
So you need to use chcp 65001 to change the current terminal encoding format in VS code.
then run the code ( success display symbol)
But this still has problems, every time you open a new terminal, you need to manually type the command chcp 65001 to change the encoding format.
I found a way in my constant search. Add the following configuration in settings.json:
"terminal.integrated.shellArgs.windows": ["-noexit", "chcp 65001"]
A yellow squiggly line will appear indicating that this configuration is out of date and there are now new configuration commands. Never mind, this still works. If you want to see the new configuration, here.
Note: After adding this configuration, you need to restart VS code to take effect.
In this way, when you create a new terminal, you will automatically use the command chcp 65001 to change the encoding to 65001
Now run the code directly, the symbols can be displayed
Related
I have a simple java code snippet that prints out text. This is what the code snippet looks like:
public class Main {
public static void main(String[] args) {
System.out.println("Pelisäännöt.");
}
}
However, when I run this code from the Windows command prompt the output does not print ä's and ö's properly. This is the output.
Pelisõõnn÷t
I am using Gradle and Netbeans to manage this "project", my Java version is 1.8.0_261 and I am using Windows 10 command prompt. I have not changed any settings of the command prompt or customised it in any way.
How can I make the ä's and ö's display properly?
By default your command prompt is not set to UTF-8. You can use chcp 65001 to change it to use UTF-8. You also must be using a font that supports the characters.
I figured out how to make the scandinavian characters display correctly.
Change code page to 1252 by running command chcp 1252
Change the font the command prompt is using to "Lucida Console" by right clicking on the command prompts window and selecting "Properties" -> "Font" -> "Lucida Console".
Thank you for all the answers, they helped me in arriving at the conclusion!
I have a problem with encoding of Java System Output occurring only in Visual Studio Code.
My program is very simple:
public class Main {
public static void main(final String[] args) {
System.out.println("Příliš žluťoučký kůň úpěl ďábelské ódy");
}
}
but the output looks like this:
Příliš žluťoučký kůň úpěl ďábelské ódy�ábelské ódy�dy
My file has the UTF-8 encoding and it's being compiled with -Dfile.encoding=UTF-8 flag. I am using the official Java Extension Pack by Microsoft in Visual Studio Code.
as you had installed "Java Extension Pack", and mentioned "-Dfile.encoding=UTF-8", so, you are debug through "Debugger for Java" extension. that's caused the fails of I want to recurrence your problem.
I tried to edit the encoding of terminal, through task.json, or in the settings.json adds "terminal.integrated.shellArgs.windows": ["/K", "chcp 65001"], but both of them not works.
that's of course, as the "Debugger for Java" extension will open a new terminal to execute the commands, as the settings above will not take a infulence on that terminal.
so, I can recurrence your problem this time, as I open the "launcher.bat" file, which shows in the commands, and change the code -> "chcp.com 65001 > NUL" in it. any other number of chcp.com will caused your problem.
so, maybe, this can provide some inspirations to you.
vscode was different from your original 'javac,java' commands, as vscode depends on extensions to execute those commands.
through "Language Support for Java(TM) by Red Hat" extension compile '.java' file to '.class', it customized add '-encoding utf8' args.
through "Debugger for Java" extension to change the terminal 'chcp' to '65001', and '-Dfile.encoding=UTF-8' to make sure the JVM start with encoding 'utf8'.
you can achieve the same effects through add enviroments variable:
"JDK_JAVAC_OPTIONS":"-encoding utf8",
"JDK_JAVA_OPTIONS":"-Dfile.encoding=UTF-8",
and 'chcp 65001' to temporary change the terminal encoding.
it's very complex and strange, as I can not sure what's the 'java', 'javac' default encoding, I had tried a lot but still confusing, as I googled a lot, but cann't get the enough informations. but everythings are included in those three variables, I hope this can provide you some useful informations, I am too tired. sorry...
for code runner extention,just add this code to user setting json:
"code-runner.executorMap":{"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",},
"code-runner.runInTerminal": true,
I faced the similar issue. My environment is a little bit complicated.
I use both of 'Code-Runner' and 'Java-extenstion Pack.' That seems to make it harder for me to resolve the issue.
After several trying, I found out below things.
When I use run icon (in upper-right side), 'code-runner' compine and run my code in "OUTPUT", which shows broken Korean characters.
When I use "Run > Debugging" or "Run > Run Without Debugging", 'Java extension Pack' compile and run my code in "TERMINAL", which shows Korean characters properly.
Below is my final settings.
A. settings.json in workspace
Insert -encoding utf-8 in 'code-runner.executoMap'
....
"code-runner.executorMap": {
"javascript": "node",
"java": "cd $dir && javac -encoding utf-8 $fileName && java $fileNameWithoutExt",
.....
"sml": "cd $dir && sml $fileName"
}
B. settings.json in User
Add "java.jdt.ls.vmargs": "-Dfile.encoding=utf-8"
Add "code-runner.runInTerminal": true
After setting, code-runner prints Korean characters well in "TERMINAL"(not "OUTPUT"), and 'java extension pack' also does.
I want to read a file path from the user in java console application,
some of the file path may contain some Hebrew characters.
how can i read the input from the command line when i don't know the encoding charset?
I have been spending some time on the web and didn't succeed to find any relevant solution that will be dynamic for every platform.
*
Screen shot when running in console
If you are using windows you need to check the terminal encoding before to make sure that its encoding supports hebrew.
To do this just type chcp in the console
as output you should see chcp 28598
if you see diffrent number type chcp 28598
Now your console encoding is set to hebrew and you should be able to write the path in hebrew without getting any exception.
I have created a simple .jar file which is taking as argument a string with greek characters and prints it in a file.
However, I have the following issue:
When I execute the jar file from my local windows machine, the string is properly passed as argument in the jar file and the output in the file contains the greek characters I inserted.
When I try to execute the same jar file in a windows VM, the greek characters are not properly encoded and the output in the file contains unreadable characters.
I have even set the command prompt in the VM in chcp 1253 and set an environmental variable as JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8 with no luck...
Any suggestion?
Running chcp 1253 sets your console codepage to Windows 1253, and yet you set Java to not use it...
If you are running your program via a batch script, save it as UTF-8 and add -Dfile.encoding=UTF-8 to parameters for the java command.
If you are running your program via the console, run chcp 65001 to switch the console to UTF-8. Also, you set the variable correctly, you can leave it that way, but you can also run Java with this option set explicitly:
chcp 65001
java -Dfile.encoding=UTF-8 -jar binks.jar
EDIT: If Windows is still complaining and/or messing stuff up, try changing 65001 to 1523 and UTF-8 to Windows-1253. You'll lose support for most of Unicode, but there's greater chance now it will work.
So, I have basically been trying to use Spanish Characters in my program, but wherever I used them, Java would print out '?'.
I am using Slackware, and executing my code there.
I updated lang.sh, and added: export JAVA_TOOL_OPTIONS=-Dfile.encoding=UTF-8
After this when I tried printing, it did not print the question marks, but other junk characters. I printed the default Charset on screen, and it has been successfully set, but it is not printing properly.
Help?
Thanks!
EDIT: I'm writing code in windows on NetBeans, and executing .class or .jar on slackware.
Further, I cannot seem to execute locale command. I get error "bash: locale: command not found".
This is what confuses me: When I echo any special characters on Slackware console, they are displayed perfectly, but when I run a java program that simply prints it's command line arguments (and I enter the special characters as Command Line input), it outputs garbage.
If you are using an ssh client such as PuTTY, check that it is using a UTF-8 charset as well.