I am trying to change proxy settings for JVM in my user interface (Eclipse application running on Java 1.6.0.23)
if (isUseProxy()) { System.setProperty("java.net.useSystemProxies", "true"); System.setProperty("http.proxyHost", getProxyHost()); System.setProperty("http.proxyPort", getProxyPort()); System.setProperty("https.proxyHost", getProxyHost()); System.setProperty("https.proxyPort", getProxyPort()); .......... } else { System.clearProperty("http.proxyHost"); System.clearProperty("http.proxyPort"); System.clearProperty("https.proxyHost"); System.clearProperty("https.proxyPort"); }
the problem is that the NEW proxy server value is not used until the JVM is restarted, it is cached somewhere in Java.
Java version:
java.runtime.version=1.6.0_26-b03 java.specification.name=Java Platform API Specification java.specification.vendor=Sun Microsystems Inc.
UPDATE: the magic continues ... I tried to isolate the problem to find out how Java magically works with system.properties. It seems that Java is ignoring the wrong proxy setting in some random cases. This test fails:
import org.junit.Test; import java.io.IOException; import java.net.*; import static org.junit.Assert.fail; public class ProxySetTest { @Test public void verifyProxyIsNotCachedInJVM() throws IOException { tryConnectionToGoogleCom(); System.setProperty("http.proxyHost", getInvalidProxyHost()); System.setProperty("http.proxyPort", getInvalidProxyPort()+""); System.setProperty("https.proxyHost", getInvalidProxyHost()); System.setProperty("https.proxyPort", getInvalidProxyPort()+"");
source share