VS Code Java System Output Encoding - java

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.

Related

How to hide unwanted log messages on Visual Studio Code, running Java code

I am using Visual Studio Code to write Java programs.
Everything works fine except that I always get unwanted messages when I run the code.
For example, I created a very simple and basic Java project that contains a App.java file (in the src folder, default package):
public class App {
public static void main(String[] args) throws Exception {
System.out.println("Hello, World!");
}
}
I have installed the extensions Java Test Runner and Debugger for Java. If I press "Run", I get:
PS C:\Users\Light\Documents\J\JVsc> cd 'c:\Users\Light\Documents\J\JVsc'; & 'c:\Users\Light\.vscode\extensions\vscjava.vsco\extensions\vscjava.vscode-java-debug-0.28.0\scripts\launcher.bat' 'C:\Program Files\Java\jdk-11.0.1\bin\javaTF-8' '-cp' 'C:\Users\Ri.exe' '-Dfile.encoding=UTF-8' '-cp' 'C:\Users\Light\AppData\Roaming\Code\User\workspaceStorage\67e3f4593863e5815\bin' 'App' 7c280b6c994f0132b01b2\redhat.java\jdt_ws\JVsc_623e5815\bin' 'App'
Hello, World!
This is very bad to see, and I have to record the video for school purposes, and I don't want these messages to appear. I dont' even know if this is a debugger issue or not.
It seems that VSC calls "CD command" before running the program.
I have tried to change the launch.json setting "console": "internalConsole" but it does not solve the problem, what it happens with this setting set up this way is that NO output at all is shown.
If I digit "cd src" then MANUALLY call "java App.java" it prints Hello World without that message, but obviously I don't want to manually type the compile and run commands.
There are some workarounds:
1.Add this in settings.json:
"terminal.integrated.enableFileLinks": false,
which will show less scripts before output in integrated terminal.
2.Set "console": "externalTerminal" in launch.json, you'll get a external console and no scripts displayed befor the real output:
3.Install the extension Code Runner and add the following code in settings.json:
"code-runner.clearPreviousOutput": true,
"code-runner.showExecutionMessage": false,
Install code runner extension
go to File> preferences > settings
search for "Code-runner: Run In Terminal" and enable it to accept input from scanner.
go to File> preferences > settings, then search for "Executor Map" > click edit in settings.json
and add the following modification corresponding to the programming language, java for example:
Java : "function prompt{ \">>\"} && clear && tput setaf 6 && cd $dir && javac $fileName && java $fileNameWithoutExt && echo `n && tput setaf 5",
you can check my videos for detailed explanation
https://youtu.be/WM5iW8SyGpk
https://youtu.be/yq8j3AsEOL0
The closest we can get is by clearing the terminal before code runner runs the code. To do that:
Use the Code Runner extension
In your settings.json file, change the java value in code-runner.executor map so that it'll look like this:
"code-runner.executorMap": {
"java": "cd $dir && javac $fileName && clear && java $fileNameWithoutExt"
}
some screen shots:
code runner not running
just hit the run code button
cleared console
answered questions
final output after everything

New to Java but I am having Issues

I have been doing research on why Java keeps closing immediately upon opening it. I am on Windows 10 and running on a 64 bit. I did install the correct Java download for Windows 64 bit. I have uninstalled it and installed it about four times but nothing seemed to help. I also double checked to see if it was up to date and it was at the latest version. I am unsure why this problem is occurring.
Any advice?
You should not directly run java.exe from the jdk bin path. You must do it in command prompt. The main thing is if you are not using any IDE and trying to write your code in any editor then manually run it. then you have to set java path as environment variable.
Watch this video to do that https://www.youtube.com/watch?v=l1VxWWubxFo .
Then write a simple java program , save it with .java extension.
Then open cmd there where you have written the program by holding shift key and right click on the file explorer, you will see a option like open command window here.
Then write javac <your java file name with .java extension>.
Then write java <your class name in the program>. Ignore these < > angle quotes

Run jar files - FlowDroid on mac terminal

I need to run a Java lib called FlowDroid in the Mac terminal.
I followed the documents and download the nighty build version of FlowDroid project.
After that, I use the commands that provided in the website but it does not work.
java -cp soot.jar;soot-infoflow.jar;soot-infoflow-android.jar;slf4j-api-1.7.5.jar; slf4j- simple-1.7.5.jar;axml-1.0.jar soot.jimple.infoflow.android.TestApps.Test "D:\Callbacks_Button1.apk" D:\Tools\AndroidSDK\sdk\platforms
I think it is a Java classpath problem, but I cannot figure it out. Whats wrong with the above commands?
As we don't have your exact error, that's pretty difficult to help. Anyway, on Mac OS X you should use the : as a classpath separator instead of ; which is for Windows systems.
Your command line should then look like:
java -cp soot.jar:soot-infoflow.jar:soot-infoflow-android.jar:slf4j-api-1.7.5.jar:slf4j-simple-1.7.5.jar:axml-1.0.jar soot.jimple.infoflow.android.TestApps.Test "D:\Callbacks_Button1.apk" D:\Tools\AndroidSDK\sdk\platforms

Trouble in Setting Java Environment Path on Ubuntu (libjvm.so: cannot open shared object file: No such file or directory)

Found the solution!
Ubuntu by default ignore the command "RD_LIBRARY_PATH" therefore I need to add those codes in ".bashrc" instead of "/etc/environment" or .profile.
it now work perfectly fine!
I am trying to run a program that reads Java method from C/C++ code. (I made it on Windows successfully, now, I am working on Linux)
I made it successfully compiled and run on Ubuntu, however, the problem is..
Whenever I try to run the program, I have to type following commands on terminal..
source /etc/environment
./invoke #program name is invoke
If I do not type "source /etc/environment" every time I boot my PC, the error message I get is...
error while loading shared libraries: libjvm.so: cannot open shared object file: No such file or directory
Ok. Here is what I added in /etc/environment
export PATH="$PATH:/usr/lib/jvm/jdk1.7.0_25_x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_25_x64
export LD_LIBRARY_PATH=$JAVA_HOME/jre/lib/amd64:$JAVA_HOME/jre/lib/amd64/server
What I want is that, as you know, I want to run the program without typing "source /etc/environment".
I also tried typing those PATHs on /etc/profile, but result was same... I should type "source /etc/profile" every time I reboot my PC..
How to make it permanently applied to whole range of System without typing "source /etc/environment" stuffs everytime I boot?
if you need to see source code of the program, here is downloadable address:
http://cfile237.uf.daum.net/attach/247819495212DF1C07B9EB
This worked for me:
For x86:
export LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/i386:/usr/lib/jvm/default-java/jre/lib/i386/server
For x86_64
export LD_LIBRARY_PATH=/usr/lib/jvm/default-java/jre/lib/amd64:/usr/lib/jvm/default-java/jre/lib/amd64/server
I had the same problem of setting the path(permanently) however I was using Red Hat, If you look change PATH permanently on Ubuntu it might help you. There was a file on Red Hat .profile that once you add your exports statements, they will get loaded when the computer starts. :) There should be the kind of file for ubuntu
export PATH="$PATH:/usr/lib/jvm/jdk1.7.0_25_x64/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games"
export JAVA_HOME=/usr/lib/jvm/jdk1.7.0_25_x64
Put "-Djava.library.path=FULL_PATH_HERE/jre/lib/amd64" as a JVM option in your C code.
Edit the full_path_here part.
The path is allowed to be relative, but I don't think you can use $JAVA_HOME as a placeholder for the environment variable.
Found the solution!
Ubuntu by default ignore the command "RD_LIBRARY_PATH" therefore I need to add those codes in ".bashrc" instead of "/etc/environment" or .profile.
it now work perfectly fine!
add the path of the missing libraries in the file /etc/ld.so.conf
execute the following command: ldconfig
this way, you'll add the new libraries to the default ones.
To check whether the new libraries have been linked,
execute the following command: ldconfig -p
to have the list of libraries currently linked by default.

Sublime Text 2 build system to compile & run Java in a new Terminal/Command Prompt window?

I would like to make a build system in Sublime Text 2 that will compile a Java file, and then run it in a new Terminal (for OS X or Linux) or Command Prompt (for Windows) window.
The reason for this is because Sublime Text 2 doesn't allow users to input anything, so any programs requiring input will spit out an error when running inside Sublime Text 2, like this:
This is what I currently have (I've also tried a batch file), but it simply runs inside Sublime Text 2, as opposed to in a new shell:
Is this possible? If so, please explain, step-by-step (I'm a noob at Sublime Text 2), how to do it; I've already tried posting on the Sublime Text 2 forums, and so far no luck! I'd be inexpressibly grateful. Thanks for your time!
Here's the "polite" (read: short and readable) version of what I did to make this work.
This is a starting point only. Full impl is a blog post, not an answer.
Assumes: OS X, xterm, no package hierarchy, etc.
Package/project stuff is relatively straight-forward, but IMO awkward.
I don't have a complete solution that's cross-OS or that takes weird directories into account.
My real version makes some assumptions that may or may not work for the rest of the world.
My real version uses Ant or Maven, which solves many problems, but not all.
Some of this can be wrapped up in the sublime-build file, but…
…for me it's easier this way because of other stuff not shown here.
Nutshell (Simplification): compile and run through a shell script in order to get a new window.
Script
cd $1
/usr/bin/javac $2
/usr/X11/bin/xterm -e "/bin/bash -c \"/usr/bin/java $3; echo 'Press ENTER to quit...'; read line\""
JavaC.sublime-build
{
"cmd": ["~/bin/run-java.sh $file_path $file $file_base_name"],
"file_regex": "^(...*?):([0-9]*):?([0-9]*)",
"path": "/usr/bin/java",
"selector": "source.java",
"shell": true
}
In real life it's a bit more complex.
All this said, I never really do anything with console input in Java proper; I do it via either a Groovy or JRuby REPL, or allow stubbing of input/output sources/destinations, or… but not in Java, and not from Sublime Text 2–I use an IDE for Java development. Anything else is a waste of my time, even for short, experimental stuff.
I didn't have to use all these long methods. Well not for big projects. An example build system is
{
"cmd": ["javac '$realpath$file' && java $file_base_name && rm *.class"],
"selector": "source.java",
"shell": true,
"variants": [
{
"name": "JavaDoc",
"cmd": ["mkdir documentation && javadoc -d documentation *.java"]
},
{
"name": "JAR",
"cmd": ["javac '$realpath$file' && echo \"Main-Class: $file_base_name\" > Manifest.txt && jar cfm $file_base_name.jar Manifest.txt *.class && rm *.class && java -jar $file_base_name.jar"]
},
]
}
This works for me on Linux and can be downloaded on Github at Java,sublime-build
The interesting thing is that it also compile files to JAR. Remove classes after compilation to make things neater and it also support generating JavaDocs.
The only limitation is that it cannot accept user input or arguments at compile time. You would have to do that manually in the terminal.
This might help you but it only works for Linux at the moment, I am still working on a Windows version . I've made a bash script for running java in sublime text 2 and 3. This script allows you to use package hierarchy but is not required.
It can be downloaded on github using this link: https://github.com/dannyvantol/JPack
If you want to pass argument you need to install GLUE for sublime text using package manage.
I don't know if the linux version works on windows with Cygwin install, But you can try it.
All the informatie you need to know how to use it can also be found on github.
I hope this was helpful for you

Categories