system.setproperty problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hsn
    New Member
    • Sep 2007
    • 237

    system.setproperty problem

    hello everyone.
    i am trying to use the system.setprope rty to set the proxy in my computer.

    Code:
    public static void main(String[] args) {
    	
    	// TODO Auto-generated method stub
    	
    	
    		System.setProperty("http.proxyHost","192.222.222.2");
    		System.setProperty("http.proxyPort","808");
    		System.setProperty("http.nonProxyHosts","yahoo.com");
    		 
    		detect2();
    		
    		
    	
    	}
    	
    	
    	
    	public static void detect2() {
    	
    		try {
    			//System.setProperty("java.net.useSystemProxies","true");
    			
    			List l = ProxySelector.getDefault().select(new URI("http://www.yahoo.com/"));
    			
    			for (Iterator iter = l.iterator(); iter.hasNext(); ) {
    			Proxy proxy = (Proxy) iter.next();
    			System.out.println("proxy type : " + proxy.type());
    			InetSocketAddress addr = (InetSocketAddress)
    			proxy.address();
    			if(addr == null) {
    			
    			System.out.println("No Proxy");
    			} else {
    			
    			System.out.println("proxy hostname : " +addr.getHostName());
    			System.out.println("proxy port : " +addr.getPort());
    			
    			}
    			
    			}
    			
    		} 
    		catch (Exception e) {
    			e.printStackTrace();
    		}
    		
    		
    		
    		
    		
    	}
    	
    	public static void detect(String location)
    	
    	{
    	
    		String proxyHost;
    		
    		int proxyPort;
    		
    		try {
    		
    			ProxyInfo info[] = ProxyService.getProxyInfo(new URL(location));
    			
    			if(info != null && info.length>0)
    			
    			{
    			
    			proxyHost = info[0].getHost();
    			
    			proxyPort = info[0].getPort();
    			
    			System.out.println("PROXY = " + proxyHost + ":" + proxyPort);
    			
    			}
    			
    		}
    		catch (Exception ex) {
    			System.err.println("could not retrieve proxy configuration, attempting direct connection." + ex);
    		}
    	
    	}
    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
    Code:
    System.setProperty("http.proxyHost","192.222.222.2");
    System.setProperty("http.proxyPort","808");
    System.setProperty("http.nonProxyHosts","yahoo.com");
    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
  • hsn
    New Member
    • Sep 2007
    • 237

    #2
    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

    Comment

    • JosAH
      Recognized Expert MVP
      • Mar 2007
      • 11453

      #3
      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

      Comment

      • hsn
        New Member
        • Sep 2007
        • 237

        #4
        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

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Put those -Dkey=value parameter(s) at the start (following 'java'), otherwise they are just arguments for your main() method.

          kind regards,

          Jos

          Comment

          • hsn
            New Member
            • Sep 2007
            • 237

            #6
            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.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Originally posted by 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.
              Reread my reply #3: properties aren't persisten. When your program stops running all those properties are gone.

              kind regards,

              Jos

              Comment

              • hsn
                New Member
                • Sep 2007
                • 237

                #8
                thanks Jos. at least now i know how useful are setproperties

                Comment

                Working...