如图,进入指定文件夹,后调用语句
//预存想要执行的命令
List<String> commands= new ArrayList<>();
//进入当前用户bin目录,也可替换成自己想要的目录
commands.add("cd ~/bin");
//我要执行的命令
commands.add("uhvpatrolmodelbuilder -excels "+ substationCode + ".xlsx");
Runtime run = Runtime.getRuntime();
BufferedReader in = null;
PrintWriter out = null;
Process proc = null;
try {
proc = run.exec("/bin/bash", null, null);//通用的,不用动。
in = new BufferedReader(new InputStreamReader(proc.getInputStream()));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true);
//开始执行命令
for (String line : commands) {
logger.info("执行命令:" + line);
out.println(line);
}
// 这个命令必须执行,否则in流不结束。
out.println("exit");
logger.info("-----------返回结果--------------:" + in.readLine());
proc.waitFor();
} catch (IOException e1) {
e1.printStackTrace();
logger.error("调用IO异常", e1);
} catch (InterruptedException e2) {
e2.printStackTrace();
logger.error("异常中断", e2);
} catch (Exception e3) {
e3.printStackTrace();
logger.error("请求失败", e3);
} finally {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (proc != null) {
proc.destroy();
}
} catch (Exception ex) {
logger.error("调用in.close Exception");
}
}