CoderJammer
CoderJammer

Reputation: 725

SpringBoot Passing parameter to CommandLineRunner

I'm using SpringBoot 2.1.3 and I'm trying to passing a number of parameters through command line.

Here is the main class:

@SpringBootApplication
@Slf4j
public class Application implements CommandLineRunner {

@Value("${priceOne.pound}")
private Integer p1pound;

@Value("${priceOne.shilling}")
private Integer p1shilling;

@Value("${priceOne.pences}")
private Integer p1pences;

@Value("${priceTwo.pound}")
private Integer p2pound;

@Value("${priceTwo.shilling}")
private Integer p2shilling;

@Value("${priceTwo.pences}")
private Integer p2pences;

@Value("${factor}")
private Integer factor;

@Value("${op}")
private String op;

public static void main(String[] args) {
    SpringApplication.run(EurisApplication.class, args);
}

@Override
public void run(String... args) {
    Price priceOne = new Price(p1pound, p1shilling, p1pences);
    Price priceTwo = new Price(p2pound, p2shilling, p2pences);
    ....
    ....
}

I tried to fill those variables with:

spring-boot:run -Drun.arguments=--priceOne.pound=5, --priceOne.shilling=5, --priceOne.pences=5, --priceTwo.pound=5, --priceTwo.shilling=5, --priceTwo.pences=5, --factor=0, --op=+

or with:

spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5, --priceOne.shilling=5, --priceOne.pences=5, --priceTwo.pound=5, --priceTwo.shilling=5, --priceTwo.pences=5, --factor=0, --op=+

if I add quotes " " at the beginning and at the end of the parameters list I obtain a conversion error..

Can you help me?

Thanks a lot.


Thanks for your replies!

if I remove all the parameters eccept the first and I launch the application with:

 spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5

Works fine. Other ways you mentioned as:

 spring-boot:run --priceOne.pound=5

retrieve a parsing exception both with one parameter and with many. I'm a little bit confused..

it took me less time to write the whole application than to write that damned commandLine launch. I can't get all the parameters as a single String.. I don't understand why with just one parameter works and with two not!

Thanks

Upvotes: 1

Views: 13483

Answers (3)

CoderJammer
CoderJammer

Reputation: 725

Reading around (especially the baldeung docS) I found a solution to the problem on my own (lately it happens in 85% of cases, perhaps the forum has become too big..).

Then I post the solution hoping it will save time to somebody else. Now, considering the code above (which I propose again for clarity):

@SpringBootApplication
@Slf4j
public class Application implements CommandLineRunner {

@Value("${priceOne.pound}")
private Integer p1pound;

@Value("${priceOne.shilling}")
private Integer p1shilling;

@Value("${priceOne.pences}")
private Integer p1pences;

@Value("${priceTwo.pound}")
private Integer p2pound;

@Value("${priceTwo.shilling}")
private Integer p2shilling;

@Value("${priceTwo.pences}")
private Integer p2pences;

@Value("${factor}")
private Integer factor;

@Value("${op}")
private String op;

public static void main(String[] args) {
   SpringApplication.run(EurisApplication.class, args);
}

@Override
public void run(String... args) {
 System.out.println(p1pound + " " + p1shilling + " " + p1pences + " " +
                    p2pound + " " + p2shilling + " " + p2pences + " " +
                    factor + " " + op;
}

If you want to launch the application with multiple parameters with maven or inside Eclipse using maven launch configurator (see picture below)

enter image description here

the string you have to use is:

spring-boot:run -Dspring-boot.run.arguments=--spring.main.banner-mode=off,--priceOne.pound=5,--priceOne.shilling=5,--priceOne.pences=5,--priceTwo.pound=4,--priceTwo.shilling=4,--priceTwo.pences=4,--factor=5,--op=+

The String above will produce, according with the code inside run() method, the following output:

5 5 5 4 4 4 5 +

If you build the Application (with Jar Packaging) and want to launch it from Terminal (cmd in Windows for Example) you should use the following String:

java -jar [package_name].jar --priceOne.pound=5 --priceOne.shilling=5 --priceOne.pences=5 --priceTwo.pound=4 --priceTwo.shilling=4 --priceTwo.pences=4 --factor=5 --op=+

and it will produce the same result.

If you want to pass just one parameter you should use the following String with maven:

spring-boot:run -Dspring-boot.run.arguments=--priceOne.pound=5

And from cmd:

java -jar [package_name].jar --priceOne.pound=5

P.S.: These solutions work with SpringBoot ver >= 2.x

Hope helps.

Upvotes: 0

Nicolas
Nicolas

Reputation: 1186

As @improbable mentioned, -Dspring-boot.run.arguments is used to pass arguments to the main() method.

In order to set @Value attributes, directly use the -- syntax:

spring-boot:run --priceOne.pound=5 --priceOne.shilling=5 --priceOne.pences=5, --priceTwo.pound=5 --priceTwo.shilling=5 --priceTwo.pences=5 --factor=0 --op=+

Please check the relevant documentation

Upvotes: 5

improbable
improbable

Reputation: 2954

You are misusing @Value. It's used for injecting externalized properties.

If you don't have externalized properties that correspond - all these fields will be null.

Apart from that, in

@Override
public void run(String... args) {

String... args are your command line parameters in the form of varargs.

For instance, if you execute next line, you'll have varargs array that contains single element "first_arg_string", which is grabbed by index.

java -jar yourjar.jar "first_arg_string"

Upvotes: 2

Related Questions