java 如何将属性从 Powershell 传递给 jar?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1518698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 16:52:44  来源:igfitidea点击:

How to pass Properties to jar from Powershell?

javapowershelljarproperties

提问by jgran

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 property to a jar, like that:

我一直在使用 Powershell-1.0 来满足命令行需求,而不是 cmd.exe。不幸的是,在使用 Java 时仍有一些注意事项。我需要将一个属性传递给一个 jar,如下所示:

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

This line works fine in cmd.exe, but not in Powershell as it searches for another jar:

此行在 cmd.exe 中工作正常,但在 Powershell 中无法正常工作,因为它会搜索另一个 jar:

Unable to access jarfile user.language=en

无法访问 jarfile user.language=en

Using quotes doesn't help.

使用引号没有帮助。

Is it doable in Powershell-1.0, or do I miss something in Java?

它在 Powershell-1.0 中是否可行,或者我是否遗漏了 Java 中的某些内容?

回答by Keith Hill

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.:

看看我对这个问题的回答。请注意如何使用 echoargs.exe 来诊断此类问题。最有可能的解决方法是引用参数,例如:

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

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

您可以使用 echoargs(来自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>

回答by mmuller

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

在 Windows 7 上的 PowerShell 中使用引号对我来说效果很好。

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

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

注意:jar 名称必须紧跟在 之后-jar,放在之后的参数-jar myjar.jar将传递给jarFile的程序。

回答by Adam Batkin

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:

假设 user.language 是系统属性。如果您将其作为命令行参数,请将其更改为:

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.

我实际上很惊讶你提到的命令行在 powershell 之外完全有效(尽管我已经确认它对我来说也很好用,即使在 Linux 上也是如此)而且有点奇怪的是,在 powershell 内部和外部的工作方式不同.

From java -help:

来自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 -jarcommand line option, and any JVM options (such as setting system properties with -D) before.

所以基本上你应该总是把 JAR 文件名直接放在-jar命令行选项之后,任何 JVM 选项(比如用 设置系统属性-D)之前。