Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get proxy settings from system settings in Java

I'm looking form way how to get system proxy information in Java under Windows, but I've found just one way. But it does not work for me.

public static void main(String[] args) throws Throwable {
  System.setProperty("java.net.useSystemProxies", "true");
  System.out.println("detecting proxies");
  List<Proxy> pl = ProxySelector.getDefault().select(new URI("http://ihned.cz/"));
  for (Proxy p : pl)
    System.out.println(p);
  Proxy p = null;
  if (pl.size() > 0) //uses first one
    p = pl.get(0);
  System.out.println(p.address());
  System.out.println("Done");
}

When I run the program, I get:

detecting proxies
DIRECT
null
Done

Java means, that I'm situated directly on internet. But it's wrong. I'm behind proxy. I'm unable to get the solution for my computer.

like image 268
Theodor Keinstein Avatar asked May 06 '11 10:05

Theodor Keinstein


2 Answers

Thanks to Dacwe. The problem is, that browser does not use any system proxy, but it sets proxy self using a script. Thus there are not any proxies in the system and Java cannot reach them.

like image 44
Theodor Keinstein Avatar answered Sep 21 '22 18:09

Theodor Keinstein


As we discussed in the comments the proxy settings is just applied for some of browsers you use.

If you want Java to use the same settings you need to manually put it into the java network settings (check this web page for details).

like image 145
dacwe Avatar answered Sep 18 '22 18:09

dacwe