I want to create a UTF-8 file from a jar-file with the following code:
public class UTF8 {
public static void main(String[] args) throws Exception {
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(
"utf8.txt"), "UTF8"));
out.write("Look at this BASTÖÖÜÜÄÄ!");
}
}
This code works very well using eclipse. But if I pack it to a jar file, it uses a platform depended file encoding (Cp1252 for example on Windows). What could I do against that?
In the JVM options field on the JRE tab in Launch4j (after loading your configuration file) enter the following:
-Dfile.encoding=UTF8
ALternatively, can specify the same thing in an ini file for your application - see the launch4j documentation at:
http://launch4j.sourceforge.net/docs.html#Additional_jvm_options
"UTF8"
should be "UTF-8"
.
Also OutputStreamReader(outputStream, encosingString)
throws UnsupportedEncodingException
which you should catch.
Major mistake: never catch (or pass to main) a generic Exception
public static void main(String[] args) throws Exception {}
This prevented you to see what your real exception was and where happened.
The rule of thumb when debugging encoding problems: if you have non-us-ascii letters in string literals, try to replace them by Unicode escapes (ie BAST\Ö\Ö\Ü\Ü\Ä\Ä
). If the problem disappears, you have a source/compiler encoding mismatch.
For example, the behaviour you describe may happen if your source encoding is Windows-1252 whereas compiler encoding is UTF-8.
i faced the same problem .. and i solved it with java -Dfile.encoding=UTF8 -jar CAP.jar ( where CAP the name of the JAR File)
I mad a batch file. open text file, write the line above and save it as .bat and it should work
尝试使用"UTF-8"
。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.