Setting Jvm/Jre to Use Windows Proxy Automatically

Setting JVM/JRE to use Windows Proxy Automatically

It is possible to detect the proxy using the ProxySelector class and assign the system proxy by assigning environment variables with the setProperty method of the System class:

System.setProperty("java.net.useSystemProxies", "true");
System.out.println("detecting proxies");
List l = null;
try {
l = ProxySelector.getDefault().select(new URI("http://foo/bar"));
}
catch (URISyntaxException e) {
e.printStackTrace();
}
if (l != null) {
for (Iterator iter = l.iterator(); iter.hasNext();) {
java.net.Proxy proxy = (java.net.Proxy) iter.next();
System.out.println("proxy type: " + proxy.type());

InetSocketAddress addr = (InetSocketAddress) proxy.address();

if (addr == null) {
System.out.println("No Proxy");
} else {
System.out.println("proxy hostname: " + addr.getHostName());
System.setProperty("http.proxyHost", addr.getHostName());
System.out.println("proxy port: " + addr.getPort());
System.setProperty("http.proxyPort", Integer.toString(addr.getPort()));
}
}
}

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

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.*.*.*|*.example.com|etc"

How to get the system HTTP proxy configuration in Java

oh, just look at that question.
there was a sample with a solution.

currently all samples I've seen use SUN's undocumented APIs (com.sum packages), which result in warnings at compile time...

System proxy setting, Java

From source code we can use

System.getProperties().put("http.proxyHost", "ProxyURL");
System.getProperties().put("http.proxyPort", "ProxyPort");
System.getProperties().put("http.proxyUser", "UserName");
System.getProperties().put("http.proxyPassword", "Password");

Command Line :

  $> java -Dhttp.proxyHost=proxyhostURL -Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=UserName -Dhttp.proxyPassword=Password ProxyClassHere

Document

Applying the system settings of SOCKS proxy in Java, programmatically

Found it:
The protocol label for HTTP is "HTTP".

HTTPS: "HTTPS"

FTP: "FTP"

SOCKS? "SOCKET"

Does Java's ProxySelector not work with automatic proxy configuration scripts?

No, the Java ProxySelector does not read Proxy Auto-Config (PAC) files.

However, as suggested by Brian de Alwis as an answer to my similar question, the Proxy Vole library appears to provide that support/capability.

To provide network connectivity out of the box for you Java
application you can use the Proxy - Vole library. It provides some
strategies for autodetecting the current proxy settings. There are
many configureable strategies to choose from. At the moment Proxy -
Vole supports the following proxy detection strategies.

  • Read platform settings (Supports: Windows, KDE, Gnome, OSX)
  • Read browser setting (Supports: Firefox 3.x, Internet Explorer; Chrome and Webkit use the platform settings)
  • Read environment variables (often used variables on Linux / Unix server systems)
  • Autodetection script by using WPAD/PAC (Not all variations supported)

How do I configure proxy settings for Java in Solaris to handle Proxy Auto Config (PAC) scripts?

Sadly the system proxy selector does not handle PAC/WPAD/JS specifications, confirmed by my testing with Windows or MacOS X. Even on Java6, which includes a JavaScript interpreter.

I'm hoping proxy-vole http://code.google.com/p/proxy-vole/ may do the trick.

How to configure proxy settings for Java?

The Sun (er, Oracle) Java SE 6 Java Networking and Proxies page covers these properties.



Related Topics



Leave a reply



Submit