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.
A Java application can accept any number of arguments from the command line. This allows the user to specify configuration information when the application is launched. When an application is launched, the runtime system passes the command-line arguments to the application's main method via an array of String s.
To access the command-line arguments inside a Java program is quite easy. They are stored as strings in the String array passed to main( ).
Use the String-Array to pass the parameters in. The main-Method has only the args parameter. You can pass all owners into the array, then put a limiter String into it (which can't be an owner or consumer) and then put all consumers into the array. In the main you iterate over the args-array and create two arrays of it.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With