1

Generally when we have to access command line arguments in Java we use, args[] But this will be fine when command line input is like this,

//Demo is class
C:\java Demo 1 2

we can access 1 and 2 simply by using args[0] and args[1].

But I have encountered a situation where the command line input will be like this,

C:\java Demo --num1=12 --num2=14

If I have to get above values of num1 and num2, how do I get it?

I googled a couple of times for command line arguments with minor modification, every time I got the simpler version only.

I came across this answer here

How do I parse command line arguments in Java?.

But I am looking for a simple Java version with no third party libraries. Yes they make our life easy, but I want to learn Java way first.

public static void main(String[] args) {
    //simpler way to access command line arguments
    int first_num = Integer.parseInt(args[0]);
    int second_num = Integer.parseInt(args[1]);

    System.out.println("num1= "+first_num+", num2= "+second_num);

}

Not actually sure if by using

C:\ java Demo --num1=12 --num2=13

we will be able to directly access num1 and num2 in our main method or not.

But I need those values to be available in main method.

Hope I am clear, please let me know if any info needed.

  • 1
    Possible duplicate of [How do I parse command line arguments in Java?](https://stackoverflow.com/questions/367706/how-do-i-parse-command-line-arguments-in-java) – MTCoster Jan 08 '19 at 13:02
  • Thanks for replying, I already saw that question and I am looking for a way without third party libraries. Using only Java – Akash Singh Jan 08 '19 at 13:06
  • 1
    The answer is that either you use third-party libraries or you start parsing yourself. What Java sees is two strings "--num1=12" and "--num2=13" and that's it. The third party libraries help you break down those strings into the parts that interest you. – RealSkeptic Jan 08 '19 at 13:06
  • 1
    If you want to parse it yourself, you can look into [`String.split`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/lang/String.html#split(java.lang.String)) or more advanced Regex matching using [`Pattern`](https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/regex/Pattern.html) with capturing groups – Hulk Jan 08 '19 at 13:07
  • @RealSkeptic okay so the "--", which seems like a command, doesn't make any difference, right ? – Akash Singh Jan 08 '19 at 13:08
  • 1
    @AkashSingh the double dash "--" is just a convention, it doesn't do anything by itself. See for example https://serverfault.com/questions/387935/whats-the-difference-betwen-the-single-dash-and-double-dash-flags-on-shell-comm for a common meaning – Hulk Jan 08 '19 at 13:09
  • @Hulk okay. I was just looking for a better method, I already tried this. Thanks – Akash Singh Jan 08 '19 at 13:10
  • 1
    Right. It's just a common way to pass arguments when some arguments are options and others are inputs/data. – RealSkeptic Jan 08 '19 at 13:11
  • @Hulk Ohh okay :) – Akash Singh Jan 08 '19 at 13:11
  • @RealSkeptic Okay cool :) – Akash Singh Jan 08 '19 at 13:12
  • 1
    Just be careful - you can get the parameters in different orders. You have to rely on the name of the parameter (num1 or num2) to know which is which, not on the order in `args`. – RealSkeptic Jan 08 '19 at 13:14

2 Answers2

2

To achieve what you want in a very simple manner you can try,

    public static void main(String[] args) {
    Map<String, String> argsMap = new LinkedHashMap<>();
    for (String arg: args) {
        String[] parts = arg.split("=");
        argsMap.put(parts[0], parts[1]);
    }

    argsMap.entrySet().forEach(arg-> {
        System.out.println(arg.getKey().replace("--", "") + "=" + arg.getValue());
    });
}

Note: There are more advance ways as per the duplicate post. But I guess you are looking for a very simple way.

CCoder
  • 151
  • 12
1

I would suggest to have a look at Apache Commons CLI that is exactly made for your requirements:

http://commons.apache.org/proper/commons-cli/

Read the intro first: http://commons.apache.org/proper/commons-cli/introduction.html

It has a even a nice fluent API:

Options options = new Options();
options.addOption(OptionBuilder.withLongOpt("num1")
                               .withDescription("some info about the usage")
                               .hasArg()
                               .withArgName("VALUE")
                               .create() );
Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82
  • Yup, I will do that as well, for now I wanted to know if any provision was inbuilt in Java or not, next I am working on that only. – Akash Singh Jan 08 '19 at 14:06