Run Main Class of Maven Project

run main class of Maven project

Try the maven-exec-plugin. From there:

mvn exec:java -Dexec.mainClass="com.example.Main"

This will run your class in the JVM. You can use -Dexec.args="arg0 arg1" to pass arguments.

If you're on Windows, apply quotes for exec.mainClass and exec.args:

mvn exec:java -D"exec.mainClass"="com.example.Main"

If you're doing this regularly, you can add the parameters into the pom.xml as well:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<executions>
<execution>
<goals>
<goal>java</goal>
</goals>
</execution>
</executions>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>foo</argument>
<argument>bar</argument>
</arguments>
</configuration>
</plugin>

Run main class compiled with Maven from terminal

As mentioned in comments, while using spring boot, you should use start-class instead of mainClass as the parameter in exec plugin. Try:

mvn exec:java -Dstart-class=com.company.Process

Alternatively, if you want to use java -cp to run your main class, you need to pass the correct dependencies. It seems that coolproject-1.0.0-SNAPSHOT.jar isn't built with dependencies that com.company.Process needs. To build a jar with all dependencies, you can use maven-assembly-plugin. Then you can try:

mvn package

java -cp target/coolproject-1.0.0-SNAPSHOT-jar-with-dependencies.jar com.mycompany.Process

How to execute maven main class with required user libraries?

JnetPcap requires you to reference two libraries in your project:

  1. The JAR file containing the Java interfaces you can use from your code (jnetpcap.jar)
  2. The native library that exposes OS specific functions to the Java library (i.e., libjnetpcap.so or jnetpcap.dll)

The exception that you are seeing indicates that you are missing #2 at runtime. In this case, you have two options to make the library available to your application:

You can find out which directories are on your path on Ubuntu by echoing the $PATH system variable:

> echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/snap/bin

Simply copy the native library into a directory that is already included on the system path.

Alternatively, you can pass the location of the library using the java.library.path argument to Java. Assuming the library is in a directory called lib inside your maven project directory, use the following configuration:

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<executable>java</executable>
<arguments>
<argument>-Djava.library.path=${project.basedir}/lib</argument>
<argument>-classpath</argument>
<classpath />
<argument>com.example.Main</argument>
</arguments>
</configuration>
</plugin>

To execute this, simply run:

mvn exec:exec

Call Java main method after maven build process

As per @azbarcea's comment, tried adding the following snippet to pom.xml properties

<maven.test.failure.ignore>true</maven.test.failure.ignore>

After adding the above code, Test.java is called and it is getting executed.

Maven Run Project

See the exec maven plugin. You can run Java classes using:

mvn exec:java -Dexec.mainClass="com.example.Main" [-Dexec.args="argument1"] ...

The invocation can be as simple as mvn exec:java if the plugin configuration is in your pom.xml. The plugin site on Mojohaus has a more detailed example.

<project>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.2.1</version>
<configuration>
<mainClass>com.example.Main</mainClass>
<arguments>
<argument>argument1</argument>
</arguments>
</configuration>
</plugin>
</plugins>
</build>
</project>

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

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.



Related Topics



Leave a reply



Submit