Spawning jvm processes

This is kind of an experiment on my part and not absolutely critical if it can’t be done easily, but I’m wondering if it’s possible to launch separate jvm processes from an already existing jvm process. The reason I want to do this is to be able to compare the behavior and performance of certain global settings side by side. For example I run my “launcher” app, set my settings such as sun.java2d.noddraw true or false, click a button and then launch the new window with the approprate settings enabled. Then I can launch as many processes as I want observe their behavior/appearance and then clean them all up by just closing the launcher/master application.

Anway, can this be done or is it more trouble than it’s worth. Thanks for any help.

Have a look at java.lang.Runtime, especially:


exec(String command)
          Executes the specified string command in a separate process.

and friends.

[quote]clean them all up by just closing the launcher/master application.
[/quote]
Windows doesn’t (usually) maintain a parent/child relationship between processes. This means that closing the parent does not close the children. You could register a shutdown task in the parent and explicitly kill any remaining children during the parent process shutdown. On Unix/Linux of course killing the parent does kill all its children.

Number 2 on the bug parade:
http://developer.java.sun.com/developer/bugParade/bugs/4109888.html

Thank You the information so far has been very helpful. I have a couple of questions, however, that I haven’t been able to sort out. For instance, the java docs state for Runtime.exec():

[quote]If envp is null, the subprocess inherits the environment settings of the current process
[/quote]
However, I am unable to successfully launch the new process without explicitly settings the classpath in the environment argument. Shouldn’t it be able to figure it out from the current process as it states?

Second, I can’t seem to register a shutdown task through addShutdownHook. Is this not the correct function to use?

I wrote a small test program. Is there something I’m doing wrong?

package com.launcher;

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;


public class MainLauncher extends JFrame
{
      private JButton launchTest1;
      private JButton launchTest2;
      private JPanel buttonPanel;
      
      private ArrayList externalProcessList = new ArrayList();
      
      private String [] envp = new String [] {"CLASSPATH=c:\\projects\\LauncherTest\\bin"};
      public MainLauncher(String title)
      {
            super(title);
            
            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            
            initComponents();
            
            Runtime.getRuntime().addShutdownHook(new ShutdownThread());
      }
      
      public void initComponents()
      {
            buttonPanel = new JPanel();
            buttonPanel.setLayout(new GridLayout(1, 2, 2, 2));
            
            launchTest1 = new JButton("Test 1");
            launchTest1.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        try
                        {
                              //Does not work.  Returns a main class not found exception.
                              externalProcessList.add(Runtime.getRuntime().exec("javaw com.space.TestFrame -1"));
                        }
                        catch (IOException e1)
                        {
                              e1.printStackTrace();
                        }
                  }      
            });
            
            launchTest2 = new JButton("Test 2");
            launchTest2.addActionListener(new ActionListener()
            {
                  public void actionPerformed(ActionEvent e)
                  {
                        try
                        {
                              //Correctly launches the separate process but does not get shutdown through the registered Shutdown thread :(
                              externalProcessList.add(Runtime.getRuntime().exec("javaw -Dsun.java2d.noddraw=true com.space.TestFrame -2", envp));
                        }
                        catch (IOException e1)
                        {
                              e1.printStackTrace();
                        }
                  }      
            });
            
            buttonPanel.add(launchTest1);
            buttonPanel.add(launchTest2);
            
            getContentPane().add(buttonPanel, BorderLayout.NORTH);
      }
      
      private class ShutdownThread extends Thread
      {      
            public void run()
            {
                  System.out.println("Shutting down external processes");
                  Iterator iterator = externalProcessList.iterator();
                  while(iterator.hasNext())
                  {
                        Process process = (Process)iterator.next();
                        process.destroy();
                  }
                  System.out.println("Finished shutting down external processes");
            }
      }
      
      public static void main(String[] args)
      {
            MainLauncher launcher = new MainLauncher("Launcher Test Suite");
            launcher.show();
            launcher.pack();
      }
}

package com.space;

import java.awt.BorderLayout;
import java.awt.Font;

import javax.swing.JFrame;
import javax.swing.JTextArea;


public class TestFrame extends JFrame
{
      
      public TestFrame(int frameID)
      {
            super("Test Frame" + frameID);
            
            initComponents();
      }
      
      public void initComponents()
      {
            setFont(new Font("Arial", 0, 25));
            
            getContentPane().add(new JTextArea("This window was launched in it's own process"), BorderLayout.NORTH);
      }
      
      public static void main(String[] args)
      {
            int frameID = Integer.parseInt(args[0]);
            TestFrame frame = new TestFrame(frameID);
            frame.show();
            frame.pack();
      }
}

What goes wrong when you try? Note that shutdown hooks will only be run if the process is terminated nicely. Killing the process from Task manager does not give the JVM an opportunity to do any cleanup. The same applies if you kill it from an IDE. If the process is run using java.exe from a command prompt then Ctrl-C will allow the cleanup to take place. Similarly if you call System.exit() from within the process.

As to the environment, I just use the exec variants which don’t require passing it and don’t have any problem.

[quote]Thank You the information so far has been very helpful. For instance, the java docs state for Runtime.exec():
However, I am unable to successfully launch the new process without explicitly settings the classpath in the environment argument. Shouldn’t it be able to figure it out from the current process as it states?
[/quote]
Not if its not in your shell ENVIRONMENT variables.

For instance if you are using -classpath on your command line.

[quote]Note that shutdown hooks will only be run if the process is terminated nicely
[/quote]
D’oh! That’s it. For some reason, I had it in my head that terminating it from my IDE was like hitting Ctrl-C from cmd, but I can see why that wouldn’t be the case and goes with what you said before about Windows not killing child processes.

[quote] Not if its not in your shell ENVIRONMENT variables
[/quote]
Makes sense. I guess it’s not really that big of a deal to set an explicit path for this, but since the type A side of me needs to be appeased, I found that

private String [] envp = new String [] {"CLASSPATH=" + System.getProperty("java.class.path")};

seems to work pretty well for getting the path to wherever it was launched from.

As always, thanks for the help :slight_smile:

Unfortunately it seems the Sun JVM does not process either the WM_ENDSESSION or WM_QUIT messages correctly. As result shutdown processing is unreliable for processes with a message loop (anything gui). Even worse the Sun engineers do not seem to understand the issue. (see bugs 4486580 and 4302814).