检索 Java net.properties 文件中定义的默认值 [英] retrieving default values defined in Java net.properties file
问题描述
如果我了解 Java 网络和代理 正确,jre/lib/net.properties
文件包含在运行时填充到系统属性中的默认值.我的 net.properties
文件包含以下内容:
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]
但是当我在 Eclipse 4.5 中运行我的 Java 应用程序时,System.getProperty("http.nonProxyHosts")
返回 null
.我没有在我的应用程序中的任何地方定义或覆盖这个值.
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.
如何在运行时检索 jre/lib/net.properties
中定义的 http.nonProxyHosts
的值?
How am I able to retrieve the value of http.nonProxyHosts
defined in jre/lib/net.properties
at runtime?
推荐答案
结论总结
- 文件
${java.home}/lib/net.properties
未加载到 系统属性 (System::getProperties
). - 通过
System.setProperty
手动设置代理,或在命令行设置,例如使用-Dhttp.proxyHost=proxy
语法,将覆盖定义的属性,还有任何java.net.useSystemProxies
设置,即使设置为true
. - 您可以通过使用
Properties::load
.确切位置在 Java 主目录中,可以使用java.home
系统属性,位于lib
子目录中. - 在 Windows 桌面上,当与
java.net.useSystemProxies=true
结合使用时,您可以从控制面板的Internet 属性"下设置使用的代理.在这些设置中,您需要点击LAN 设置"按钮. - 小程序具有额外的设置级别,请参阅代理设置在 Java 文档中.
- 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 anyjava.net.useSystemProxies
setting even if set totrue
. - You can access the
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. - 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.
研究
我已经在我的环境中复制了你的例子,实际上使用了 netBeans,这个简单的例子是:
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"));
}
}
我打印了 java.home
系统属性以确保我正在编辑正确的 jre/lib/net.properties
.
I print the java.home
system property to make sure I am editing the correct jre/lib/net.properties
.
但是两个属性 http.nonProxyHosts
和 java.net.useSystemProxies
打印为空,而我可以清楚地看到这两个值都在 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
作为默认值- 当
java.net.useSystemProxies
设置为 true 时,从您系统的互联网设置(控制面板等). - 来自 Java 配置,在您的 Java 控制面板中设置,当作为小程序运行时,可能从您的浏览器设置继承.
System.properties
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
在我看来,这是 java.net api 的一个自定义功能,只有在未明确设置系统属性的情况下才会读取 net.properties.我怀疑 not 是否意味着 net.properties
文件用于设置系统属性,但只能由 java.net api 自己读取.
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.
还要注意默认安装的 net.properties
文件中的这段文字:
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.
[更新]
通过一个小例子,我能够证明这一点
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("
");
}
}
}
您可以看到 http.proxyHost
打印为 null,而使用默认的 net.properties
,"http://www.yahoo.com"
打印为 DIRECT
.DIRECT
表示没有代理.这是因为在 net.properties 文件中,
http.proxyHost` 未定义.
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.
现在我修改 net.properties
文件如下(取消注释并修改现有条目):
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
系统属性仍然为空,但是 net.properties
文件中的相同设置确实生效了.
The System Property is still null, however the same setting from the net.properties
file did take effect.
其他一些观察:
- 当如下明确设置系统属性时:
System.setProperty("http.proxyHost", "other.net");
就在执行ProxySelector::select
之前>,将覆盖net.properties
中的值. - 使用系统属性覆盖将仅影响完全相同的系统属性.也就是说,如果您只覆盖
http.proxyHost
,如果在net.properties
中设置,它仍然会继承http.proxyPort
. - 如果在任何地方,无论是在
net.properties
文件中,还是通过在代码中显式设置系统属性,我都会显式设置http.proxyHost
,这将始终覆盖即使java.net.useSystemProxies
设置为 true,系统默认也是如此.java.net.useSystemProxies=true
仅在未明确设置代理时有效,否则将被忽略(我已经测试并验证了这一点).
- When setting the system property explicitly as follows:
System.setProperty("http.proxyHost", "other.net");
right before doing theProxySelector::select
, will override the value innet.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 inherithttp.proxyPort
if set innet.properties
. - If anywhere, either in the
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).
[更新 2]
如果您的最终目标只是访问 net.properties 文件的内容,您可以尝试以下操作:
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"));
}
}
您了解安全性和权限!
这篇关于检索 Java net.properties 文件中定义的默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!