在Java中的Double Quotes中扩展变量

时间:2014-05-02 15:23:10

标签: java shell variables jdbc command-line-arguments

我有一个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(&#34;文件路径&#34); 变量未展开,因此shellExec方法失败。请注意,双引号是强制性的。请告诉我如何实现这一目标。我试过了:     commands.add(\&#34;文件路径\&#34) 但它仍然无效。请帮忙。

现在filePath每周都在不断变化,所以我不能在程序中对该值进行硬编码。

4 个答案:

答案 0 :(得分:1)

您应该尝试从引号中删除filepath。该行将如下所示:

commands.add(filePath);

当您编写commands.add("filePath")时,您没有传递变量 filePath 的值,而是传递值 filePath

的字符串

我将filePath的值设置为&#34; C:/ test /我在这里&#34; 并得到以下回应:

Entering into the test shell loop
[C:/test/I am here]

答案 1 :(得分:1)

尝试

command.add("\"" + filepath + "\"");

答案 2 :(得分:1)

您需要将filePath设为您班级的静态变量。在您的代码中,它是main方法的局部变量,因此shellExec方法无法访问。然后从"filePath"方法中移除shellExec中的引号,以便传递静态变量而不是String "filePath"

class MyClass {

  static String filePath;

  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)
      ...
  }

  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);
    filePath = args[2]; 

    stmtExample();
  }

}

如果您需要在命令名前加filePath,可以这样做:

commands.add("filePath " + filePath);

答案 3 :(得分:0)

尝试为filepath添加单引号,

command.add("'filepath'");
相关问题