How to run a java file from command line passing arguments in Maven

0 votes

I have the following problem. I would like to run mvn from command line for a Main.java file. Main.java accepts a parameter. How do I do that from command line?

I tried finding an example but I was not successful. Could someone help me by giving me an example of that?

How do I execute that command from a different folder than the Main.java folder?

For example the Main.java is located in my/java/program/Main.java. What should I put in

mvn exec:java -Dexec.mainClass="what to put here?" -Dexec.args="arg0 arg1 arg2"
Jun 3, 2020 in Java by kartik
• 37,510 points
19,976 views

1 answer to this question.

0 votes

Hello @kartik,

You could run: mvn exec:exec -Dexec.args="arg1".

This will pass the argument arg1 to your program.

You should specify the main class fully qualified, for example, a Main.java that is in a package test would need

mvn exec:java  -Dexec.mainClass=test.Main

By using the -f parameter, you can also run it from other directories.

mvn exec:java -Dexec.mainClass=test.Main -f folder/pom.xm

For multiple arguments, simply separate them with a space as you would at the command line.

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="arg1 arg2 arg3"

For arguments separated with a space, you can group using 'argument separated with space' inside the quotation marks.

mvn exec:java -Dexec.mainClass=test.Main -Dexec.args="'argument separated with space' 'another one'"


Hope this helps and to know more about running Java file, join one of our Java certification course now.

answered Jun 3, 2020 by Niroj
• 82,880 points

Related Questions In Java

0 votes
1 answer

Is it possible to run a java program from command line on windows?How?

  Let's say your file is in C:\myprogram\ Run ...READ MORE

answered Apr 18, 2018 in Java by sophia
• 1,400 points
2,382 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 79,593 views
0 votes
1 answer

How to run a JAR file in Java?

The command given below will help you ...READ MORE

answered Jun 1, 2018 in Java by Parth
• 4,630 points
7,164 views
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 28, 2018 in Java by samarth295
• 2,220 points
2,138 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,077 views
0 votes
1 answer
0 votes
1 answer

Error:The superclass “javax.servlet.http.HttpServlet” was not found on the Java Build Path

Hello @kartik, Include servlet-api-3.1.jar in your dependencies. Maven <dependency> ...READ MORE

answered Jun 3, 2020 in Java by Niroj
• 82,880 points
3,867 views
0 votes
1 answer

How to run a Java program from the command line on Windows?

Hello @kartik, In case your Java class is ...READ MORE

answered Apr 9, 2020 in Java by Niroj
• 82,880 points
688 views
0 votes
1 answer

How to install Maven artifact with sources from command line?

Hello @kartik, To download sources for your dependencies: mvn ...READ MORE

answered Jun 3, 2020 in Java by Niroj
• 82,880 points
2,478 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP