Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
699 views
in Technique[技术] by (71.8m points)

eclipse - retrieving default values defined in Java net.properties file

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Summary of conclusions

  1. The file ${java.home}/lib/net.properties does not get loaded into the System Properties (System::getProperties).
  2. 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.
  3. 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.
  4. 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.
  5. 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:

  1. jre/lib/net.properties as the default
  2. From your System's internet settings (Control Panel etc) when java.net.useSystemProxies is set to true.
  3. From the Java Configuration, as set in your Java Control panel, and when ran as an Applet, and potentially inherited from your browser settings.
  4. 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:

  1. 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.
  2. 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.
  3. 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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...