http://topic.csdn.net/u/20110704/23/ed1d0b61-3302-4d8e-a2e3-e9922a4cd318.html?1328067503
public class HelloWorld {
    public static void main(String[] args){
        System.out.println("Hello");
    }
} 我想实现的是,另一个程序(暂且称为能够为 主程序)中运行另一个jar,如图:(有图有码)
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;public class MainWindow extends JFrame {
public MainWindow() {
super("主窗口");
setLayout(new FlowLayout());
final JTextField jtf = new JTextField(16);
add(jtf);
JButton jbtn = new JButton("运行Jar文件");
jbtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {
try {
Process process = RunJar.run("java -jar " + jtf.getText().trim());
ShowResult result = new ShowResult(process, "HelloWorld");
Thread t = new Thread(result);
t.start();
} catch (IOException e) {
e.printStackTrace();
}
}
});
add(jbtn);
pack();
setResizable(false);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
} public static void main(String[] args) {
new MainWindow();
}
}class RunJar {
public static Process run(String[] cmdarray, String[] envp, File dir)
throws IOException {
Runtime r = Runtime.getRuntime();
return r.exec(cmdarray, envp, dir);
} public static Process run(String cmdarray, String[] envp, File dir)
throws IOException {
Runtime r = Runtime.getRuntime();
return r.exec(cmdarray, envp, dir);
} public static Process run(String cmdarray) throws IOException {
Runtime r = Runtime.getRuntime();
return r.exec(cmdarray);
}
}class ShowResult extends JFrame implements Runnable {
/**
 * 
 */
private static final long serialVersionUID = -2800186689207336067L;
private JTextArea area_Log = new JTextArea();
public Process process = null; public ShowResult(Process process, String title) {
this.process = process;
setTitle(title);
setSize(800, 750);
setLocation(200, 0);
setResizable(false); this.area_Log.setFont(new Font("宋体", 0, 18));
this.area_Log.setBackground(Color.BLACK);
this.area_Log.setForeground(Color.white);
this.area_Log.setEditable(false);
initLog(); setLayout(new BorderLayout());
add(new JScrollPane(this.area_Log), "Center");
addWindowListener(new MyWindowAdapter()); setVisible(true);
} public void showError() {
BufferedReader br1 = new BufferedReader(new InputStreamReader(
this.process.getErrorStream()));
String str1 = "";
try {
while ((str1 = br1.readLine()) != null) {
this.area_Log.append(str1 + "\n");
this.area_Log.setCaretPosition(this.area_Log.getDocument()
.getLength());
}
this.process.waitFor();
} catch (Exception e) {
System.out.println("错误:" + e.getMessage());
e.printStackTrace();
} finally {
if (br1 != null)
try {
br1.close();
br1 = null;
} catch (IOException e) {
System.out.println("错误:" + e.getMessage());
e.printStackTrace();
}
}
} public void showResult() {
BufferedReader br = new BufferedReader(new InputStreamReader(
this.process.getInputStream()));
String str = "";
try {
while ((str = br.readLine()) != null) {
this.area_Log.append(str + "\n");
this.area_Log.setCaretPosition(this.area_Log.getDocument()
.getLength());
}
this.process.waitFor();
} catch (Exception e) {
System.out.println("错误:" + e.getMessage());
e.printStackTrace();
} finally {
if (br != null)
try {
br.close();
br = null;
} catch (IOException e) {
System.out.println("错误:" + e.getMessage());
e.printStackTrace();
}
}
} public void run() {
new Thread(new Runnable() {
public void run() {
ShowResult.this.showError();
}
}).start();
showResult();
} private void initLog() {
this.area_Log.append("控制台\n\n");
} public static Process run(String[] cmdarray, String[] envp, File dir)
throws IOException {
Runtime r = Runtime.getRuntime();
return r.exec(cmdarray, envp, dir);
} private class MyWindowAdapter extends WindowAdapter {
private MyWindowAdapter() {
} public void windowClosing(WindowEvent e) {
super.windowClosed(e);
ShowResult.this.dispose();
if (ShowResult.this.process != null)
ShowResult.this.process.destroy();
}
}
}