【问题标题】:Java Runtime.exe() not returning PowerShell command outputJava Runtime.exe() 不返回 PowerShell 命令输出
【发布时间】:2018-12-04 22:39:05
【问题描述】:

我尝试使用 java Runtime.exe 运行以下命令

String command ="powershell (Get-item  \"D:\\test\" ).creationtime.ToString(\"yyyy-MM-dd'T'HH:mm:ss\")";

当我在 windows 命令提示符 上运行它时,它会返回“2017-08-07T20:03:00”。

但是当我在 myMethod 中运行它时,程序也在运行,没有任何东西返回给我。

我的方法:

private static void executePowerShellCommand(String command) {

    String line = "";
    command = "cmd /c " + command;
    try {
         Process process = Runtime.getRuntime().exec(command);
         process.getOutputStream().close();
         BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
         while ((line = reader.readLine()) != null) {
             System.out.println(line);
         }
         reader.close();
         System.out.println("Done");
         } catch (java.io.IOException e) {
             e.printStackTrace();
         }
        return;
}

【问题讨论】:

    标签: java windows powershell cmd


    【解决方案1】:

    我发现了问题

    我的命令是错误的,正确的命令是:

    String command = "powershell (Get-item '" + filePath + "' -Force).LastAccessTime.ToString(\\\"yyyy-MM-dd'T'HH:mm:ss'Z'\\\")";
    

    【讨论】:

      猜你喜欢
      • 2011-06-04
      • 2012-08-27
      • 1970-01-01
      • 1970-01-01
      • 2011-12-11
      • 2013-02-19
      • 1970-01-01
      • 1970-01-01
      • 2018-10-31
      相关资源
      最近更新 更多