1

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?

Community
  • 1
  • 1
Nikolay Kuznetsov
  • 9,467
  • 12
  • 55
  • 101

2 Answers2

1

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.

schippi
  • 1,034
  • 5
  • 15
  • Do you mean 2 instances of vm? I have always believed that there is only one instance of JVM in the system. – Nikolay Kuznetsov Nov 29 '12 at 08:38
  • i dont know the exact inner workings of the jvm but you start 2 processes. each loads its own properties. you dont have interprocess communication. it wouldnt make any sense that your project interferes with other programs like for example JDownloader – schippi Nov 29 '12 at 08:45
1

Just like you open an IE browser, it goes to homepage at first. If you visit another website, then you open another IE, it will still be the homepage. JVMs are quite similar. 2 different processes of java program use different JVMs. It means when the program ends, the file-encoding property will be default again.

Wang Wei
  • 573
  • 1
  • 5
  • 10