通过java运行shell脚本 [英] to run shell script through java
问题描述
我想通过java运行一个shell脚本。我正在使用许可证生成工具,它可以在./LicenseGen.sh命令的帮助下调用,在它下面我需要执行另一个命令
create licensekey - x license-input.xml
创建一个新的licensekey.xml文件,其中license-input.xml是输入文件,licensekey是输出xml文件,如何在java中提供帮助我。
I want to run a shell script through java .I am using license generation tool,It can be call with the help of ./LicenseGen.sh command,under it I require to execute another command create licensekey -x license-input.xml which create a new licensekey.xml file where license-input.xml is a input file and licensekey is a output xml file how it is posssible in java please help me.
我的代码是
import java.io.*;
import java.util.*;
public class ProcessExample {
/**
* @param args
*/
public static void main(String args[]) throws IOException {
File file=new File("/opt");
// List<String> list=new List<String>();
ProcessBuilder processBuilder = new ProcessBuilder("./LicenseGen.sh");
processBuilder.directory(file);
Process process=processBuilder.start();
//processBuilder.command("create licensekey -x license-input.xml");
//process=processBuilder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
System.out.printf("Output of running %s is:",
Arrays.toString(args));
while ((line = br.readLine()) != null) {
System.out.println(line);
}
}
}
推荐答案
您不能直接执行脚本,因为它必须由像bash这样的shell解释。
注意bash是可执行的。
You can't execute the script directly since it has to be interpreted by a shell like bash. Note that bash is an executeable.
ProcessBuilder pb = new ProcessBuilder("/bin/bash", "/path/LicenseGen.sh");
这篇关于通过java运行shell脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!