My scenario:
I have a Main.java file that simply does System.out.println("Hello")
.
I run it by first, compiling with javac Main.java
and then excecuting the command java Main
.
Now what I want is that instead of printing "Hello", it will print whatever the user wants, but I don't want to change the source code whenever I want a different output. So I replaced the System.out.println("Hello")
with System.out.println(${MESSAGE})
. But this gives error "Cannot resolve symbol MESSAGE".
Ultimately, I want a Main.class file and run with something like java Main -env MESSAGE=whateverIPutHere
and it should print out whateverIPutHere.
Is it possible?
You can use the input arguments:
public static void main(String[] args) {
System.out.println(args[0]);
}
And then call it like this: java Main whateverIPutHere
Simple as that! args
is an array containing all the arguments that you pass in the command line.