简体   繁体   中英

Java Runtime.exe() not returning PowerShell command output

i try to run below command with java Runtime.exe

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

when i run it on windows Command Prompt it return "2017-08-07T20:03:00".

but when i run it in myMethod the program is also running and nothing return to me.

myMethode:

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;
}

I found the problem

my command was wrong and the correct command is:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM