Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure proxy settings for Java?

Tags:

java

url

proxy

I am trying to open a URL and read the website line by line. I can do this fine in Eclipse because I guess Eclipse configures it automatically for you. When I try to run the program from the command line the program hangs and never reads the URL.

After some research the problem has to do with the proxy settings, I figured out. All articles I come across say to change something like this:

System.setProperty("java.net.useSystemProxies","true");

Or to add lines of code like this:

System.setProperty("http.proxyHost", "webcache.mydomain.com");
System.setProperty("http.proxyPort", "80");

But I have no idea what to put for my proxy settings and what any of those System.setProperty calls do. Does anyone know how to set the proxy settings? I am just trying to run this from my home computer on a localhost and I'm not even behind a proxy or anything.

Here is the code I use that works fine in Eclipse.

URL link = new URL("http://www.yahoo.com");
            
BufferedReader in = new BufferedReader(new InputStreamReader(link.openStream()));
//InputStream in = link.openStream();
String inputLine = "";
int count = 0;
while ((inputLine = in.readLine()) != null) {
    site = site + "\n" + inputLine;
}
in.close();
like image 987
Ray Avatar asked Nov 14 '22 17:11

Ray


1 Answers

java -Dhttp.proxyHost=proxyhostURL
-Dhttp.proxyPort=proxyPortNumber
-Dhttp.proxyUser=someUserName
-Dhttp.proxyPassword=somePassword javaClassToRun

like image 191
Abiram Avatar answered Nov 21 '22 03:11

Abiram