-1

I am trying to make a powershell script that automatically sets up this program and requires to use a cmd command to run do it.

I know that the cmd command work and I tried cmd.exe.

cmd.exe /c "java -jar fitnesse-standalone.jar -p 9090"

Error Message: cmd.exe : The term 'cmd.exe' is not recognized

Rohan M
  • 1
  • 1
  • 2
  • 7
  • Is this on Windows? Which version of PowerShell are you running? – Mathias R. Jessen Aug 13 '19 at 20:25
  • Yes I am using Windows, and Version 5.1 – Rohan M Aug 13 '19 at 20:27
  • Try just “cmd”, leaving off the .exe – CourageousPotato Aug 13 '19 at 20:35
  • Is the file executable? – connorjohnson Aug 13 '19 at 20:44
  • I'm not able to reproduce your issue. `cmd.exe` executes successfully and fails complaining that it can't find fitnesse-standalone.jar. Try preceding the command with [`&`](https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_operators?view=powershell-6#call-operator-) like this: `& cmd.exe /c "java -jar fitnesse-standalone.jar -p 9090"` – Rich Moss Aug 13 '19 at 21:05
  • 1
    It sounds like `cmd.exe` is not in your `$Env:Path`. What is the output of `Get-ChildItem "$Env:windir\System32\cmd.exe"`? – lit Aug 13 '19 at 22:13
  • Possible duplicate of [What is the reason for "X is not recognized as an internal or external command, operable program or batch file"?](https://stackoverflow.com/questions/41454769/what-is-the-reason-for-x-is-not-recognized-as-an-internal-or-external-command) – aschipfl Aug 14 '19 at 07:53
  • @lit my result was ```Directory: C:\Windows\System32 Mode LastWriteTime Length Name ---- ------------- ------ ---- -a---- 4/11/2018 7:34 PM 273920 cmd.exe ``` – Rohan M Aug 14 '19 at 16:50
  • Does this report `True`? `$Env:Path -match "C:\\Windows\\System32;"` – lit Aug 14 '19 at 17:06
  • @lit no it returns false – Rohan M Aug 14 '19 at 20:14
  • I think you could force it to work by specifying `& $Env:windir\System32\cmd.exe`. However, you need to determine why $Env:Path does not contain the directory containing cmd.exe. – lit Aug 14 '19 at 21:48

1 Answers1

4

You can use start-process

Start-Process -FilePath "cmd.exe"  -ArgumentList '/c "java -jar fitnesse-standalone.jar -p 9090"'

The /c and everything following it is just part of the one set of parameters being passed to cmd. If you want powershell to wait for the the Java app to close, add the -wait parameter.

There's also no real need to use CMD at all in this case (since your giving it /c to exit immediately anyway), you can call java directly:

Start-Process -FilePath "java.exe"  -ArgumentList '-jar fitnesse-standalone.jar -p 9090'

and it should be the same.

Note: you'll need Java in your path or the current working directory, and fitnesse-standalone.jar in the current directory for either of these to work.

Josh
  • 170
  • 7
  • Are you sure you have Java.exe / CMD.exe in your path environment?Did you get no result at all? You should have some error from powershell, or else java.exe could be running in the background. – Josh Aug 14 '19 at 22:29
  • When I use the command, a window pops up and closes really fast and then asks for my next powershell command. The point of the command is the set up this program and the program is not running. – Rohan M Aug 16 '19 at 17:51
  • So it's not nothing happening then. Well if you chance /c to /k then you'll probably see the actual error message. Which will probably be either `'java' is not recognized as an internal or external command, operable program or batch file.` or `Error: Unable to access jarfile fitnesse-standalone.jar` Because you made no mention of this part _Note: you'll need Java in your path or the current working directory, and fitnesse-standalone.jar in the current directory for either of these to work._ if you insist on calling cmd, I suggest using the full path to fitnesse-standalone.jar – Josh Aug 19 '19 at 04:41