Zombies
Zombies

Reputation: 25872

Java: Modify System properties via runtime

I have an existing jar file which I run. It is the Selenium RC server. I want to be able to change the JVM httpProxy.host/port/etc system values. On the one hand I can modify the source and add this feature in. Would take some time. Is there another possible way to do this? Like have my own JAR (which would set these JVM properties) invoke selenium-rc inside that very same JVM instance (that way it would be able to modify its JVM variable's values) ?

Upvotes: 3

Views: 8384

Answers (2)

Esko Luontola
Esko Luontola

Reputation: 73625

Use the System.setProperty() method.

Upvotes: 2

mdma
mdma

Reputation: 57707

You can define system properties on the command line, using

-DpropertyName=propertyValue

So you could 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 is an application launcher. It's easily to emulate calling a main method in a class using reflection. You then can also set system properties via System.setProperty before launching the final application. 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, 0, appArgs.length);
      System.setProperty("http.proxyHost", "someHost");
      main.invoke(null, appArgs);
   }
}

Upvotes: 5

Related Questions