检索Java net.properties文件中定义的默认值
如果我正确理解Java Networking and
Proxies,则该jre/lib/net.properties
文件包含默认值,这些默认值在运行时填充到系统属性中。我的net.properties
文件包含以下内容:
http.nonProxyHosts=localhost|127.*|[::1]
但是,当我在Eclipse
4.5中运行Java应用程序时,会System.getProperty("http.nonProxyHosts")
返回null
。
我尚未在应用程序中的任何地方定义或覆盖此值。
我如何在运行时检索http.nonProxyHosts
in 的值jre/lib/net.properties
?
回答:
结论总结
- 该文件
${java.home}/lib/net.properties
未加载到系统属性(System::getProperties
)中。 - 通过
System.setProperty
或在命令行上手动设置代理(例如使用-Dhttp.proxyHost=proxy
语法)将覆盖已定义的属性,java.net.useSystemProxies
即使设置为,也将覆盖任何设置true
。 - 您可以使用
net.properties
手动将文件作为属性加载来访问文件Properties::load
。确切位置在Java主目录中,可以使用子目录中的“java.home
系统属性” 来检索该lib
目录。 - 在Windows桌面上,当与结合使用时
java.net.useSystemProxies=true
,可以在“ Internet属性”下的控制面板中设置要使用的代理。在这些设置中,您需要单击“ LAN设置”按钮。 - 小程序具有其他级别的设置,请参阅Java文档中的“ 代理设置 ”。
研究
我已经使用以下简单示例在环境中实际使用netBeans复制了您的示例:
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
。
但是,这两个属性http.nonProxyHosts
并java.net.useSystemProxies
打印为null,而我可以清楚地看到文件中同时设置了两个值net.properties
:
java.net.useSystemProxies=falsehttp.nonProxyHosts=localhost|127.*|[::1]
实际上,文档尚不清楚,但是似乎可以通过以下几种方式之一完成代理配置:
jre/lib/net.properties
作为默认- 从系统的Internet设置(控制面板等)
java.net.useSystemProxies
设置为true时。 - 从Java配置(在Java控制面板中进行设置)以及作为Applet运行时,并有可能继承自浏览器设置。
System.properties
在我看来,这是java.net api的一种惯用功能,并且仅在未明确设置系统属性的情况下,才读取net.properties。我怀疑,确实 不
意味着该net.properties
文件是用来设置系统属性,而是由java.net API的本身是只读的。
还请注意默认安装net.properties
文件中的以下文本:
该文件可能包含网络系统属性的默认值。仅当未在命令行上指定系统属性或以编程方式设置系统属性时,才使用这些值。
它只是说将 使用 这些值,而没有关于设置系统属性本身的具体信息。
[更新]
通过一个小例子,我能够证明这一点
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");
}
}
}
您可以看到http.proxyHost
打印为空,而默认情况下net.properties
,"http://www.yahoo.com"
打印的代理为DIRECT
。DIRECT
表示没有代理。这是因为在net.properties
file,http.proxyHost中未定义。
现在,我net.properties
按如下方式修改文件(取消注释和修改现有条目):
http.proxyHost=this.is.a.test.net
现在,当我运行相同的代码时,将得到以下输出:
C:\Program Files\Java\jdk1.8.0_20\jrenull
HTTP Proxies
HTTP @ this.is.a.test.net:80
系统属性仍然为空,但是net.properties
文件中的相同设置确实生效。
其他一些观察:
- 如下明确设置系统属性时:
System.setProperty("http.proxyHost", "other.net");
在执行之前ProxySelector::select
,将覆盖中的值net.properties
。 - 使用系统属性进行覆盖 只会 影响完全相同的系统属性。也就是说,如果您仅覆盖
http.proxyHost
,则http.proxyPort
在设置时仍将继承net.properties
。 - 如果在
net.properties
文件中的任何位置,或者通过在代码中显式设置system属性,则进行显式设置,http.proxyHost
即使java.net.useSystemProxies
将其设置为true ,也将始终覆盖System默认值。java.net.useSystemProxies=true
仅在未显式设置代理的情况下有效,否则将被忽略(我已经测试并验证了这一点)。
[更新2]
如果您的最终目标只是访问net.properties文件的内容,则可以尝试以下操作:
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文件中定义的默认值 的全部内容, 来源链接: utcz.com/qa/403494.html