Java - get command line arguments, OUTSIDE of main?
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");
Related videos on Youtube
Brad Parks
Web programmer, interested in node js, cross platform development, and automating the things!
Updated on July 09, 2022Comments
-
Brad Parks almost 2 years
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.-
JFPicard over 7 yearsAlso check: stackoverflow.com/questions/2541627/…
-
Paul over 7 yearsYou could try the property "sun.java.command". It gives you access to the full command that was used to execute the jar (I'll check the docs, wether this is portable between JVM-implementations).
-
Paul over 7 years@BradParks update: as the prefix "sun" suggests, this property is most likely a oracle-jre specific system property. I haven't found any resources to verify this, so please handle with care.
-
chris over 7 yearsThis also works on windows 7 with the oracle jre.
-
-
Brad Parks over 7 yearsThanks... that's helpful, but the args that I'm looking for aren't passed as
-D
style arguments....