【发布时间】:2014-06-19 08:13:49
【问题描述】:
我有一个 Java 应用程序,它在主应用程序执行时运行各种 Unix 命令。
public static void shellExec(){
try{
System.out.println("Entering into the test shell loop");
List<String> commands = new ArrayList<String>();
commands.add("filePath"); //(filepath is the location of a file which contains various Unix command)
System.out.println(commands);
ProcessBuilder pb = new ProcessBuilder(commands);
pb.redirectErrorStream(true);
Process process = pb.start();
System.out.println("Started the Process Builder");
StringBuilder out = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null, previous = null;
while ((line = br.readLine()) != null)
if (!line.equals(previous)) {
previous = line;
out.append(line).append('\n');
System.out.println(line);
}
}catch(Exception ex){
ex.printStackTrace();
System.exit(1);
}
public static void main(String args[]) {
ds = new SQLMXDataSource();
ds.setUrl("jdbc:t4sqlmx://" + args[0] + ":11600/");
ds.setUser("super");
ds.setPassword(args[1]);
ds.setCatalog("SPM60");
ds.setSchema("RUV60");
ds.setMaxPoolSize(10);
ds.setMinPoolSize(5);
ds.setMaxStatements(10);
String filePath = args[2];
stmtExample();
}
现在您一定已经猜到了 filePath(用于 shellExec 方法)是在运行时捕获的,
java retest "dev.ind.pascal.com" "passwd" "dev/config/proc.sh"
但是当我在 shellExec 方法中调用这个 filePath 时: commands.add("filePath"); 该变量未展开,因此 shellExec 方法失败。请注意,双引号是强制性的。请告诉我如何实现这一目标。我试过了: commands.add(\"文件路径\") 但它仍然不起作用。请帮忙。
现在 filePath 每周都在变化,所以我不能在程序中硬编码那个值。
【问题讨论】:
标签: java shell variables jdbc command-line-arguments