开发者

Getting cli arguments through reflection in java

开发者 https://www.devze.com 2023-03-22 06:56 出处:网络
Is there any way to access the arguments in the args array other than as parameters to main? I\'m tinkering with a Disgruntled Bomb in java and I want to be able to get the cli arguments and call the

Is there any way to access the arguments in the args array other than as parameters to main? I'm tinkering with a Disgruntled Bomb in java and I want to be able to get the cli arguments and call the main function with them in random order or something like that.

public class Bomb{

  st开发者_如何转开发atic {
    main(new String[]{"arg"});
    System.exit(0);
  }

  public static void main(String[] args){
    for(int i = 0; i < args.length; i++){
      System.out.printf("arg %d: %s%n",i,args[i]);
    }
  }
}


On Sun's JDK, you can access the sun.java.command property:

String args = System.getProperty("sun.java.command");

However, you have to split the arguments yourself, and it's also not very portable to other JVMs.


I do not know if it what you are looking for, but the Apache CLI (Command Line Interface) lets you pass arguments in the "old" Unix way

-nameOfArgument1 valueOfArgument1 -nameOfArgument2 valueOfArgument2....


Just implement a main method in another class, and use that to call the original main with the randomization you require? Or does the call need to be exactly the same? In that case, how would you stop the call to main inself?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号