System.setProperty(“file.encoding”, “utf-8”);
The comment below implies that file.encoding would be changed for all apps running on the same JVM, however, I don’t observe this kind of behaviour.
Setting a system property programmatically will affect all code running within the same JVM, which is hazardous, especially when discussing such a low-level system property.
I have read this question and understand that there are many issues with caching and Java 1.5
Setting the default Java character encoding?
Please, now consider the following code:
public class FileEncodingTest {
public static void main (String[] args) {
System.out.println(System.getProperty("file.encoding"));
System.setProperty("file.encoding", "UTF-8");
System.out.println(System.getProperty("file.encoding"));
}
}
Then I create a jar-file using Eclipse and Java 1.6 set in project configuration.
Then I run jar-file with Java 1.7 and all this happens under Windows 7.
java -jar FileEncodingTest.jar
Cp1251
UTF-8
java -jar FileEncodingTest.jar
Cp1251
UTF-8
So who and why resets the value of file.encoding back to Cp1251?
UPD:
Anyone can explain or provide a link which explains step-by-step what happens in terms of JVM, processes when I type java -jar MyClass.jar?
you started 2 vm’s. one with each “java -jar” command.
you can change the encoding your projects uses by editing the project properties in eclipse.
but note that when you hardcode stuff that relies on the fileformat and another project uses your implementation there will be problems. thats what the comment means.