Java - get command line arguments, OUTSIDE of main

2019-06-17 01:54发布

问题:

This question already has an answer here:

  • how to get the command line arguments from another class with java 5 answers

Is there a way in java to get the command line arguments that were passed to a program outside of the main function?

I'm writing some code that's part of a larger application I don't control, and I'd like to know what the command line args were, without modifying the main function.

Note: I've seen how to get the JVM arguments, and how to get the arguments in main, but would like to get it in other parts of our app, without having them saved in the main function, which I don't control.

回答1:

When starting the jvm this way: java -DmyArgument=myValue -jar your.jar you can get the value of myArgument everywhere in your java code with String myArg = System.getProperty("myArgument");