How to run a maven project/main class in netbeans without building first?

15,675

Solution 1

Not found a way to run this in Netbeans, but as a workaround am running the project from the command line using:

mvn exec:java -Dexec.mainClass="com.rory.djgx.server.Main"

Just need to ensure this is executed in the root directory of the compiled classes (.class), e.g. com/rory/djgx and that the pom.xml is in this root directory.

Solution 2

If you want to just run the Build / Compile have an option to use all the powers of the Build Phases, as Validate, Build, Test, Package, Integration, Test, Verify, Install or Deploy. To do this you must:

-Right Click on Project -> Custom -> Goals ...

-In the Goals you can choose the more specifically option choice to build whatever you want, like Compile, Deploy, etc...

Solution 3

Maybe too late to answer but I had the same problem today with NetBeans 11.

You can configure this with right click on the given project and select "Properties" at the bottom of the popup menu. You need to select the "Actions" category and then select the "Run file via main()" action.

The original properties

Execute Goals:

process-classes org.codehaus.mojo:exec-maven-plugin:1.5.0:exec

Set properties:

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

You need to change both of them

Execute Goals:

process-classes org.codehaus.mojo:exec-maven-plugin:1.6.0:java

Set properties:

exec.mainClass=${packageClassName}
exec.cleanupDaemonThreads=false
exec.classpathScope=compile

I have also changed the version (and the executed goal as well) of the maven-exec-plugin to 1.6.0

After saving the new configuration with the OK button you can right click on the edited java file and select "Run File" and the public static void main(String[] args) method will be executed.

I also attach the NetBeans screen for reference:

enter image description here

Share:
15,675
chetan godiya
Author by

chetan godiya

please delete me

Updated on July 05, 2022

Comments

  • chetan godiya
    chetan godiya almost 2 years

    I have a maven project in the latest version of Netbeans but due to an undetermined problem with my environment/maven setup I have to build the project from the command line using gmake as building with mvn clean install comes up with a lot of errors.

    So, I was wondering as I'm building from the command line, when using netbeans to run the project/main class, how can I just run it without it building/compiling first - i.e. every time I right click the main class and select run file - its will say - 'Building...' - can I just run the file without building/compiling?

    Thanks!

  • Samuel
    Samuel over 10 years
    Could you please explain it further? Every time I run maven project from Netbeans it has to build first. Even with no changes, I just can't only re-run it. Could I skip building with your solution?