1

How to invoke the powershell command using java.

  try {
        ExecuteWatchdog watchdog = new ExecuteWatchdog(20000);
        Process powerShellProcess = Runtime.getRuntime().exec(
                "powershell.exe \"D:\\testscript.ps1\"");
        if (watchdog != null) {
            watchdog.start(powerShellProcess);
        }
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                powerShellProcess.getInputStream()));
        String line;
        System.out.println("Output :");
        while ((line = stdInput.readLine()) != null) {
            System.out.println(line);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

note : i map the correct path.

I tried with the above code but it gives the error like

java.io.IOException: Cannot run program "powershell.exe": CreateProcess error=2, The system cannot find the file specified
    at java.lang.ProcessBuilder.start(ProcessBuilder.java:459)
    at java.lang.Runtime.exec(Runtime.java:593)
    at java.lang.Runtime.exec(Runtime.java:431)
    at java.lang.Runtime.exec(Runtime.java:328)
    at com.powershell.PsJava.main(PsJava.java:17))

Anyone could you please help on this.

3
  • 1
    1) Read (and implement) all the recommendations of When Runtime.exec() won't. That might solve the problem. If not, it should provide more information as to the reason it failed. Then ignore that it refers to exec and build the Process using a ProcessBuilder. Also break a String arg into String[] args to account for arguments which themselves contain spaces. 2) There is no need to add the major tag in the title. Commented Jul 22, 2013 at 10:04
  • it just cant find the file. Change "powershell.exe" to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" Commented Jul 22, 2013 at 17:28
  • Thank u @Cole9350, Problem is resolved. Commented Jul 23, 2013 at 4:29

6 Answers 6

6

Environment Variables are not always exposed to the java compiler. Your stack error is just telling you it cannot find the powershell executable, because it doesn't automatically know to look in the $PSHOME var.

The fix is just to specify the full path:
Change "powershell.exe" to "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe"

Sign up to request clarification or add additional context in comments.

1 Comment

can you please specify, change it where, should we add this as new path
0

You can run the PowerShell command using the ProcessBuilder from Java.

ProcessBuilder builder =
    new ProcessBuilder("powershell.exe", "/c", "Get-Process");
Process p = builder.start();

Comments

0

If the powershell executable IS in your path then make sure that you have not accidentally have Use secret text(s) or file(s) selected with specific username password credentials in the Build Environment section. I'm not sure why but this seemed to cause this issue when building on our slave!

Comments

0

Yes, we need to configure that the environment variable for Powershell.exe is in the path C:\Windows\System32\WindowsPowerShell\v1.0 and then restart the system.

After, that, execute the PowerShell command or script.

Comments

0

I test all answers above and didnot worked ! try this one ... I have this problem and it was so so suffering :)) I can solve this problem in this way: first I add C:\Windows\System32\WindowsPowerShell\v1.0 to my System path in environment variables in windows 10, after that I restart my Pc, after restarting I create a new scala sbt project but I choose scala version 11.10 and sbt version 0.0.3x instead of 1.x.x and It worked , wish you best !

Comments

0

Instead of using powershell.exe try using pwsh.exe. It solved my problem.

Note: There are some changes made, so ensure switching to pwsh does not break your powershell commands.

Comments

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.