Though the accepted answer is fine, this might also help somebody.
It seems you need to make sure you're building the Maven Project before running any plugin goals that rely on the compiled classes.
In case when you create a new java class, By the time you going to use the plugin goal, ClassNotFoundException
is thrown because there is no compiled version of that class (plugin rely on the compiled version of the class).
Just assume you have a plugin configuration like below in your pom.xml (NOTE : original SO question it mentions about running the main class directly without specifying it in a pom.xml, and how to do it is explained in the accepted answer by praneel),
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.myproj.java.Main</mainClass>
</configuration>
</plugin>
So before you run any plugin goals,
do
mvn clean install
then
mvn exec:java
or do,
mvn install exec:java