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)

2
  • 2
    Are you talking about this? Commented Sep 10, 2019 at 23:59
  • Why don't just use PowerShell? That said you can use the EncodedCommand option Commented Sep 11, 2019 at 0:00

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

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.