Well, here is the problem, I have started using VScode, and I can't read from console cyrillic characters.
My code:
import java.util.Scanner;
class App {
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in, "UTF-8");
String word = input.nextLine();
System.out.println(word);
}
}
Now when I enter any cyrillic string it will print empty string back to me. If i write something like
System.out.println("Привет"); //cyrillic symbols
It will print "Привет", which is fine. So I am guessing it has something to do with reading the string rather than outputing it.
chcp command gives Active code page: 65001
I have tried setting encoding and without it, but it doesn't seem to work, is there something I missed?
Thanks in advance
I've tested the code on my machine and got the same result: nothing shown;
You can see, when run it in external Window PowerShell or Command Prompt, the result is different but still not shown correctly:
When we change the encode style to GBK(936), the cyrillic characters can be displayed correctly:
When it comes to changing integrated terminal encoding style in vscode and execute code again, it still shows nothing:
About these different results between external Command Prompt and integrated terminal in VS Code, I've put a github request. And I'm doing some research, if any useful imformation i get, i will update you.
Related
I met some interesting question on which I was not able to find an answer. Does anybody know how to pass hieroglyphs as an argument to the main method from command line?
Below there is some pseudo code which will help to test suggested solution:
public class Test {
public static void main(String args[]) {
if ("香港政府".equals(args[0])) {
System.out.println("Match");
}
}
}
So question is how to call Test.class with an argument so the application prints Match to the console? args[0] can be transformed before passing to the if statement.
Thanks in advance.
After some additional research I was able kinda figured it out. So guys who commented on question were very near to the answer.
Encoding which I tried to find was 936. But it doesn't mean that you will be able to run chcp 936 if you OS locale is other than chinese. Once you will try to run it on other locale than chinese:
chcp 936
You will get following error:
Invalide code page
For making it working you have to change region. FOllowing steps will be needed:
Start - COntrol panel
Select "Region and Language"
Select "Administrative" and click "Change system locale..."
Select "Chinese (Simplified, PRC)" and reboot laptop
After restart when you will run chcp you will see following output Active code page: 936. Now you are ready to execute command line with hieroglyphs.
I was trying to change the color of console output in java.As my program output displays a warning message so i thought to change that warning color.
After searching it over stackoverflow and response for similar questions I came accross Jansi and JLibs but they are not working.This is the sample code which i wrote using jansi.I included the jar in class-path
import static org.fusesource.jansi.Ansi.*;
import static org.fusesource.jansi.Ansi.Color.*;
class Test
{
public static void main(String[] args)
{
System.out.println( ansi().eraseScreen().fg(RED).a("Hello").fg(GREEN).a(" World").reset());
}
}
However its doing nothing just printing this: ←[2J←[31mHello←[32m World←[m
I am using windows 7 and jdk 1.7 and i am not using eclipse.
Any help
from Jansi
Using jansi is easy. Before you start sending ANSI escape sequences to
System.out make sure you run: AnsiConsole.systemInstall();
I've the following simply program that prints the IPA word ˈabsəluːt [1]. Unfortunately executing this program with mvn:exec prints the word as ?abs?lu?t[2]. How to make it print it correctly, i.e. as in [1]?
package dp4j.encodingtest;
public class App {
public static void main(String[] args) {
String s = "ˈabsəluːt";
System.out.println(s);
FileUtils.writeStringToFile(new File("s.txt"), s);
}
}
The mvn exec:exec command:
mvn "-Dexec.args=-classpath %classpath dp4j.encodingtest.App"
-Dexec.executable=C:\\jdk1.7.0_25\\bin\\java.exe exec:exec
Even printing the word to the s.txt file doesn't print it is incorrectly as in [2].
The issue is with the application where that word is displayed/printed, in this case, the console. Nothing can be done from java / maven except to make sure your java source code encoding is UTF-8 (as string literal is used).
If you are running it from an IDE, then try to change the console font from IDE's options/preferences to ‘Lucida Sans’ as this font has partial support for IPA extensions or some other available fonts with IPA support.
I do have some serious troubles understanding the console in java. I am running Eclipse, and I wanted to write a small program which prompts a few text messages to the console and receives a few strings as input arguments from it. Problem is: When I run my program, it opens the command line window properly, but my outputs are only printed on the Eclipse-Console.
In some way, I do understand why this is the case. The Command Line Windows expects commands, and not just some kind of a string or something. But how do i manage to output my Strings into the Command Line Window and read Strings from it, and not just commands.
Or am I doing it the wrong way? Do I have to open another "Console" where all my messages will be prompted and from which i can read strings a user wrote?
This is the code i use to open a command line window on start:
public static void main(String args[]) throws Exception {
Process process = new ProcessBuilder(new String[] { "cmd", "/C",
"start", "cmd" }).start();
System.out.println(process.waitFor());
Edit: I did still not manage to get this to working. Somehow, when I compiled the program, and I run it, it properly opens a command window, but no messages are posted there. Seems like "System.out.println("xxx") does not have any effect on this window.
There's no "console" specified by your program, but an stdin, stdout and stderr for input, output and error output. When you run your program from windows, these streams are bound to a command window, and if you run it in eclipse, they will be associated to the eclipse console. To give a more obscure example, ff you were running it through ssh, the streams would be associated to ssh, and ssh associated to your command window, and so on.
So, you're not doing anything wrong, you just need to run the program from the command line if you want stdout and stdin to come from that command window.
How do you open a command window, by the way?
You might want to read through this page:
http://pages.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/JavaIO_Scanner.html
Basically what you need to is create the input stream, tell the user to input something, and then get the input. E.g.
private static Scanner newScanner = new Scanner(System.in);
public static void main(String[] args) {
System.out.println("Put your input here: ");
String inputValue = newScanner.nextLine();
System.out.println(inputValue);
}
Just remember to import the scanner library!
This simple code has never worked for me in processing:
try {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while (str != null) {
System.out.print("> prompt ");
str = in.readLine();
println(str);
}
}
catch (IOException e) {
}
Probably because the console output box cannot be used for input, unlike in Eclipse. Is there a simple workaround, or am I forced to do something like a dialog box (or keyPressed handling) for standard in?
If you are using the Processing IDE, Processing does not support this behavior natively. If you export your sketch and edit the java files, or use Eclipse, Proclipsing, core.jar, etc. you can access the System.in like any other java application, however, this would defeat the purpose of processing in that it doesn't typically run from the command line and is graphic in nature.
Best practice would be to capture the keystokes with the key pressed method. For example:
String str = "";
void keyPressed() {
str += key;
}
then in your draw() loop/method, you could handle the text input on str and clear it out if you wanted.
If you wanted something more sophisticated that would have a better UX, I suggest using something like ControlP5's TextField or TextArea.
Your program works perfectly (I named it test) and exported it as an Applet. I tested with cygwin as well as the windows command prompt:
$ cd test/applet
$ java -jar test.jar
Output (I typed "hello" and hit enter):
prompt> hello
hello
prompt>
I tried really quick on an Ubuntu terminal through ssh. I had issues getting it connected to the x11 server. Consider: http://en.wikipedia.org/wiki/Xvfb if that is an issue.
Just to confirm, I was able to run the SharedCanvasServer example included in Library->Network where I added a System.out.println to dump debug to the executing terminal.
java -cp "core.jar;net.jar;SharedCanvasServer.jar" SharedCanvasServer