Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - get command line arguments, OUTSIDE of main? [duplicate]

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.

like image 736
Brad Parks Avatar asked Jan 16 '17 14:01

Brad Parks


People also ask

Can Java take command line arguments?

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.

Where does command line arguments are get stored in Java?

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( ).

How do you pass multiple arguments in Java main method?

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.


1 Answers

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");

like image 154
chris Avatar answered Oct 11 '22 22:10

chris