Java: modifying system properties through runtime

I have a running jar file It is selenium RC server I want to be able to change the JVM httpproxy Host / port / etc system value On the one hand, I can modify the source and add this function It will take some time Is there another possible way? Just like having my own JAR (which sets these JVM attributes), calling selenium-rc in the same JVM instance (so that it can modify the value of its JVM variable)?

Solution

You can use the command line to define on system properties

-DpropertyName=propertyValue

So you can write

java -jar selenium-rc.jar -Dhttp.proxyHost=YourProxyHost -Dhttp.proxyPort=YourProxyPort

See Java – the Java application launcher,

Edit:

You can write a wrapper that acts as an application launcher It's easy to simulate using reflection to call the main method in a class You can then also use system. Net before starting the final application Setproperty sets system properties For example,

public class AppWrapper
{
/* args[0] - class to launch */     
   public static void main(String[] args) throws Exception
   {  // error checking omitted for brevity
      Class app = Class.forName(args[0]);
      Method main = app.getDeclaredMethod("main",new Class[] { (new String[1]).getClass()});
      String[] appArgs = new String[args.length-1];
      System.arraycopy(args,1,appArgs,appArgs.length);
      System.setProperty("http.proxyHost","someHost");
      main.invoke(null,appArgs);
   }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>