Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting JVM/JRE to use Windows Proxy Automatically

Tags:

java

proxy

jvm

I did see the question about setting the proxy for the JVM but what I want to ask is how one can utilize the proxy that is already configured (on Windows).

Here is a demonstration of my issue:

  1. Go to your Control Panel->Java and set a proxy address.
  2. Run the following simple applet code (I'm using the Eclipse IDE):
import java.awt.Graphics; import javax.swing.JApplet; import java.util.*;  public class Stacklet extends JApplet {     private String message;     public void init(){         Properties props = System.getProperties();         message = props.getProperty("http.proxyHost", "NONE");               message = (message.length() == 0)? "NONE": message;     }      public void paint(Graphics g)     {         g.drawString(message, 20, 20);     } } 

The Applet displays "NONE" without regard to the settings you've placed in the Java Control Panel. What would be best would be if the Windows proxy settings (usually set in Internet Explorer) were what I could determine but doing an extra configuration step in the Java Control Panel would still be an acceptable solution.

Thanks!

like image 441
t3rse Avatar asked Dec 17 '08 21:12

t3rse


People also ask

How do I turn on automatic proxy settings?

To set up a proxy server connection automaticallySelect the Start button, then select Settings > Network & internet > Proxy. Under Automatic proxy setup, turn on Automatically detect settings.

Does Java use Http_proxy?

Java provides proxy handlers for HTTP, HTTPS, FTP, and SOCKS protocols. A proxy can be defined for each handler as a hostname and port number: http. proxyHost – The hostname of the HTTP proxy server.

Should automatic proxy setup be on?

It's basically split into two configurations: either Automatic or Manual proxy setup. In 99% of the cases, everything should be set to Off. If anything is turned on, your web traffic could be going through a proxy.


1 Answers

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()));         }     } } 
like image 184
t3rse Avatar answered Sep 28 '22 17:09

t3rse