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
Add this before you connect to a URL behind a proxy.
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
System.getProperties().put("http.proxyUser", "someUserName");
System.getProperties().put("http.proxyPassword", "somePassword");
I think configuring WINHTTP will also work.
Many programs including Windows Updates are having problems behind proxy. By setting up WINHTTP will always fix this kind of problems
JVM uses the proxy to make HTTP calls
System.getProperties().put("http.proxyHost", "someProxyURL");
System.getProperties().put("http.proxyPort", "someProxyPort");
This may use user setting proxy
System.setProperty("java.net.useSystemProxies", "true");
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.
Combining Sorter's and javabrett/Leonel's answers:
java -Dhttp.proxyHost=10.10.10.10 -Dhttp.proxyPort=8080 -Dhttp.proxyUser=username -Dhttp.proxyPassword=password -jar myJar.jar
Set the java.net.useSystemProxies property to true
. You can set it, for example, through the JAVA_TOOL_OPTIONS environmental variable. In Ubuntu, you can, for example, add the following line to .bashrc
:
export JAVA_TOOL_OPTIONS+=" -Djava.net.useSystemProxies=true"