URLClassLoader + loadClass + main method call offline? Java

I use the following method to call 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 call this in a separate process? So, the running application and the new called application have nothing in common?

Situation: you are running program a (client update program). From client a, you run program b (client)

With the current code, project a and all instances of project b have the same heap space. I am trying to reach a state where all instances of project b are autonomous and don't care if project A is complete or not.

+4
source share
1 answer

Yes, and actually it saves you from doing this reflection process in general

You will need to use ProcessBuilder to start a new process on a separate virtual machine.

Sort of:

 ProcessBuilder pb = new ProcessBuilder("java", "-jar", f.getAbsolutePath()); Process p = pb.start(); 

EDIT

- Will this work if the program that terminates pb.start ()?

- Will this work if the java environment variable is not set (for example, Mac OS X?) [Cannot test mac os x]

This is true. Take a look at this video:

http://img33.imageshack.us/img33/8380/capturadepantalla201001s.png

Source code (import omitted):

 // MainApp.java public class MainApp { public static void main( String [] args ) throws IOException { JFrame frame = new JFrame("MainApp"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new JLabel("<html><font size='48'>Main App Running</font><html>") ); frame.pack(); frame.setLocationRelativeTo( null ); frame.setVisible( true ); launchSeparateProcess(); frame.addWindowListener( new WindowAdapter() { public void windowClosing( WindowEvent e ){ System.out.println("MainAppp finished"); } }); } private static void launchSeparateProcess() throws IOException { File f = new File("./yourjar.jar"); ProcessBuilder pb = new ProcessBuilder("java", "-jar", f.getAbsolutePath() ); Process p = pb.start(); } } //-- Updater.jar public class Updater { public static void main( String [] args ) { JFrame frame = new JFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.add( new JLabel("<html><font size='78'>Updating....</font></html>")); frame.pack(); frame.setVisible(true); } } //--manifest.mf Main-Class: Updater 
+5
source

All Articles