Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Use Maven to run java class in command prompt

 
Ranch Hand
Posts: 32
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm new to Maven and Netbeans 11.

I want to run a Java class using mvn exec:exec in command prompt.

Questions 1:
In Netbeans 11, my POM has no user-defined plugins. Just the default Netbeans POM.
I'm able to hit "Run" in Netbeans and it compiles and runs my program successfully.

Now I want to run the same Java class in command prompt. How do I do that?
I see that Netbeans has this set up in its Properties->Actions-Set properties like this: See attached file.

process-classes org.codehaus.mojo:exec-maven-plugin:1.6.0:exec
exec.args=-classpath %classpath ${packageClassName}
exec.executable=java

I don't know how to put that into a complete command prompt.

Thanks!
Annotation.jpg
[Thumbnail for Annotation.jpg]
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this code may help you:

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, as decribed here, 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
 
Marshal
Posts: 66980
255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
AC: Welcome to the Ranch
 
Saloon Keeper
Posts: 10994
243
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you always use the same main class in your project, you can simplify the process by configuring the exec-maven-plugin in your POM. Here's an example:

Then, in your project directory, all you need to do to run your application is type "mvn exec:exec".
 
henry leu
Ranch Hand
Posts: 32
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still cannot run it in command prompt.
My Netbeans 11 uses these properties to successfully compile and run my Java program.
process-classes org.codehaus.mojo:exec-maven-plugin:1.6.0:exec

exec.args=-classpath %classpath ${packageClassName}
exec.executable=java


However, I just don't know how to type those in the command prompt using Maven.
I want to run it using mvn exec:exec,not exec:java.

What exactly is %classpath and ${packageClassName} in Netbeans?
 
Stephan van Hulst
Saloon Keeper
Posts: 10994
243
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Uhh so what happened when you configured the plugin the way I showed you and then just ran mvn exec:exec?
 
You save more money with a clothesline than dozens of light bulb purchases. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!