4

Hullo,

I have a Java program, with command line interface. It is used on Linux and Windows. The Java code is portable, and I want it to remain portable.

My Java source files are in Unicode — which is good. In them, I have lines like this :

System.err.println("Paramètre manquant. … ");

I use Eclipse to package the program as a JAR archive.

Then, the program is run by a command like this :

java -jar MyProgram.jar parameters

In the Windows XP command line, this gives :

ParamÞtre manquant. … 

Is there a portable way to write strings with accents in the Java program, so that they appear correctly in the Windows command line ? Or do we just have to live with Windows stupidly replacing accented E with icelandic thorn ?

I use Java 6.

Nicolas Barbulesco
  • 1,789
  • 3
  • 15
  • 20
  • Are you specifying the right `-encoding` option while compiling your code? See the [javac documentation](http://docs.oracle.com/javase/1.5.0/docs/tooldocs/windows/javac.html) for details. – mthmulders Apr 12 '13 at 10:48
  • @mthmulders — Good idea, but Eclipse does not provide a way to do that. I choose Export… → Java → Runnable JAR file. – Nicolas Barbulesco Apr 12 '13 at 12:04

3 Answers3

6

In Java 1.6 you can use System.console() instead of System.out.println() to display accentuated characters to console.

public class Test2 {
  public static void main(String args[]){
   String s = "caractères français :  à é \u00e9"; // Unicode for "é"
   System.out.println(s);
   System.console().writer().println(s);
  }
}

and the output is

C:\temp>java Test
caractþres franþais :  Ó Ú Ú
caractères français :  à é é
sideshowbarker
  • 81,827
  • 26
  • 193
  • 197
RealHowTo
  • 34,977
  • 11
  • 70
  • 85
  • One thing to point out is, that typical Java IDEs (at least Eclipse and IntelliJ IDEA) will throw NullPointerException, since System.console() is undefined when you work directly from IDE. The method is correct, anyway. – Paweł Dyda Apr 12 '13 at 16:50
  • According to the Javadoc, this does not work in some situations, for example when the program is started by a job scheduler. – Nicolas Barbulesco Jan 26 '15 at 15:04
3

Use unicode escaped sequence: \u00E8

System.err.println("Param\u00E8tre manquant. … ");

Here's an useful Unicode character table.

Lundin
  • 195,001
  • 40
  • 254
  • 396
Alepac
  • 1,833
  • 13
  • 24
  • Thank you, but this does not solve the problem, I get the same (bad) result in the Windows command prompt. – Nicolas Barbulesco Apr 12 '13 at 12:43
  • Whats the default charset of the system wwhere you run the code (`java.nio.charset.Charset.defaultCharset()`)? – Alepac Apr 12 '13 at 13:00
  • Alepac — I don't know, and I don't want to know. I want a **portable** development. There is no such thing as “the system where I run the code”. The program is run on Linux and Windows systems. – Nicolas Barbulesco Apr 12 '13 at 15:28
  • yes I agree. My solution is tipically portable but probably there is a problem on the environment you use – Alepac Apr 12 '13 at 15:31
1

Try using \u<XXXX> when encoding unicode characters. It won't look pretty in code, but it will work and be portable.

For instance:

String specialCharacters= "\u00E1 \u00E9 \u00ED \u00F3 \u00FA";
System.out.println(specialCharacters); // This will print á é í ó ú

Check Alepac's answer for a table of unicode characters.

fcm
  • 6,305
  • 5
  • 24
  • 37
  • No, it does not work. I get the same (bad) result in the Windows command prompt. – Nicolas Barbulesco Apr 12 '13 at 12:44
  • @Nicolas: That's because Windows console use different character encoding. And in most cases it is not even the same as Windows so-called code page, it is a DOS code page. `Console` class takes care about character encoding for you, `System.out` does not. – Paweł Dyda Apr 13 '13 at 10:09