If I understand Java Networking and Proxies correctly, the jre/lib/net.properties
file contains default values that are populated into the system properties at runtime. My net.properties
file contains the following, among other things:
http.nonProxyHosts=localhost|127.*|[::1]
But when I run my Java application inside Eclipse 4.5, System.getProperty("http.nonProxyHosts")
returns null
. I have not defined or overridden this value anywhere within my application.
How am I able to retrieve the value of http.nonProxyHosts
defined in jre/lib/net.properties
at runtime?
Summary of conclusions
${java.home}/lib/net.properties
does not get loaded into the System Properties (System::getProperties
).System.setProperty
, or setting at the command line, for example using-Dhttp.proxyHost=proxy
syntax, will override the the defined property, but also anyjava.net.useSystemProxies
setting even if set totrue
.net.properties
file by loading it manually as properties usingProperties::load
. The exact location is in the Java Home directory, which can be retrieved using thejava.home
System Property, within thelib
subdirectory.java.net.useSystemProxies=true
, you set the proxy used from the control panel, under 'Internet Properties'. From those settings you will need to click on the 'LAN settings' button.Research
I have replicated your example in my environment, using netBeans actually, with this simple example:
I print the
java.home
system property to make sure I am editing the correctjre/lib/net.properties
.however the two properties
http.nonProxyHosts
andjava.net.useSystemProxies
print as null, while I can clearly see that both values are set in thenet.properties
file:Actually the documentation is a little unclear, but it seems that proxy configuration can be done in one of several ways:
jre/lib/net.properties
as the defaultjava.net.useSystemProxies
is set to true.System.properties
It appears to me that this is a cusom feature of the java.net api's, and will read the net.properties only in case the system properties are not set explicitly. I suspect that does not mean that the
net.properties
file is used to set system properties, but are only read by the java.net api's themselves.Note also this text within the default installed
net.properties
file:It only says that these values will be used, nothing specific about setting the system properties themselves.
[UPDATE]
With a small example, I was able to prove that out
You can see that the
http.proxyHost
prints as null, while with the defaultnet.properties
, the proxy for"http://www.yahoo.com"
prints asDIRECT
.DIRECT
means no proxy. This is because in thenet.properties file,
http.proxyHost` is undefined.Now I modify the
net.properties
file as follows (un-commenting and modifying the existing entry):Now when I run the same code, I get following output:
The System Property is still null, however the same setting from the
net.properties
file did take effect.Some other observations:
System.setProperty("http.proxyHost", "other.net");
right before doing theProxySelector::select
, will override the value innet.properties
.http.proxyHost
, it will still inherithttp.proxyPort
if set innet.properties
.net.properties
file, or by explicitly setting the system property in code, I sethttp.proxyHost
explicitly, this will always override the System default even whenjava.net.useSystemProxies
is set to true.java.net.useSystemProxies=true
only works when the proxy is not explicitly set, and will be otherwise ignored (I have tested and verified this).[Update 2]
If your ultimate goal is just to access the content of the net.properties file, you can try something as follows:
You figure out security and privileges!