0

I have a simple script and I want to call it from my Java code. The script does not run properly.

The script is very simple: mkdir 0000000;

public static void main(String[] args) throws Exception{
    String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
    String command = "C:\\test\\test.ps1";
    Runtime runtime = Runtime.getRuntime();
    Process proc = runtime.exec(path + " " + command);
    proc.destroy();
}

The dir "0000000" is not created. I use JDK 7, windows 10.

Any suggestion would be gratefully appreciated.

2
  • Remove proc.destroy();. Commented Jan 21, 2019 at 20:09
  • @Ansgar Wiechers: That's not working Commented Jan 22, 2019 at 4:21

2 Answers 2

1

I changed the code as below and finally it worked!

    public static void main(String[] args) throws Exception{
        String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
        String command1 ="cmd /c \"cd C:\\test && " + path + " /c .\\test.ps1\"";
        Runtime runtime = Runtime.getRuntime();
        Process proc = runtime.exec(command1);
        Thread.sleep(2000);
        proc.destroy();
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Using a Thread.sleep(2000) while leaving proc.destroy() in place is setting a hard timeout for the external command, meaning the command will be terminated after 2 seconds, regardless of whether it has finished or not. To wait for the completion of an external command use waitFor() as described in my answer. The change to the commandline (running powershell.exe from CMD instead of running it directly) is not required.
1

exec() runs the process asynchronously, so the subsequent proc.destroy() terminates it right away before it can do anything. If you run the program from an interactive shell simply removing proc.destroy() would mitigate the issue, but to actually fix it you need to wait for the external process to finish. You may also want to catch the exception exec() (or waitFor()) could throw.

import java.io.*;

public static void main(String[] args) throws Exception{
    String path = "C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe";
    String command = "C:\\test\\test.ps1";
    Runtime runtime = Runtime.getRuntime();
    try {
        Process proc = runtime.exec(path + " " + command);
        proc.waitFor();
    } catch (Exception e) {
        // do exception handling here
    }
}

4 Comments

Thanks for your answer, but that does not work. destroy() was a part of the problem. The command itself should be changed too.
@DanielSmith There was a semicolon missing after the import statement, and I didn't account for the exception waitFor() throws. Both issues are fixed now. The code is tested and does work. Changing the command string is not required.
Which OS do you use? I use win10 and this does not work on it. proc.waitFor sometimes makes a lot of problems, in my case I prefer the 2 seconds delay.
@DanielSmith I ran it on Windows 7 with JDK 8. I just tested it again on Windows 10 with JDK 7. Same result: the code I posted works perfectly fine. It does require the execution policy to actually allow running PowerShell scripts, of course, but if that was the issue, the code from your answer wouldn't work either.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.