博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
如何设置JVM使用的代理
阅读量:2288 次
发布时间:2019-05-09

本文共 4660 字,大约阅读时间需要 15 分钟。

本文翻译自:

Many times, a Java app needs to connect to the Internet. 很多时候,Java应用程序需要连接到Internet。 The most common example happens when it is reading an XML file and needs to download its schema. 最常见的示例是在读取XML文件并需要下载其架构时发生的。

I am behind a proxy server. 我在代理服务器后面。 How can I set my JVM to use the proxy ? 如何设置我的JVM使用代理?


#1楼

参考:


#2楼

To use the system proxy setup: 要使用系统代理设置:

java -Djava.net.useSystemProxies=true ...

Or programatically: 或以编程方式:

System.setProperty("java.net.useSystemProxies", "true");

Source: 来源: :


#3楼

From the Java documentation ( not the javadoc API): 从Java文档( 不是 javadoc API):

Set the JVM flags http.proxyHost and http.proxyPort when starting your JVM on the command line. 在命令行上启动JVM时,设置JVM标志http.proxyHosthttp.proxyPort This is usually done in a shell script (in Unix) or bat file (in Windows). 这通常是在Shell脚本(在Unix中)或bat文件(在Windows中)中完成的。 Here's the example with the Unix shell script: 这是Unix shell脚本的示例:

JAVA_FLAGS=-Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800java ${JAVA_FLAGS} ...

When using containers such as JBoss or WebLogic, my solution is to edit the start-up scripts supplied by the vendor. 当使用诸如JBoss或WebLogic之类的容器时,我的解决方案是编辑供应商提供的启动脚本。

Many developers are familiar with the Java API (javadocs), but many times the rest of the documentation is overlooked. 许多开发人员都熟悉Java API(javadocs),但是很多其他文档却被忽略了。 It contains a lot of interesting information: 它包含许多有趣的信息: :


Update : If you do not want to use proxy to resolve some local/intranet hosts, check out the comment from @Tomalak: 更新:如果您不想使用代理来解析某些本地/内联网主机,请查看@Tomalak中的注释:

Also don't forget the http.nonProxyHosts property! 另外,不要忘记http.nonProxyHosts属性!

-Dhttp.nonProxyHosts="localhost|127.0.0.1|10.*.*.*|*.foo.com‌​|etc"

#4楼

You can set those flags programmatically this way: 您可以通过以下方式以编程方式设置这些标志:

if (needsProxy()) {    System.setProperty("http.proxyHost",getProxyHost());    System.setProperty("http.proxyPort",getProxyPort());} else {    System.setProperty("http.proxyHost","");    System.setProperty("http.proxyPort","");}

Just return the right values from the methods needsProxy() , getProxyHost() and getProxyPort() and you can call this code snippet whenever you want. 只需从needsProxy()getProxyHost()getProxyPort()方法中返回正确的值,就可以随时调用此代码段。


#5楼

reading an XML file and needs to download its schema 读取XML文件并需要下载其架构

If you are counting on retrieving schemas or DTDs over the internet, you're building a slow, chatty, fragile application. 如果您指望通过Internet检索模式或DTD,那么您正在构建一个缓慢,健谈,易碎的应用程序。 What happens when that remote server hosting the file takes planned or unplanned downtime? 当托管文件的远程服务器发生计划内或计划外停机时,会发生什么情况? Your app breaks. 您的应用中断了。 Is that OK? 这可以吗?

See 参见

URL's for schemas and the like are best thought of as unique identifiers. 最好将模式等的URL视为唯一标识符。 Not as requests to actually access that file remotely. 并非要求实际远程访问该文件。 Do some google searching on "XML catalog". 做一些谷歌搜索“ XML目录”。 An XML catalog allows you to host such resources locally, resolving the slowness, chattiness and fragility. XML目录使您可以在本地托管此类资源,从而解决了速度慢,聊天混乱和脆弱的问题。

It's basically a permanently cached copy of the remote content. 它基本上是远程内容的永久缓存副本。 And that's OK, since the remote content will never change. 没关系,因为远程内容永远不会改变。 If there's ever an update, it'd be at a different URL. 如果有更新,则将使用其他URL。 Making the actual retrieval of the resource over the internet especially silly. 使得通过Internet进行资源的实际检索特别愚蠢。


#6楼

You can set some properties about the proxy server as jvm parameters 您可以将有关代理服务器的某些属性设置为jvm参数

-Dhttp.proxyPort=8080, proxyHost, etc. -Dhttp.proxyPort = 8080,proxyHost等

but if you need pass through an authenticating proxy, you need an authenticator like this example: 但是如果您需要通过身份验证代理,则需要像以下示例一样的身份验证器:

ProxyAuthenticator.java ProxyAuthenticator.java

import java.net.*;import java.io.*;public class ProxyAuthenticator extends Authenticator {    private String userName, password;    protected PasswordAuthentication getPasswordAuthentication() {        return new PasswordAuthentication(userName, password.toCharArray());    }    public ProxyAuthenticator(String userName, String password) {        this.userName = userName;        this.password = password;    }}

Example.java 范例.java

import java.net.Authenticator;    import ProxyAuthenticator;public class Example {    public static void main(String[] args) {        String username = System.getProperty("proxy.authentication.username");        String password = System.getProperty("proxy.authentication.password");                if (username != null && !username.equals("")) {            Authenticator.setDefault(new ProxyAuthenticator(username, password));        }                // here your JVM will be authenticated    }}

Based on this reply: 根据此回复: :

转载地址:http://yzjnb.baihongyu.com/

你可能感兴趣的文章
深入理解设计模式(设计原则+种设计模式+设计模式PK+设计模式混编)
查看>>
谷歌大佬回国发展,吊打各大厂面试官!吐血总结大厂面试高频点及笔记解析
查看>>
面试复盘:面完字节、美团、阿里等大厂,今年面试到底问什么?
查看>>
从0到1,决战Spring Boot《Spring Boot 2实战之旅》
查看>>
5面终于拿到字节跳动offer!忍不住和大家分享一波
查看>>
拿到阿里、字节offer后。我总结了一线大厂Java面试重难点:Java基础+并发+JVM+算法+框架+分布式+架构设计
查看>>
金九银十已过 成功入职美团,面试回顾及个人总结:算法+框架+Redis+分布式+JVM
查看>>
香!阿里P8手写3份满级“并发编程”笔记,原理→精通→实战
查看>>
五面美团后,我总结出美团面试四大难题:JVM+微服务+MySQL+Redis
查看>>
滴滴Java后台3面题目:网络+内存溢出+各种锁+高性能+消息队列
查看>>
大厂面试果然名不虚传,蚂蚁三面凉经,真的是“太难了”
查看>>
分享一次止于三面的阿里面试之旅,是我不配呀
查看>>
美团工作7年,精华全在这份学习笔记里了,已成功帮助多位朋友拿到5个大厂Offer
查看>>
淘宝架构师又出神作,Java异步编程实战笔记总结,彻底被征服
查看>>
深入OAuth2核心源码,阿里大佬的Spring Security手册惊呆我了
查看>>
普本毕业,阿里五面成功斩下offer,名校出身的我究竟输在哪?
查看>>
最新“美团+字节+腾讯”三面面经,你能撑到哪一面?
查看>>
三年Java开发,年底跳槽挂了阿里和字节,却收获美团offer,分享大厂面试心得
查看>>
4面全过之后还有交叉面,阿里面试也太“刺激”了
查看>>
手慢无!出自美团内部的精通Java并发编程手册,打倒高并发
查看>>