Will nwe instance of JVM or reflection help in this case
===

I had a problem that I posted before but got no clear solution

http://stackoverflow.com/questions/11300700/how-to-prevent-jframe-from-closing.

So I am posting a SSCCE may be this might help in better understanding the problem being faced

    package myApp;
    
    import java.lang.reflect.InvocationTargetException;
    import java.lang.reflect.Method;
    
    import javax.swing.JFrame;
    
    import App2.Applic2;
    
    public class MYApp {
    	
    	@SuppressWarnings({ "unchecked", "rawtypes" })
    	public static void main(String arg[]){
    		JFrame f = new JFrame();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setTitle("Application frame 1");
    		f.setSize(200,200);
    		f.setVisible(true);
    		Class cls = Applic2.class;
    		Object[] actuals = { new String[] { "" } };
    
    		
    		Method m = null;
    		try {
    			m=cls.getMethod("main", new Class[] { String[].class } );
    		} catch (NoSuchMethodException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		} catch (SecurityException e) {
    			// TODO Auto-generated catch block
    			e.printStackTrace();
    		}
    			try {
    				m.invoke(null,actuals);
    			} catch (IllegalAccessException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (IllegalArgumentException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			} catch (InvocationTargetException e) {
    				// TODO Auto-generated catch block
    				e.printStackTrace();
    			}
    	}
    
    }


second package

    package App2;
    
    import javax.swing.JFrame;
    
    public class Applic2  {
    	
    	@SuppressWarnings("unused")
    	public static void main(String args[]){
    		
    		JFrame f = new JFrame();
    		f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		f.setSize(200,200);
    		f.setVisible(true);
    		f.setTitle("This needs not to be changed");
    		NewFrame3 Frame3 = new  NewFrame3();
    	}
    
    }


Second class of App2 package.

    package App2;
    
    import javax.swing.JFrame;
    
    public class NewFrame3 {
    	
    	public NewFrame3(){
    		
    		JFrame f = new JFrame();
    			f.setTitle("f3");
    			f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    			f.setSize(200,200);
    			f.setLocation(200, 200);
    			f.setVisible(true);
    	}
    
    }


`MYAPP` invokes instance of `Applic2` which further invokes instance of `NewFrame3`. Like we can see if I close instance of 'NewFrame3' or instance of `Applic2` the entire program closes (due to `EXIT_ON_CLOSE`) statement.

I want a solution in which `MYAPP` should not close on closing `Applic2` or `NewFrame3`.

I cannot make any changes to APPlic2 or NewFrame3. via reflection If we try to turn `EXIT_ON_CLOSE` to `DISPOSE_ON_CLOSE` How do we access these frames and their `setDefaultCloseOperation()` when we do not have our classes extended `JFrames` ?

In another solution as mentioned a new `JVM` instance should be created and `Applic2` should be executed in a new process on this new `JVM` instance. but then I encountered that runtime.exec takes Java commands as input and not Java statements like `method.invoke()`. 

I have access to the Applic2 via loader that loaded Applic2 I can only access the class files of Applic2 in memory so no way I can use jar to run in runtime.exec(). Now how do I solve it?


