473,466 Members | 1,388 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

system.setproperty problem

hsn
237 New Member
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 5901
hsn
237 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
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 New Member
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 Recognized Expert MVP
@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 New Member
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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.