Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Use Maven to run java class in command prompt

 
Ranch Hand
Posts: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 76489
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
AC: Welcome to the Ranch
 
Saloon Keeper
Posts: 14327
321
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 127
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 14327
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
When I was younger I felt like a man trapped inside a woman’s body. Then I was born. My twin is a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic