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

17
  • Try cleaning the project couple of times in eclipse or restart the eclipse and then try again. Commented May 13, 2020 at 4:56
  • Are you looking to create an uber JAR? Commented May 13, 2020 at 5:09
  • @SamuelAudet No, just build and execute it. whether simply with main class or with Jar file Commented May 13, 2020 at 6:24
  • @Smile I am building the project with command line. Commented May 13, 2020 at 6:25
  • 1
    open your jar file and see if there is manifest.mf file that file should contain main class attribute then only it will work Commented May 13, 2020 at 8:50

2 Answers 2

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> 
Sign up to request clarification or add additional context in comments.

Comments

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.

1 Comment

First solution by Adonis worked here: stackoverflow.com/questions/42470641/…

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.