1

I tried to build this example : https://github.com/oltzen/JavaCppExample with Maven (mvn clean install) on Linux. After the successful build, when I run : java de.oltzen.javacppexample.Abc : it says 'could not load or find the main class'

The video tutorial (https://www.youtube.com/watch?v=LZrrqZLhtmw) uses Eclipse and it runs the program with Run as .. Java Application

Is the POM file missing something ?

I tried to add this plugin in POM but it did not work:

          <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <archive>
                    <manifest>
                        <addClasspath>true</addClasspath>
                        <classpathPrefix>lib/</classpathPrefix>
                        <mainClass>de.oltzen.javacppexample.Abc</mainClass>
                    </manifest>
                </archive>
            </configuration>
        </plugin>

I am executing from JavaCppExample/target/classes/ :-

The /classes folder contains package folders : de/oltzen/javacppexample/ The last folder contains the class file Abc.class

So I run the command :
java de.oltzen.javacppexample.Abc

The /target folder contains : 1) classes [folder containing the package] 2) JavaCppExample.jar 3) maven-archiver 4) maven-status

Please help

Nitin
  • 31
  • 5

2 Answers2

1

I added the following plugins for maven copy dependencies and executed java -jar javaCppExample.jar [from /target folder]and it worked. Thanks everyone !

[Simply build using mvn clean install]

<plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <target>1.8</target>
                    <source>1.8</source>
                </configuration>
            </plugin>


            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib/</outputDirectory>
                            <overWriteReleases>false</overWriteReleases>
                            <overWriteSnapshots>false</overWriteSnapshots>
                            <overWriteIfNewer>true</overWriteIfNewer>
                        </configuration>
                    </execution>
                </executions>
            </plugin>


            <plugin>
                <!--  Build an executable JAR -->
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>3.0.2</version>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <classpathPrefix>lib/</classpathPrefix>
                            <mainClass>de.oltzen.javacppexample.Abc</mainClass>
                        </manifest>
                    </archive>

                </configuration>
            </plugin>
Nitin
  • 31
  • 5
0

If you are only looking to run your prgramm via maven then use exec-maven-plugin.

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.6.0</version>
            <executions>
              <execution>
                <goals>
                  <goal>java</goal>
                </goals>
              </execution>
            </executions>
            <configuration>
              <mainClass>de.oltzen.javacppexample.Abc</mainClass>
            </configuration>
          </plugin>

Explore more here => https://www.mojohaus.org/exec-maven-plugin/index.html

If you want to build & run as jar then use assembly plugin

         <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-assembly-plugin</artifactId>
            <version>3.1.0</version>
            <configuration>
              <appendAssemblyId>true</appendAssemblyId>
              <descriptors>
                <descriptor>${project.basedir}/assembly/assembly.xml</descriptor>
              </descriptors>
              <descriptorRefs>
                <descriptorRef>jar-with-dependencies</descriptorRef>
              </descriptorRefs>
              <archive>
                <manifest>
                  <mainClass>de.oltzen.javacppexample.Abc</mainClass>
                </manifest>
              </archive>
            </configuration>
            <executions>
              <execution>
                <id>make-assembly</id>
                <phase>install</phase>
                <goals>
                  <goal>single</goal>
                </goals>
              </execution>
            </executions>
          </plugin>

Explore more about Maven assembly plugin to customize to your needs.

  • First solution by Adonis worked here: https://stackoverflow.com/questions/42470641/error-could-not-find-or-load-main-class-maven-project-out-of-eclipse – Nitin May 13 '20 at 11:05