Java:将命名和未命名参数的组合传递给可执行的 Jar/Main 方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/24891990/
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
Java: Passing combination of named and unnamed parameters to executable Jar/Main Method
提问by sun_dare
I am looking for a way so that we can pass both named and unnamed arguments to the main method. Currently I am passing them as follows
我正在寻找一种方法,以便我们可以将命名和未命名的参数传递给 main 方法。目前我通过它们如下
java -jar myJar param1 param2 param3
and handling them as
并将它们处理为
public static void main( String[] args ) throws IOException
{
String param1 = args[0];
String param2=args[1];
.....
I must be able to send them and handle them as below in Java so that I can read the values based on their names and need not pass the arguments in the same order everytime I execute the Main method. I am fine to work with some external libraries which would make my work easier.
我必须能够在 Java 中发送和处理它们,如下所示,以便我可以根据它们的名称读取值,并且每次执行 Main 方法时都不需要以相同的顺序传递参数。我可以使用一些外部库,这将使我的工作更轻松。
I am want to send my arguments as below and handle it in java main
我想发送我的参数如下并在 java main 中处理它
java -jar myJar param3name=param3 param2name=param2 param1name=param1 param5 param6
and I want to handle them as something below
我想把它们当作下面的东西来处理
public static void main( String[] args ) throws IOException
{
//something like
String param3 = getvaluemethod("param3name");
String param1 = getvaluemethod("param1name");
.....
String Param5 =args[n]
String param6 = args[n+1]
.....
I have already seen this linkand is not comprehensive. Any input on how to accomplish the task?
我已经看过这个链接,并不全面。关于如何完成任务的任何输入?
采纳答案by Archangel33
Apache Commons CLI is what I use to parse java command line arguments. Examples can be found hereand can be used to do any of the following option formats:
Apache Commons CLI 是我用来解析 java 命令行参数的工具。可以在此处找到示例,可用于执行以下任何选项格式:
- POSIX like options (ie.
tar -zxvf foo.tar.gz
) - GNU like long options (ie.
du --human-readable --max-depth=1
) - Java like properties (ie.
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
) - Short options with value attached (ie.
gcc -O2 foo.c
) - long options with single hyphen (ie.
ant -projecthelp
)
- POSIX 之类的选项(即。
tar -zxvf foo.tar.gz
) - GNU 喜欢长选项(即。
du --human-readable --max-depth=1
) - Java 类属性(即。
java -Djava.awt.headless=true -Djava.net.useSystemProxies=true Foo
) - 附加价值的短期权(即。
gcc -O2 foo.c
) - 带有单个连字符的长选项(即
ant -projecthelp
)
回答by Mac70
As long as params and names don't contain spaces - you can get all of them, split at "=" key and add key/value pairs to the HashMap. Later you can just get any value you want using key.
只要参数和名称不包含空格 - 您可以获取所有这些,在“=”键处拆分并将键/值对添加到 HashMap。稍后您可以使用 key 获取任何您想要的值。
Edit: If you want to not add some elements to the map, then you can ignore them if these elements don't contain "=" key.
编辑:如果您不想向地图添加某些元素,那么如果这些元素不包含“=”键,则可以忽略它们。
回答by Phani Rahul
Based on @Mac70's answer and a few additions,
基于@Mac70 的回答和一些补充,
private static Map<String, String> map;
private static void makeMap(String[] args) {
map = new HashMap<>();
for (String arg : args) {
if (arg.contains("=")) {
//works only if the key doesn't have any '='
map.put(arg.substring(0, arg.indexOf('=')),
arg.substring(arg.indexOf('=') + 1));
}
}
}
public static void main(String[] args) {
makeMap(args);
//..
String param3 = map.get("param3name");
String param1 = map.get("param1name");
}
If you need anything extensive, you need to look at @Archangel33's answer.
如果您需要任何广泛的内容,则需要查看@Archangel33 的答案。