jar包想执行一个shell脚本,用到ProcessBuilder,发现把死活执行不了,刚开始是路径不对,最后调整完路径之后发现jar包根本无法找到对应的资源文件.如下:
error1:Cannot run program "./runJar11.sh" (in directory "file:/Users/songxuhua/Documents/MonkeyDev自动化安装/libWebsocket.jar!/assets"): error=2, No such file or directory
最后只能将jar包和sh脚本放在同一个文件夹下,获取jar所在文件夹目录(不包括jar)
path = System.getProperty("user.dir");
完整代码如下
ProcessBuilder pb = new ProcessBuilder("./runJar11.sh"); String path = System.getProperty("user.dir"); pb.directory(new File(path)); int runningStatus = 0; String s = null; try { Process p = pb.start(); BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream())); BufferedReader stdError = new BufferedReader(new InputStreamReader(p.getErrorStream())); while ((s = stdInput.readLine()) != null) { System.out.println("s is " + s); } while ((s = stdError.readLine()) != null) { System.out.println("s is" + s); } try { runningStatus = p.waitFor(); } catch (InterruptedException e) { System.out.println("error:" + e.getLocalizedMessage()); } } catch (IOException e) { System.out.println("error1:" + e.getLocalizedMessage()); } if (runningStatus != 0) { } return; }