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

前端 未结 19 1598
孤街浪徒
孤街浪徒 2020-11-22 05:51

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

相关标签:
19条回答
  • 2020-11-22 06:07

    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");
    
    0 讨论(0)
  • 2020-11-22 06:07

    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

    0 讨论(0)
  • 2020-11-22 06:08

    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");
    
    0 讨论(0)
  • 2020-11-22 06:12

    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.

    0 讨论(0)
  • 2020-11-22 06:12

    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
    
    0 讨论(0)
  • 2020-11-22 06:12

    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"

    0 讨论(0)
提交回复
热议问题