Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I set the proxy to be used by the JVM

Tags:

java

proxy

jvm

Many times, a Java app needs to connect to the Internet. The most common example happens when it is reading an XML file and needs to download its schema.

I am behind a proxy server. How can I set my JVM to use the proxy ?

like image 565
Leonel Avatar asked Sep 23 '08 12:09

Leonel


People also ask

What is proxy server Java?

Introduction. Proxy servers act as intermediaries between client applications and other servers. In an enterprise setting, we often use them to help provide control over the content that users consume, usually across network boundaries. In this tutorial, we'll look at how to connect through proxy servers in Java.

How do I manually set a proxy?

To set up a proxy server connection manuallySelect the Start button, then select Settings > Network & Internet > Proxy. Under Manual proxy setup, turn on Use a proxy server. Do the following: In the Address and Port boxes, enter the proxy server name or IP address and port (optional) in the respective boxes.


2 Answers

From the Java documentation (not the javadoc API):

http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

Set the JVM flags http.proxyHost and http.proxyPort when starting your JVM on the command line. This is usually done in a shell script (in Unix) or bat file (in Windows). Here's the example with the Unix shell script:

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

When using containers such as JBoss or WebLogic, my solution is to edit the start-up scripts supplied by the vendor.

Many developers are familiar with the Java API (javadocs), but many times the rest of the documentation is overlooked. It contains a lot of interesting information: http://download.oracle.com/javase/6/docs/technotes/guides/


Update : If you do not want to use proxy to resolve some local/intranet hosts, check out the comment from @Tomalak:

Also don't forget the http.nonProxyHosts property!

-Dhttp.nonProxyHosts="localhost|127.0.0.1|10.*.*.*|*.foo.com‌​|etc" 
like image 122
Leonel Avatar answered Sep 22 '22 14:09

Leonel


To use the system proxy setup:

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

Or programatically:

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

Source: http://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html

like image 33
gr5 Avatar answered Sep 20 '22 14:09

gr5