Java:将命名和未命名参数的组合传递给可执行的Jar / Main方法 [英] Java: Passing combination of named and unnamed parameters to executable Jar/Main Method

查看:254
本文介绍了Java:将命名和未命名参数的组合传递给可执行的Jar / Main方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法,以便我们可以将命名和未命名的参数传递给main方法。目前我传递的内容如下:

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

 java -jar myJar param1 param2 param3

并将其视为

  public static void main( String[] args ) throws IOException
  {
    String param1 = args[0];
    String param2=args[1];
     .....

我必须能够发送并处理它们,如下所示在Java中,我可以根据它们的名称读取值,并且每次执行Main方法时都不需要以相同的顺序传递参数。我可以使用一些外部库来使我的工作更轻松。

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 -jar myJar param3name=param3 param2name=param2 param1name=param1 param5 param6

and I希望将它们作为以下内容处理

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 link and is not comprehensive. Any input on how to accomplish the task?

推荐答案

我使用Apache Commons CLI解析java命令行参数。示例可以在此处找到,可用于执行以下任何操作选项格式:

Apache Commons CLI is what I use to parse java command line arguments. Examples can be found here and can be used to do any of the following option formats:


  • 类似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

  • 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)

这篇关于Java:将命名和未命名参数的组合传递给可执行的Jar / Main方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆