views:

266

answers:

1

I'm using the following method to invoke a class inside a jar file:

invokeClass("path.to.classfile", new String[] {});

public static void invokeClass(String name, String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, MalformedURLException {
    File f = new File(System.getProperty("user.home") + File.separator + ".myapplication"+File.separator+"myjar.jar");

    URLClassLoader u = new URLClassLoader(new URL[]{f.toURI().toURL()});
    Class c = u.loadClass(name);
      Method m = c.getMethod("main", new Class[] { args.getClass() });
      m.setAccessible(true);
      int mods = m.getModifiers();
      if (m.getReturnType() != void.class || !Modifier.isStatic(mods) || !Modifier.isPublic(mods)) {
        throw new NoSuchMethodException("main");
      }
      try {
        m.invoke(null, new Object[] { args });
      } catch (IllegalAccessException e) {

      }
}

Is it possible to invoke this on a separate process? So the running application and the new invoked application have nothing in common?

Situation: You launch program a (client updater). From client a you launch program b (client)

With the current code, project a AND all instances of project b share the same heap space. I'm trying to achieve a state where all instances of project b are stand-alone and do not care if project A is terminated or not.

Regards, Peeter

+3  A: 
OscarRyz
will that work if the program that does pb.start() terminates?
TofuBeer
Theres is only one way to know ...
OscarRyz
Will that work if the java environmental variable isn't set (e.g. Mac OS X?)[can't test on mac os x]
Peeter