简体   繁体   English

谁将 JVM file.encoding 重置为原始值?

[英]Who resets JVM file.encoding back to original?

System.setProperty("file.encoding", "utf-8"); 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.下面的评论暗示 file.encoding 将针对在同一 JVM 上运行的所有应用程序更改,但是,我没有观察到这种行为。

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.以编程方式设置系统属性将影响在同一 JVM 中运行的所有代码,这是危险的,尤其是在讨论此类低级系统属性时。

I have read this question and understand that there are many issues with caching and Java 1.5我已阅读此问题并了解缓存和 Java 1.5 存在很多问题
Setting the default Java character encoding? 设置默认的 Java 字符编码?

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.然后我使用 Eclipse 和在项目配置中设置的 Java 1.6 创建一个 jar 文件。
Then I run jar-file with Java 1.7 and all this happens under Windows 7.然后我用 Java 1.7 运行 jar 文件,所有这些都发生在 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 ?那么是谁以及为什么将file.encoding的值重置回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 ? UPD:任何人都可以解释或提供一个链接,该链接逐步解释当我键入java -jar MyClass.jar时,JVM 进程会发生什么?

you started 2 vm's.你开始了2个虚拟机。 one with each "java -jar" command.一个带有每个“java -jar”命令。

you can change the encoding your projects uses by editing the project properties in eclipse.您可以通过在 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.这就是评论的意思。

Just like you open an IE browser, it goes to homepage at first.就像你打开一个IE浏览器一样,它首先会进入主页。 If you visit another website, then you open another IE, it will still be the homepage.如果您访问另一个网站,那么您打开另一个 IE,它仍然是主页。 JVMs are quite similar. JVM 非常相似。 2 different processes of java program use different JVMs. 2个不同的java程序进程使用不同的JVM。 It means when the program ends, the file-encoding property will be default again.这意味着当程序结束时,文件编码属性将再次默认。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM