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.