在myEclpise環境下,這種方法怎么不行?!例如,我在MyEclipse環境下給出的兩個程序:package multipleProcess;
/*
* Test3.java
*
* Created on 2007-9-27, 11:46:31
*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.File;
import java.io.IOException;
import java.util.Map;
/**
*
* @author hadeslee
*/
public class test3 {
public static void main(String[] args) throws IOException{
//用一條指定的命令去構造一個進程生成器
ProcessBuilder pb=new ProcessBuilder("java","test2");
pb.start();
//讓這個進程的工作區空間改為F:\dist
//這樣的話,它就會去F:\dist目錄下找Test.jar這個文件
//pb.directory(new File("d:\\dist"));
//得到進程生成器的環境 變量,這個變量我們可以改,
//改了以后也會反應到新起的進程里面去
Map<String,String> map=pb.environment();
Process p=pb.start();
//然后就可以對p做自己想做的事情了
//自己這個時候就可以退出了
System.exit(0);
}
}
package multipleProcess;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextField;
/**
*
* @author hadeslee
*/
public class test2 extends JPanel{
private JTextField input;
private JButton go;
public test2() {
super(new BorderLayout());
initWindow();
}
private void initWindow() {
try {
go = new JButton("轉到");
input = new JTextField();
JPanel up = new JPanel(new BorderLayout());
up.add(input, BorderLayout.CENTER);
up.add(go, BorderLayout.EAST);
this.add(up, BorderLayout.NORTH);
} catch (Exception ex) { }
JFrame jf = new JFrame("JAVA瀏覽器");
jf.add(this, BorderLayout.CENTER);
jf.setSize(500, 300);
jf.setLocationRelativeTo(null);
jf.setVisible(true);
jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent ae) {
doOpen();
}
private void doOpen() {
// try {
String text = input.getText();
if (text == null || text.equals("")) {
return;
}
if (!text.toLowerCase().startsWith("http://")) {
text = "http://" + text;
}
/* } catch (MalformedURLException ex) {
Logger.getLogger(Test1.class.getName()).log(Level.SEVERE, null, ex);
}*/
}
public static void main(String[] args) {
new test2();
}
}
當然,上面的兩個程序,去掉package之后,在命令行下能得到預期結果,但在MyEclipse環境下,就得不到預期結果!
回復 更多評論