Search code examples
javapowershellcmdkeystore

how do I set keystore path in my java program in powershell script or cmd?


We use a CMD to call a Powershell script. In the powershell script, a Java program is called. Both files are in the same directory. In that directory is also a keystore file (zzzz.keystore) available that needs to be used by the java program. I know this can be done via -Djavax.net.ssl.trustStore=

I have used many ways of setting the path but all of them return error: "Could not find or load main class .net.ssl.trustStore=zzzz.keystore"

This is on a windows system. I'm not sure if I have to set something in the cmd that calls the powershell script to make it work. Please advice.

I have found this question: java SSL and cert keystore but there is no explicit example of the path set in windows. I have used these ways to add the path within the powershell script but none do work:

& java -Djavax.net.ssl.trustStore=zzzz.keystore com.router.router.router.Router

& java -Djavax.net.ssl.trustStore="zzzz.keystore" com.router.router.router.Router

& java -Djavax.net.ssl.trustStore="C:/path/to/file/zzzz.keystore" com.router.router.router.Router

& java -Djavax.net.ssl.trustStore="C://path//to//file//zzzz.keystore" com.router.router.router.Router

& java -Djavax.net.ssl.trustStore="C:\\path\\to\\file\\zzzz.keystore" com.router.router.router.Router

& java -Djavax.net.ssl.trustStore=C:/path/to/file/zzzz.keystore com.router.router.router.Router

& java -Djavax.net.ssl.trustStore=C://path//to//file//zzzz.keystore com.router.router.router.Router

Cmd file contains:

SET CLASSPATH=.\yyyyy.jar
powershell .\startscript.ps1

startscript.ps1

& java followed by script as shown above followed by parameters for Java object

I expect java to run without errors. I receive again and again java.exe : Error: Could not find or load main class .net.ssl.trustStore=zzzz.ke ystore

I know the keystore is good as it is used in other scripts where everything is set and called within the cmd script, there is no powershell involved.


Solution

  • This is because PowerShell sees the - in front of the java D switch, and thinks "oh, a parameter name".

    Prevent PowerShell from parsing it as a command parameter with --%:

    & java --% -Djavax.net.ssl.trustStore="C:\\path\\to\\file\\zzzz.keystore" com.router.router.router.Router
    

    or by either quoting or grouping the argument strings:

    & java '-Djavax.net.ssl.trustStore="C:\\path\\to\\file\\zzzz.keystore"' com.router.router.router.Router
    # or 
    & java @('-Djavax.net.ssl.trustStore="C:\\path\\to\\file\\zzzz.keystore"', 'com.router.router.router.Router')