By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,742 Members | 793 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,742 IT Pros & Developers. It's quick & easy.

system.setproperty problem

hsn
100+
P: 237
hello everyone.
i am trying to use the system.setproperty to set the proxy in my computer.

Expand|Select|Wrap|Line Numbers
  1. public static void main(String[] args) {
  2.  
  3.     // TODO Auto-generated method stub
  4.  
  5.  
  6.         System.setProperty("http.proxyHost","192.222.222.2");
  7.         System.setProperty("http.proxyPort","808");
  8.         System.setProperty("http.nonProxyHosts","yahoo.com");
  9.  
  10.         detect2();
  11.  
  12.  
  13.  
  14.     }
  15.  
  16.  
  17.  
  18.     public static void detect2() {
  19.  
  20.         try {
  21.             //System.setProperty("java.net.useSystemProxies","true");
  22.  
  23.             List l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com/"));
  24.  
  25.             for (Iterator iter = l.iterator(); iter.hasNext(); ) {
  26.             Proxy proxy = (Proxy) iter.next();
  27.             System.out.println("proxy type : " + proxy.type());
  28.             InetSocketAddress addr = (InetSocketAddress)
  29.             proxy.address();
  30.             if(addr == null) {
  31.  
  32.             System.out.println("No Proxy");
  33.             } else {
  34.  
  35.             System.out.println("proxy hostname : " +addr.getHostName());
  36.             System.out.println("proxy port : " +addr.getPort());
  37.  
  38.             }
  39.  
  40.             }
  41.  
  42.         } 
  43.         catch (Exception e) {
  44.             e.printStackTrace();
  45.         }
  46.  
  47.  
  48.  
  49.  
  50.  
  51.     }
  52.  
  53.     public static void detect(String location)
  54.  
  55.     {
  56.  
  57.         String proxyHost;
  58.  
  59.         int proxyPort;
  60.  
  61.         try {
  62.  
  63.             ProxyInfo info[] = ProxyService.getProxyInfo(new URL(location));
  64.  
  65.             if(info != null && info.length>0)
  66.  
  67.             {
  68.  
  69.             proxyHost = info[0].getHost();
  70.  
  71.             proxyPort = info[0].getPort();
  72.  
  73.             System.out.println("PROXY = " + proxyHost + ":" + proxyPort);
  74.  
  75.             }
  76.  
  77.         }
  78.         catch (Exception ex) {
  79.             System.err.println("could not retrieve proxy configuration, attempting direct connection." + ex);
  80.         }
  81.  
  82.     }
  83.  
  84.  
IF YOU run this code you will find that the proxy has been changed from direct to http and the host and the port will be as it is specified in the code.
after you run it the first time please comment the following line of code
Expand|Select|Wrap|Line Numbers
  1. System.setProperty("http.proxyHost","192.222.222.2");
  2. System.setProperty("http.proxyPort","808");
  3. System.setProperty("http.nonProxyHosts","yahoo.com");
  4.  
and run the program again.
you will find that the proxy returned to Direct.

i need to know how can i set save the changes i added to system.property so all the browsers will use this proxy.

kind regards
hsn
Nov 30 '08 #1
Share this Question
Share on Google+
7 Replies


hsn
100+
P: 237
hsn
if someone else knows who to set the proxy in the computer so it could be used by the browsers in a different way. please help

hsn
Nov 30 '08 #2

Expert 10K+
P: 11,448
System properties aren't persistent; i.e. all properties are gone when the JVM has stopped. You can set your properties on the command line:
-Dkey=value associates key with value as a system property before your main() is run.

kind regards,

Jos
Nov 30 '08 #3

hsn
100+
P: 237
hsn
hello Jos.
i have never used the terminal to run java code. after u posted you reply, i search online and i learned it.
after compiling the java code i enter this command

java MainTest -D http.proxyHost= "yahoo.com" -D http.proxyPort=999

is this the correct syntax??

hsn
Nov 30 '08 #4

Expert 10K+
P: 11,448
Put those -Dkey=value parameter(s) at the start (following 'java'), otherwise they are just arguments for your main() method.

kind regards,

Jos
Nov 30 '08 #5

hsn
100+
P: 237
hsn
hello Jos.
i have tried your advice. i was able to use -D and the data was changed.
but then when i try to run the program again just to make sure that the data were saved, but the proxy was returned to Direct.
Nov 30 '08 #6

Expert 10K+
P: 11,448
@hsn
Reread my reply #3: properties aren't persisten. When your program stops running all those properties are gone.

kind regards,

Jos
Nov 30 '08 #7

hsn
100+
P: 237
hsn
thanks Jos. at least now i know how useful are setproperties
Dec 1 '08 #8

Post your reply

Sign in to post your reply or Sign up for a free account.