Summary of conclusions
- The file
${java.home}/lib/net.properties
does not get loaded into the System Properties (System::getProperties
).
- Setting the proxy manually either through
System.setProperty
, or setting at the command line, for example using -Dhttp.proxyHost=proxy
syntax, will override the the defined property, but also any java.net.useSystemProxies
setting even if set to true
.
- You can access the
net.properties
file by loading it manually as properties using Properties::load
. The exact location is in the Java Home directory, which can be retrieved using the java.home
System Property, within the lib
subdirectory.
- On a windows desktop, when used in combination with
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.
- An applet has an additional level of settings, see Proxy Setup in the Java documentation.
Research
I have replicated your example in my environment, using netBeans actually, with this simple example:
public class Play {
public static void main(String args[]) {
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("http.nonProxyHosts"));
System.out.println(System.getProperty("java.net.useSystemProxies"));
}
}
I print the java.home
system property to make sure I am editing the correct jre/lib/net.properties
.
however the two properties http.nonProxyHosts
and java.net.useSystemProxies
print as null, while I can clearly see that both values are set in the net.properties
file:
java.net.useSystemProxies=false
http.nonProxyHosts=localhost|127.*|[::1]
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 default
- From your System's internet settings (Control Panel etc) when
java.net.useSystemProxies
is set to true.
- From the Java Configuration, as set in your Java Control panel, and when ran as an Applet, and potentially inherited from your browser settings.
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:
This file may contain default values for the networking system
properties. These values are only used when the system properties are
not specified on the command line or set programatically.
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
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.URI;
import java.util.List;
public class Play {
public static void main(String args[]) {
System.out.println(System.getProperty("java.home"));
System.out.println(System.getProperty("http.proxyHost"));
ProxySelector ps = ProxySelector.getDefault();
List<Proxy> proxies = ps.select(URI.create("http://www.yahoo.com"));
System.out.println("HTTP Proxies");
for (Proxy p:proxies) {
System.out.append(p.toString()).append("
");
}
}
}
You can see that the http.proxyHost
prints as null, while with the default net.properties
, the proxy for "http://www.yahoo.com"
prints as DIRECT
. DIRECT
means no proxy. This is because in the net.properties file,
http.proxyHost` is undefined.
Now I modify the net.properties
file as follows (un-commenting and modifying the existing entry):
http.proxyHost=this.is.a.test.net
Now when I run the same code, I get following output:
C:Program FilesJavajdk1.8.0_20jre
null
HTTP Proxies
HTTP @ this.is.a.test.net:80
The System Property is still null, however the same setting from the net.properties
file did take effect.
Some other observations:
- When setting the system property explicitly as follows:
System.setProperty("http.proxyHost", "other.net");
right before doing the ProxySelector::select
, will override the value in net.properties
.
- Overriding using the system properties will only affect the exact same system property. That is, if you only override
http.proxyHost
, it will still inherit http.proxyPort
if set in net.properties
.
- If anywhere, either in the
net.properties
file, or by explicitly setting the system property in code, I set http.proxyHost
explicitly, this will always override the System default even when java.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:
import java.io.IOException;
import java.io.Reader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Properties;
public class Play {
public static void main(String args[]) {
Properties props = new Properties();
Path path = Paths.get(System.getProperty("java.home"), "lib", "net.properties");
try (Reader r = Files.newBufferedReader(path)) {
props.load(r);
System.out.println("props loaded!");
} catch (IOException x) {
System.err.println("props failed loading!");
x.printStackTrace(System.err);
}
// Now you have access to all the net.properties!
System.out.println(props.getProperty("http.proxyHost"));
}
}
You figure out security and privileges!