Why can't System.setProperty() change the classpath at runtime?

前端 未结 5 1222
野性不改
野性不改 2020-12-02 12:28

I am refering to the question on changing the classpath programmatically.

I read and found out that there is some function under System class as get

5条回答
  •  时光取名叫无心
    2020-12-02 13:17

    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.

提交回复
热议问题