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();
}
}