Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3277760
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:24:19+00:00 2026-05-17T19:24:19+00:00

My Goal I am attempting to make a Java program in which a user

  • 0

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 program 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T19:24:20+00:00Added an answer on May 17, 2026 at 7:24 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My goal is to maintain a web file server separately from my main ASP.NET
The goal: To create a .NET dll i can reference from inside SQL Server
The goal: Any language. The smallest function which will return whether a string is
I'm attempting to make an API type app in Symfony 1.4. My goal is
Goal Java client for Yahoo's HotJobs Resumé Search REST API . Background I'm used
my goal is to write a stored proc that can collect all field values
My goal is to recognize simple gestures from accelerometers mounted on a sun spot.
I am attempting to make some cross-DB(SQL Server and PostgreSQL) compatible SQL. What I
I'm attempting to use wget to recursively grab only the .jpg files from a
I've been attempting for the last week or so to compile any of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.