在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环境下,就得不到预期结果!
回复 更多评论