java程序使用ProcessBuilder在命令提示符下工作但不在eclipse中工作

时间:2016-08-04 12:17:41

标签: java eclipse

我正在尝试使用java代码执行.jar文件,我已经使用了ProcessBuilder。我已将java文件和我需要在同一文件夹中执行的jar文件放在一起,并尝试执行我的代码工作:)。但是,现在我已经在eclipse中创建了一个项目,并且我已经为该jar配置了构建路径,并且我尝试执行java文件,但我不是这样做的:(

我的java文件:Test.java

 import java.io.*;
 import java.net.URL;
 import java.net.URLConnection;

 public class Test
 {
    public static void main(String a[]) throws Exception
    {
            try
            {
                    String url="some error url";
                    ProcessBuilder pb = new ProcessBuilder("java","-jar","myjarname.jar","attr 1","attr 2","attr 3");
                    Process p = pb.start();
                    System.out.println(p.getInputStream().read());

                    BufferedReader reader = new BufferedReader(new InputStreamReader(p.getInputStream()));
                    String line;
                    while ((line = reader.readLine()) != null)
                    {
                            System.out.println(line);
                            if(line.contains("https://"))
                            {
                                    url=line.substring(line.lastIndexOf("https")+0);
                                    System.out.println(url);
                            }
                    }
                    System.out.println(p.getErrorStream().read());

                    java.awt.Desktop.getDesktop().browse(java.net.URI.create(url));
            }
            catch (Exception e)
            {
                    System.out.println(e);
            }
    }
}

0 个答案:

没有答案