检索在Java net.properties文件中定义的默认值 [英] retrieving default values defined in Java net.properties file
问题描述
如果我了解 Java网络和代理正确地, jre / lib / 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]
但是当我运行我的Java Eclipse 4.5中的应用程序 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?
推荐答案
结论总结
Summary of conclusions
- 文件
$ {java.home} / lib / net.properties
不会加载到系统属性(System :: getProperties
)。 - 手动设置代理或者通过
System.setProperty
或在命令行中设置,例如使用-Dhttp.proxyHost = proxy
语法,将覆盖定义的属性,也可以覆盖任何java.net.useSystemProxies
设置,即使设置为true
li>
- 您可以通过手动加载它来访问
net.properties
文件使用属性::负载
。确切位置在Java Home目录中,可以使用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.
< h1>研究
我已经在我的环境中复制了你的例子,实际上使用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。首页
系统属性,以确保我正在编辑正确的 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配置中,当r一个作为一个小程序,可能继承自您的浏览器设置。
-
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的cusom功能,只有在系统属性未明确设置的情况下才会读取net.properties。我怀疑不意味着 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.
它只说
[更新]
有一个小例子,我能够证明出来
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("\n");
}
}
}
你可以看到, code> http.proxyHost 打印为空,而默认 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 Files\Java\jdk1.8.0_20\jre
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
,它仍然会继承http.proxyPort
,如果设置为net.properties
。 - 如果在
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]
如果您的最终目标只是访问网络内容。属性文件,可以尝试如下:
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屋!