【问题标题】:Expanding a variable within Double Quotes in Java在 Java 中的双引号内扩展变量
【发布时间】: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


    【解决方案1】:

    您需要将filePath 设为类的静态变量。在您的代码中,它是您的 main 方法的局部变量,因此无法访问 shellExec 方法。然后在 shellExec 方法中从 "filePath" 中删除引号,因此您传递的是静态变量而不是 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);
    

    【讨论】:

      【解决方案2】:

      尝试为文件路径添加单引号,例如,

      command.add("'filepath'");
      

      【讨论】:

      • filePath 值不会以这种方式扩展。
      • 感谢您抽出宝贵时间。非常感谢。
      【解决方案3】:

      试试

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

      【讨论】:

      • test11.java:33: 错误:找不到符号 commands.add("\"" + filePath + "\""); ^ 符号:变量文件路径位置:类 test11 .... 这个值是如何没有进入 shellExec 方法的......
      • @Smruti 您需要将变量文件路径传递给 shellExec() 方法,否则将 filePath 作为静态全局变量。然后它应该工作。或者,如果您可以发布完整的课程代码,我可以更正它。
      • 乐于助人。你能接受答案吗?谢谢:)
      【解决方案4】:

      您可能应该尝试从引号中删除文件路径。该行将如下所示:

      commands.add(filePath);
      

      当您编写 commands.add("filePath") 时,您不是在传递变量 filePath 的值,而是在传递值字符串 filePath

      我将 filePath 的值设置为“C:/test/I am here” 并得到以下回复:

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

      【讨论】:

      • 这样我会错过 commands.add("file location") 所必需的双引号
      • 感谢您对此进行调查。感谢您的帮助。
      猜你喜欢
      • 2012-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-04-25
      相关资源
      最近更新 更多