问题描述
我正在参考以编程方式更改类路径。
I am refering to the question on changing the classpath programmatically.
我读过并发现 System 类下有一些函数作为getproperties,我们可以在其中检索属性然后也可以使用setProperties()设置它。
I read and found out that there is some function under System class as getproperties where we can retrieve the properties and then also can set it using setProperties().
但我得到的答案是它不会工作。我自己没试过,但是,我正在接听电话。
The answers however I got was that It Wont work. I have not tried this myself, however, i am taking the call.
为了澄清,为什么这些setProperty()和getProperty()方法如果不能在运行时改变它。或者这仅仅是针对classpath属性的?
Just to clarify, then why these setProperty() and getProperty() methods are there if they cannot alter it at run time. Or is this specific to the classpath property only ?
如果有人可以提供他们真正有帮助的场景,我将不胜感激?
I will appreciate if someone can present a scenario where they are really helpful?
推荐答案
您当然可以在任何时间设置所需的任何系统属性。问题是,它会产生什么影响吗?在类路径的情况下,答案是否定的。系统类加载器在启动序列的早期点初始化。它将类路径复制到自己的数据结构中,并且不再读取classpath属性。更改它不会影响系统中的任何内容。
You can certainly set any system properties you want at any point of time. The question is, will it have any effect? In the case of classpath, the answer is NO. The system class loader is initialized at a very early point in the startup sequence. It copies the classpath into its own data structures, and the classpath property is not read again. Changing it affect nothing in the system.
原因可能是双重的。性能较差的原因。您可能需要为快速查找资源而构建某种数据结构,并且每次重新解析类路径可能效率低下。更重要的原因是安全性。你不希望流氓类改变你下面的类路径并加载另一个类的受损版本。
The reason for this may be two-fold. The lesser reason is performance. You may need to have some sort of data structure built for quick lookup of resources, and re-parsing classpath every time may be inefficient. The more important reason is security. You don't want a rogue class change the classpath under you and load compromised version of another class.
这篇关于为什么System.setProperty()不能在运行时更改类路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!