0

I have a powershell script that is working and runs a java executable. Before I was generating a bunch of powershell script files that were run through the command prompt. Now I want to make it so there does not need to be file creation.

Here is what the line looks like from the working (.ps1) file:

java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe

I want to be able to do something like this in command prompt:

Powershell -Command "java <mem opts here> "-Doption1=3" "-Doption2=`` ` ``"true`` ` ``" jar.exe"

Even just asking this question I am having problems with the escape characters. What is the proper way to handle escape characters when you have quotes in quotes in quotes when calling java through powershell through command prompt? (I understand it is a bit messy)

aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

0

You can lessen the quoting headaches if you focus just on the parts that require quoting (assuming that option value true truly needs quoting):

REM From cmd.exe
C:\> powershell -Command java -Doption1=3 -Doption2="'\"true\"'" jar.exe

The above will make java.exe see:

java -Doption1=3 -Doption2="true" jar.exe

As you can see, even this simple case of the desired resultant quoting is quite obscure, because you're dealing with 3 layers of interpretation:

  • cmd.exe's own interpretation of the initial command line

  • PowerShell's interpretation of the arguments it receives.

  • How PowerShell's translates the arguments into a call to an external program (java.exe).

In any event, the final layer of interpretation is how the target program (java.exe) parses the command line.


That said, in order to call java.exe, i.e. an external program, you don't need PowerShell at all; invoke it directly:

REM From cmd.exe
C:\> java -Doption1=3 -Doption2="true" jar.exe
mklement0
  • 382,024
  • 64
  • 607
  • 775