开发者

IDE-Style program running

My Goal


I am attempting to make a Java program in which a user can select any .class or .jar file from their computer. My program will then pop up a JInternalFrame with a JEditorPane in it as the console, capturing any console output from the user's program. When the user's program closes (calls System.exit(int status);), my program must not close along with it. My program might also have such features as a button to immediately stop the user's program and others an IDE would. My p开发者_如何转开发rogram need not compile Java code, only run .class and .jar files.

My Experience


I have made a small test version of this program wherein I got two specific files from a package and had the user click one of two buttons, each representing one of the two programs. A press of a button calls the following method:

  private void run(Class runnable)
  {
    java.lang.reflect.Method[] m = runnable.getMethods();
    boolean hasMain = false;
    for (int i = 0; i < m.length; i++)
    {
      if (m[i].getName().equals("main") && m[i].getParameterTypes()[0].isArray() && m[i].getParameterTypes()[0].getName().contains("java.lang.String"))
        try
        {
          Object invoke = m[i].invoke(null, (Object)globalArgs);
          hasMain = true;
          hub.setExtendedState(Hub.ICONIFIED);
          numPrograms++;
        }
        catch (Throwable t)
        {
          java.util.logging.Logger.getLogger(Hub.class.getName()).log(java.util.logging.Level.SEVERE, null, t);
          javax.swing.JOptionPane.showMessageDialog(null, "Could not run " + runnable.getName(), "Error in invocation", javax.swing.JOptionPane.ERROR_MESSAGE);
        }
        finally
        {
          break;
        }
    }
    if (!hasMain)
      javax.swing.JOptionPane.showMessageDialog(null, runnable.getName()
                                                      + " does not have a public static main method that\nreturns void and takes in an array of Strings",
                                                "No main method", javax.swing.JOptionPane.ERROR_MESSAGE);
  }

This method successfully calls either program's main method and runs a copy of said program. However, when any of the programs this hub has started calls the System.exit(int status) command, the hub closes, too. Also, I haven't the slightest clue as to how to capture console output.

My Questions


Does anyone have any experience or advice they would be willing to share to help me make a fully-functional program that can...

  1. Open and run a compiled Java file (remember that .jar files may have more than one class with main(String[] args) method)
  2. Catch System.exit(int status); so that the hub program handles the internal program's exiting
  3. Catch new java.io.PrintStream().println(Object o) and similar calls and place their output in a JEditorPane
  4. Make a button that, when pressed, stops the internal program from running
  5. Possibly make all JFrames the internal program uses into JInternalFrames and place them in a JDesktopPane


If you don't want the other program (which you call through it's main method) to be able to shut down the JVM you're running in, you have, as I see it, three options:

1. Using a SecurityManager

Set up the SecurityManager so that it prevents the System.exit call:

public class Test {
    public static void main(String args[]) {
        SecurityManager sm = System.getSecurityManager();
        System.setSecurityManager(new SecurityManager() {
            @Override
            public void checkExit(int status) {
                throw new SecurityException("Client program exited.");
            }
        });

        try {
            System.out.println("hello");
            System.exit(0);
            System.out.println("world");
        } catch (SecurityException se) {
            System.out.println(se.getMessage());
        }
    }
}

Prints:

hello
Client program exited.

This is probably the nicest solution. This is the way application servers prevent an arbitrary servlet from terminating the entire server.

2. Separate JVM

Run the other program in a separate JVM, using for instance ProcessBuilder

import java.io.*;

public class Test {
    public static void main(String args[]) throws IOException {

        ProcessBuilder pb = new ProcessBuilder("java", "other.Program");
        pb.redirectErrorStream();
        Process p = pb.start();
        InputStream is = p.getInputStream();
        int ch;
        while ((ch = is.read()) != -1)
            System.out.print((char) ch);
        is.close();
        System.out.println("Client program done.");
    }
}

3. Use shutdown hooks instead

Don't disallow the termination of the JVM, but instead add shutdown-hooks that cleans up the "hub" and exits gracefully. (This option probably only makes sense if your running one "external" program at a time.)

import java.io.*;

public class Test {

    public static void main(String args[]) throws IOException {

        Runtime.getRuntime().addShutdownHook(new Thread() {
            public void run() { 
                System.out.println("Uninitializing hub...");
                System.out.println("Exiting gracefully.");
            }
        });

        // Run client program
        System.out.println("Running... running... running...");
        System.exit(0);

     }
}

Prints:

Running... running... running...
Uninitializing hub...
Exiting gracefully.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜