Run -jar with args and use args in Spring

840 views Asked by At

I need to run jar with static parameter, for example, java -jar myapp-1.0.0.jar /path/static/myfolder, and use this path "/path/static/myfolder" in my code. So, I know how to get this parameters in PSVM (String..args), but how to get it in Spring? Is there annotation for this or may be in properties file>

2

There are 2 answers

1
JeffreyXue On

For example: java -jar -Dtest=abcd myapp-1.0.0.jar.

You can use System.getProperty("test") to get value.

-D{parameter} needs to be before that jar

1
trom On

It is really answer for my question:

create special @Component that implements CommandLineRunner and override method Run, like this:

@Component public class StartupPrintRunner implements CommandLineRunner {

private String folder;

@Override public void run(String... args) throws Exception { 
this.folder = Arrays.deepToString(args); }
}

Inject bean StartupPrintRunner where you need and get parameters. It returns String looks like an array: [parameter1, parameter2.., parameter n] and we can parse it to get necessary paarameter.