11

I am having problem with the output file from a program using eclipse .i set my eclipse to UTF-8 and with

 System.getProperty("file.encoding") 

i get UTF-8.i ran my prog via eclipse run-option and the output (a text file) is encoded in UTF-8.but when i compressed the source code into a jar file,the output file shows error in some of the alphabet like Ã.what is with this diff when ruuning the prog in eclipse and frm jar file?and do i have to specify the output to be encoded in utf-8 in my source code?pls help.

help from @dacwe indeed produced the desired output.but may i know how can i run my executable .jar file outside command line?how can the -Dfile.encoding=UTF-8

@dacwe :i tried changing my source code into

 BufferedWriter bout  = new java.io.BufferedWriter(new java.io.OutputStreamWriter(
                new java.io.FileOutputStream(filename), "UTF-8"));

but the output still is not encoded correctly.anything i miss here?

reukEN11
  • 121
  • 1
  • 2
  • 9
  • possible duplicate of [Setting the default Java character encoding?](http://stackoverflow.com/questions/361975/setting-the-default-java-character-encoding) – dacwe Jul 18 '11 at 12:44
  • 1
    Don't do that! Stop setting environment variables or system properties to set the encoding to use. All relevant classes in Java have constructors that take the encoding. Specify the correct encoding there and you will never have to rely on external configuration again. – Joachim Sauer Jul 18 '11 at 12:45
  • @dacwe i did not mention any use of System.setProperty("file.encoding", "UTF-8") in my source code.so running my code via eclipse run always produce the desired result(utf-8 encoded file) but not in .jar file. – reukEN11 Jul 18 '11 at 12:47
  • @reukEN11: The answer in that question may apply anyway? Start your jar with `-Dfile.encoding=UTF8`. – dacwe Jul 18 '11 at 12:51

2 Answers2

16

After some major discussion in @Dave G's answer!

Using java -Dfile.encoding=UTF-8 -jar your-jar-file.jar works.

Updating your code as @Dave G suggested (and your edit) should work.

  • Have you really repackaged your jar?
  • Do you call close() on bout? (e.g. maybe your file isn't updated)

Here is a full example that might get you going:

public static void main(String... args) throws Exception {
    PrintWriter out = new PrintWriter(new File("hello.txt"), "UTF-8");
    out.print("written in utf-8");
    out.close();
}
dacwe
  • 43,066
  • 12
  • 116
  • 140
  • i repackaged the.jar file after each changes in my source code to determine the output and also bout.close() was already added.none that work except when using console.and whats the problem with my BufferedWriter? – reukEN11 Jul 18 '11 at 14:32
  • @reukEN11: No problem with your `BufferedWriter`. I just wanted to write a concise example. If you read files you need to use the correct encoding there aswell. – dacwe Jul 18 '11 at 14:44
  • @dacwei see.so i did changed the mybufferedwriter to specifically include the char set.repackaged and run,but the prob still exists.will keep trying. – reukEN11 Jul 18 '11 at 14:55
1

When you run from a JAR file are you setting the file.encoding property by -Dfile.encoding? If not, you can either

a) open the stream explicitly with that encoding. for this you will have to create an OutputStream and then wrap that in an OutputStreamWriter explicitly indicating the character encoding.

or

b) set the property as the first thing in your main method using System.setProperty("file.endcoding");

note @dacwe pointed out something I forgot ... corrected my answer.

Dave G
  • 9,639
  • 36
  • 41
  • b) is not correct. You must set the `file.encoding` before the JVM starts up (it is cached by for example `String` and `OutputStreamWriter`..). – dacwe Jul 18 '11 at 12:57
  • @dacwe i use export as .jar file from eclipse to compress my source code.how exactly can i set up this property?i look into eclipse and it has .ini file,with -Dfile.encoding=UTF-8 at the last line(manually from tutorial to set ide in utf). – reukEN11 Jul 18 '11 at 13:03
  • 1
    @reukEN11: *If you do not use your source code in the program it has nothing to to with the source.* Start your application from a shell with: `java -Dfile.encoding=UTF-8 -jar your-jar-file.jar`. – dacwe Jul 18 '11 at 13:32
  • @dacwe desired result indeed.can this -Dfile.encoding=UTF-8 be embedded into jar file when running executable jar ie prefer running outside command line.and i tried using – reukEN11 Jul 18 '11 at 13:45
  • @reukEN11: Use the answer that Dave G provided. When constructing your `OutputStreamWriter` you provide the encoding (second argument). – dacwe Jul 18 '11 at 13:49