简体   繁体   中英

Java command line set argument with “=”

I have a method:

public String sample (String A, int B, int C){
     String fin = "";
     if(C==0){
     fin="fail";
     }
     return fin;
}

I want to run it in command line like this: d:>java -jar prac.jar B=5 How can I set parameter in command line with "="?

In your main method, you deal with parameters that are passed:

public static void main(String[] args) {
    // args[0] = parameter 1;
    // args[1] = parameter 2;
    // args[n] = parameter n+1;
    sample(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2])); 
}

you need to do in the main something like parsing the args to integer:

use Integer.parseInt() ... and validate the input is a numeric parsable value...

public static void main(String[] args) {
    System.out.println(sample(args[0], Integer.parseInt(args[1]), Integer.parseInt(args[2])));
}

public static String sample(String A, int B, int C) {
    String fin = "";
    if (C == 0) {
        fin = "fail";
    }
    return fin;
}

and to run it from the cmd

java StringA n1 n2

Example:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM