How to pass Properties to jar from Powershell?

前端 未结 3 1430
一整个雨季
一整个雨季 2020-12-06 09:58

I\'ve been using Powershell-1.0 for command line needs for a while, instead of cmd.exe. Unfortunately, there are still some caveats when using Java. I need to pass a propert

相关标签:
3条回答
  • 2020-12-06 10:25

    Using quotes works fine for me in PowerShell on Windows 7.

    java "-Dmy.property=value" -jar myjar.jar
    

    Be careful: the jar name must be placed right after -jar, and arguments placed after -jar myjar.jar will be passed to the program inside the jarFile.

    0 讨论(0)
  • 2020-12-06 10:25

    Try launching instead using the following pattern:

    java -Duser.language=en -jar any.jar
    

    That assumes that user.language is meant as a system property. If you meant it as a command line argument, change that to:

    java -jar any.jar -Duser.language=en
    

    I am actually surprised that the command line you mentioned works at all outside of powershell (though I have confirmed that it works fine for me too, even on Linux) and it is also a little strange that things would work differently inside and outside of powershell.

    From java -help:

    Usage: java [-options] class [args...]                 
               (to execute a class)                        
       or  java [-options] -jar jarfile [args...]          
               (to execute a jar file)                     
    where options include:
    ...
        -D<name>=<value>
                      set a system property
    ...
    

    So basically you should always put the JAR filename directly after the -jar command line option, and any JVM options (such as setting system properties with -D) before.

    0 讨论(0)
  • 2020-12-06 10:35

    Take a look at my answer to this question. Note how you can use echoargs.exe to diagnose these sort of problems. Most likely the fix would be to quote the parameter e.g.:

    java -jar "-Duser.language=en" any.jar
    

    You can test that using echoargs (from PowerShell Community Extensions):

    echoargs -jar "-Duser.language=en" any.jar
    Arg 0 is <-jar>
    Arg 1 is <-Duser.language=en>
    Arg 2 is <any.jar>
    
    0 讨论(0)
提交回复
热议问题