472,354 Members | 1,672 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,354 software developers and data experts.

system.setproperty problem

hsn
237 100+
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
7 5795
hsn
237 100+
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
JosAH
11,448 Expert 8TB
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
237 100+
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
JosAH
11,448 Expert 8TB
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
237 100+
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
JosAH
11,448 Expert 8TB
@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
237 100+
thanks Jos. at least now i know how useful are setproperties
Dec 1 '08 #8

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

Similar topics

0
by: Pat Kelley | last post by:
I am trying to feed database column data into a bean using JSTL expression language: <jsp:setProperty name="fileWrite" property="fileText" value="1"/> <c:set var="test" value="test"/>...
3
by: nos | last post by:
Just curious about how I would specify the location of a system file like this one public static String getenv(String name) {} if I wanted to override it in my file...
1
by: DelboyJay | last post by:
I have found a strange resource problem, but luckily I have also found the solution which was not obvious from the runtime exception. I really think that the compiler should pick up this problem...
1
by: matic74 | last post by:
Salve a tutti, sto sviluppando un'applicazione web piuttosto complessa in java che utilizza il framework Bluprint della Sun. In particolare, la mia problematica &egrave; legata alla...
11
by: ptass | last post by:
Hi I've installed win2k3 sp1 on a machine where an openRead on any given file was previously working. After installation, I get a webException as follows... ..message "An exception occurred...
3
by: Michael Bøcker-Larsen | last post by:
Hi I'v been stuck on this problem for ages now. I have found that I'm not the only one with this problem, by looking through the different newsgroups. Hope you can help me! I know there is a...
0
by: Michal | last post by:
Hi, i'm trying in C# to turn on/off task pane in powerpoint. I can check the status, but i can not set it. this is example how can i check if task pane is visible string property = "Task...
2
by: james | last post by:
Hey Guys, Is there any way to attach to the onLoad property of a page, outside of the page itself? In porting an application from jsp to aspx, I added attributes like <script...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was proposed, which integrated multiple engines and...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. header("Location:".$urlback); Is this the right layout the...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific technical details, Gmail likely implements measures...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.