【问题标题】:Java - Runtime.getRuntime().exec() what's going on?Java - Runtime.getRuntime().exec() 发生了什么?
【发布时间】:2010-06-13 13:37:50
【问题描述】:

Java 中的 Runtime.exec() 有问题 我的代码:

String lol = "/home/pc/example.txt";
String[] b = {"touch", lol}; 
try {  
    Runtime.getRuntime().exec(b);  
} catch(Exception ex) {  
    doSomething(ex);  
}

它工作得很好,但是当我尝试 changle 变量“lol”时,文件没有在硬盘中创建

例如: String lol = x.getPath(); 其中 getPath() 返回字符串

我该怎么办?

感谢您的回复:)

【问题讨论】:

  • 在 Linux 上没有做过很多 Java,但可能是权限问题——也许沙盒不允许您在主目录之外创建文件?只是一个猜测,也许需要调查一下。
  • 谢谢回复,但我设置了 chmod 777 并且当我不使用 getPath() 文件时出现。
  • 注意:如果命令失败,Runtime#exec() 不会抛出任何异常。您想读取它的输出或错误流。另请参阅此链接(所有 4 页)javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html

标签: java runtime exec runtime.exec


【解决方案1】:

这是您问题的解决方案。我遇到了类似的问题,这通过指定输出目录对我有用,它应该在该工作目录中执行文件的输出。

   ProcessBuilder proc = new ProcessBuilder("<YOUR_DIRECTORY_PATH>" + "abc.exe"); // <your executable path> 
   proc.redirectOutput(ProcessBuilder.Redirect.INHERIT);  // 
   proc.directory(fi); // fi = your output directory
   proc.start();

【讨论】:

    【解决方案2】:

    您可能正在使用java.io.File
    在这种情况下,getPath() 不会返回绝对路径。 例如:

    System.out.println(System.getProperty("user.dir")); // Prints "/home/pc/"
    // This means that all files with an relative path will be located in "/home/pc/"
    File file = new File("example.txt");
    // Now the file, we are pointing to is: "/home/pc/example.txt"
    System.out.println(file.getPath()); // Prints "example.txt"
    System.out.println(file.getAbsolutePath()); // Prints "/home/pc/example.txt"
    

    所以,结论:使用java.io.File.getAbsolutePath()

    提示:还有一个java.io.File.getAbsoluteFile() 方法。这将在调用getPath()时返回绝对路径。


    我刚刚阅读了您对另一个答案的评论:

    我认为你做到了:

    String[] cmd = {"touch /home/pc/example.txt"};
    Runtime.getRuntime().exec(cmd);
    

    这不起作用,因为操作系统会搜索名为“touch /home/pc/example.txt”的应用程序。
    现在,您在想“WTF?为什么?”
    因为Runtime.getRuntime().exec(String cmd); 方法将您的字符串拆分为空格。 而Runtime.getRuntime().exec(String[] cmdarray); 不会将其拆分。所以,你必须自己做:

    String[] cmd = {"touch", "/home/pc/example.txt"};
    Runtime.getRuntime().exec(cmd);
    

    【讨论】:

      【解决方案3】:

      当你打电话给x.getPath()时,只需看看lol的内容。我猜这不是绝对路径,并且文件已创建,但不是您期望的位置。

      xJava.io.File 我们 getCanonicalPath() 的绝对路径。

      【讨论】:

      • 好点,但如果我打印 x.getPath() 得到的结果等于“/home/pc/example.txt”。应该没问题的当我使用 Runtime.getRuntime().exec("touch /home/pc/example.txt") 时它工作正常,但是当我尝试使用函数时它不起作用。
      【解决方案4】:

      如果代码在您将字符串设置为文字“/home/pc/example.txt”时有效,并且 x.getPath 也返回相同的值,那么它必须有效 - 就这么简单。这意味着 x.getPath() 实际上正在返回其他内容。也许字符串中有空格?尝试直接比较字符串:

      if (!"/home/pc/example.txt".equals(x.getPath())) throw new RuntimeException();
      

      【讨论】:

        【解决方案5】:

        喜欢为真实路径写代码

        String path = request.getSession().getServletContext().getRealPath("/");
        

        在这里你可以得到真正的路径............

        【讨论】:

          猜你喜欢
          • 2012-06-21
          • 1970-01-01
          • 1970-01-01
          • 2011-02-22
          • 2011-01-07
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多