Maven build no main manifest attribute - java

I'm building a small app with Maven that has dependencies and is an executable jar file.
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>site-downloader</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.company.siteripper</groupId>
<artifactId>common-interfaces</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
<configuration>
<archive>
<manifest>
<mainClass>
com.company.Main
</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
The build completes with no issues but when executing the jar it writes
no main manifest attribute, in local maven repo / my jar.
I sought aid at the docs and Baeldung too to no avail.
I seek assistance to make the jar include its dependencies and is executable at the same time.
Edit:
I updated my pom to use the jar and the dependencies plugins instead of the assembly. Here is the updated pom:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.company</groupId>
<artifactId>site-downloader</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<properties>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>1.8</maven.compiler.source>
</properties>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/commons-io/commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.company.siteripper</groupId>
<artifactId>common-interfaces</artifactId>
<version>1.0.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
com.company.Main
</mainClass>
</manifest>
</archive>
</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}/libs
</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
But I get a NoClassDefFoundError when running it:
C:\Users\myusername\Documents\Commands>call java -jar
D:.m2\repository\com\company\site-downloader\1.0.0\site-downloader-1.0.0.jar
-9gag https://9gag.com/gag/arGEvRX?ref C:\Users\myusername\Documents\sites\data\android Error: A JNI error
has occurred, please check your installation and try again Exception
in thread "main" java.lang.NoClassDefFoundError:
org/openqa/selenium/WebDriver
at java.lang.Class.getDeclaredMethods0(Native Method)
at java.lang.Class.privateGetDeclaredMethods(Unknown Source)
at java.lang.Class.privateGetMethodRecursive(Unknown Source)
at java.lang.Class.getMethod0(Unknown Source)
at java.lang.Class.getMethod(Unknown Source)
at sun.launcher.LauncherHelper.validateMainClass(Unknown Source)
at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source) Caused by: java.lang.ClassNotFoundException:
org.openqa.selenium.WebDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 7 more

To create an executable jar, you have to use the following maven jar plugin. I provide below the code snippet.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>
your main class
</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
For more details, refer below the github link to get an idea of how to create executable/runnable jar.
https://github.com/debjava/runnableJar
As per docs, Maven Assembly Plugin for Maven is primarily intended to
allow users to aggregate the project output along with its
dependencies, modules, site documentation, and other files into a
single distributable archive.
Refer below the details of Maven Assembly plugin.
http://maven.apache.org/plugins/maven-assembly-plugin/

This is how I solved your issue:
Please ignore business logic,
since I am using MongoDB, added MongoDB driver as a dependency and able to run with java -jar command.
The end goal is same as of yours.
I am using maven-shade plug-in.
Checkout project from Github source code:
unzip, build, test
OR
1. Created a maven project
mvn archetype:generate -DarchetypeGroupId=org.apache.maven.archetypes -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4
Update pom.xml with following, added mongo client code from OP and printing collection names
Execute
mvn clean package
Execute
java -jar test.jar
output I got is:
INFO: Opened connection [connectionId{localValue:1, serverValue:9}] to localhost:27017
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, version=ServerVersion{versionList=[4, 0, 5]}, minWireVersion=0, maxWireVersion=7, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=2249770}
Jun 01, 2019 8:17:22 PM com.mongodb.diagnostics.logging.JULLogger log
INFO: Opened connection [connectionId{localValue:2, serverValue:10}] to localhost:27017
admin
config
local
test
[XenonSuite] Successfully connected to MongoDB
pom.xml(ignore package names)
Check shade plugin configuration
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>test</groupId>
<artifactId>test</artifactId>
<version>1.0-SNAPSHOT</version>
<name>test</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<mainClass>test.App</mainClass>
</properties>
<packaging>jar</packaging>
<dependencies>
<!-- https://mvnrepository.com/artifact/org.mongodb/mongo-java-driver -->
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>3.10.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>test.App</mainClass>
</transformer>
</transformers>
<finalName>test</finalName>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
There are multiple ways to solve this problem
Ref: https://www.baeldung.com/executable-jar-with-maven

Related

apache-13 error appears at package lifecycle with maven

I created a simple Maven project on a rasberry pi and my project connects to a mysql database to be able to insert in, so to handle the dependencies and the plugins I have this pom.xml but when I try to make mvn package I have the following error knowing that it worked a first time and that it did well insert in my database I tried to look at several examples on the internet without result. thank you in advance
The error:
[ERROR] Plugin org.apache.maven.plugins:maven-surefire-plugin:2.17 or
one of its dependencies could not be resolved: Failed to read
artifact descriptor for
org.apache.maven.plugins:maven-surefire-plugin:jar:2.17: 1 problem
was encountered while building the effective model for
org.apache.maven.plugins:maven-surefire-plugin:[unknown-version]
[ERROR] [FATAL] Non-parseable POM
/home/pi/.m2/repository/org/apache/apache/13/apache-13.pom: comment
started on line 3 and column 5 was not closed (position:
START_DOCUMENT seen ...\ufffd\u63D\ufffd\u1\u4
\ufffd\ufffd2/\ufffd\ufffd7\ufffd/\ufffd\u00\ufffd\ufffd\u3\u0
\ufffd\n\u0\u0\ufffd\u3\u0
\ufffd\u0\u0\ufffd\ufffd$\u40d\ufffd\ufffd\ufffd\ufffd\ufffd\u40\ufffd\ufffd\u3\u0\u13\ufffdI\ufffd\ufffd\u1a\u0\u0U\ufffd\ufffd\ufffd\u1au\ufffd...
#81:40) caused by: java.io.EOFException: no more data available
START_DOCUMENT seen ...\ufffd\u63D\ufffd\u1\u4
\ufffd\ufffd2/\ufffd\ufffd7\ufffd/\ufffd\u00\ufffd\ufffd\u3\u0
\ufffd\n\u0\u0\ufffd\u3\u0
\ufffd\u0\u0\ufffd\ufffd$\u40d\ufffd\ufffd\ufffd\ufffd\ufffd\u40\ufffd\ufffd\u3\u0\u13\ufffdI\ufffd\ufffd\u1a\u0\u0U\ufffd\ufffd\ufffd\u1au\ufffd...
#81:40 # /home/pi/.m2/repository/org/apache/apache/13/apache-13.pom,
line 81, column 40
pom.xml:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>csRunBI</groupId>
<artifactId>csRunBi</artifactId>
<packaging>jar</packaging>
<version>0.0.1-SNAPSHOT</version>
<url>http://maven.apache.org</url>
<properties>
<jodatime.version>2.5</jodatime.version>
<log4j.version>1.2.17</log4j.version>
</properties>
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.13</version>
</dependency>
<dependency>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.17</version>
<type>maven-plugin</type>
</dependency>
</dependencies>
<build>
<finalName>csRunProject</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.1.2</version>
<configuration>
<archive>
<manifest>
<!-- Jar file entry point -->
<addClasspath>true</addClasspath>
<mainClass>Main</mainClass>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>2.5.1</version>
<executions>
<execution>
<id>copy-dependencies</id>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>${project.build.directory}/dependency-jars/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
If you are calling the variable jdk.version with ${jdk.version} you should define it in your pom.xml. You can simply add the new variable between your <properties>...</properties> tag like it:
<properties>
<jodatime.version>2.5</jodatime.version>
<log4j.version>1.2.17</log4j.version>
<jdk.version>1.8</jdk.version>
</properties>
You can read more about the pom properties: pom properties documentation

How to fix "JAR will be empty - no content was marked for inclusion!" maven-jar-plugin?

When I run maven-jar-plugin, jar:jar i have this warning and empty jar-file
[WARNING] JAR will be empty - no content was marked for inclusion!
The build is successful but the produced jar-file is empty as the warning says.
How can I fix it? Im trying to find solution in ither questions, but all solutions not working
Here is my pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.actticus.instdbparse</groupId>
<artifactId>instDBParse</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.github.igor-suhorukov</groupId>
<artifactId>instagramscraper</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>org.brunocvcunha.instagram4j</groupId>
<artifactId>instagram4j</artifactId>
<version>1.8</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.14</version>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.3.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<index>true</index>
<manifest>
<mainClass>com.actticus.instdbparse.Main</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>1.6.0</version>
<configuration>
<cleanupDaemonThreads>false</cleanupDaemonThreads>
<classpathScope>test</classpathScope>
<mainClass>Main</mainClass>
</configuration>
<executions>
<execution>
<id>run-selenium</id>
<phase>integration-test</phase>
<goals><goal>java</goal></goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Don't run mvn jar:jar, but mvn clean package. The jar:jar goal just packages - without the lifecycle, there is nothing to package.
Look up "Maven lifecycle" to get more background information.

No classDefinitionFound error Spring Boot

I'm getting the following error when trying to run my jar file on the command line:
$ java -jar target/Controller-0.0.1-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: org/springframework/boot/SpringApplication
at com.vismark.Controller.App.main(App.java:11)
Caused by: java.lang.ClassNotFoundException: org.springframework.boot.SpringApplication
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:349)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 1 more
The project runs just fine on eclipse, but this error shows up when running it via commandline.
Below is my pom file:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.vismark</groupId>
<artifactId>Controller</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>Controller</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.0.4.RELEASE</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<mainClass>com.vismark.Controller.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
</project>
I'm likely missing something in my pom, I;m just not sure what it is. Where did I go wrong?
You most likely want to package an executable JAR. Since you are using Spring Boot you should follow chapter 71. Spring Boot Maven Plugin which explains how to do it using Spring Boot Maven Plugin.
Normally this is done by adding following build configuration to your pom.xml which be trigger during package phase.
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.0.4.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
As a note you are using ancient JUnit 3.8.1, you really should upgrade.

Execution of Scala and Maven project

I have created a simple maven project of Scala in eclipse's scala ide using below details -
Artifact Id - scala-archetype-simple
Group Id - net.alchim31.maven
After created the project I have modified the pom.xml file.
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.test</groupId>
<artifactId>scalapoc</artifactId>
<version>0.0.1</version>
<name>${project.artifactId}</name>
<description>Test scala app</description>
<properties>
<maven.compiler.source>1.6</maven.compiler.source>
<maven.compiler.target>1.6</maven.compiler.target>
<encoding>UTF-8</encoding>
<scala.version>2.11.5</scala.version>
<scala.compat.version>2.11</scala.compat.version>
</properties>
<dependencies>
<dependency>
<groupId>org.scala-lang</groupId>
<artifactId>scala-library</artifactId>
<version>${scala.version}</version>
</dependency>
<!-- Test -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-core_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.specs2</groupId>
<artifactId>specs2-junit_${scala.compat.version}</artifactId>
<version>2.4.16</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.scalatest</groupId>
<artifactId>scalatest_${scala.compat.version}</artifactId>
<version>2.2.4</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<sourceDirectory>src/main/scala</sourceDirectory>
<testSourceDirectory>src/test/scala</testSourceDirectory>
<plugins>
<plugin>
<!-- see http://davidb.github.com/scala-maven-plugin -->
<groupId>net.alchim31.maven</groupId>
<artifactId>scala-maven-plugin</artifactId>
<version>3.2.0</version>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>testCompile</goal>
</goals>
<configuration>
<args>
<!-- <arg>-make:transitive</arg> -->
<arg>-dependencyfile</arg>
<arg>${project.build.directory}/.scala_dependencies</arg>
</args>
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.18.1</version>
<configuration>
<useFile>false</useFile>
<disableXmlReport>true</disableXmlReport>
<!-- If you have classpath issue like NoDefClassError,... -->
<!-- useManifestOnlyJar>false</useManifestOnlyJar -->
<includes>
<include>**/*Test.*</include>
<include>**/*Suite.*</include>
</includes>
</configuration>
</plugin>
</plugins>
</build>
</project>
I have also added manifest entry inside plugin tag.
<archive>
<manifest>
<mainClass>App.scala</mainClass>
</manifest>
</archive>
I have only one App.scala file and one test file available in project. Build was successful. But when trying to execute the jar using java -jar <jar_name> getting below error -
no main manifest attribute, in scalapoc-0.0.1.jar
When trying to execute the command java -cp scalapoc-0.0.1.jar com.test.scalapoc.App.scala getting below error -
Error: Could not find or load main class com.test.scalapoc.App.scala
Please suggest what to be needed to execute the jar.
try
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.test.scalapoc.App</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
.scala is for source only, source is not executable
archive tag is part of the configuration of a plugin
see https://maven.apache.org/shared/maven-archiver/examples/classpath.html#Make
UPDATE 2018-06-05:
To create a standalone jar (without need to defined a classpath with scala-library and other dependencies on command line), you should create a jar that included other classes/jar.
see:
Apache Maven Shade Plugin – Executable JAR and the rest of the doc of the plugin for tuning & configuration
or the old one-jar tool Deliver Your Java Application in One-JAR™ !

ClassNotFoundException on a Maven Project using Selenium [duplicate]

This question already has answers here:
NoClassDefFoundError on Maven dependency
(7 answers)
Closed 5 years ago.
I wrote a small Maven Project in Netbeans using the Selenium HtmlUnitDriver.
The Project works when i'm starting it from the IDE. But when i'm using the command java -jar to open the jar file it reports an Error.
Exception in thread "main" java.lang.NoClassDefFoundError: org/openqa/selenium/htmlunit/HtmlUnitDriver
at com.mycompany.myPackage.classNameUI.<init>(classNameUI.java:26)
at com.mycompany.myPackage.mainClass.main(mainClass.java:20)
Caused by: java.lang.ClassNotFoundException: org.openqa.selenium.htmlunit.HtmlUnitDriver
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
... 2 more
the manifest.mf got the right Mainclass: path
Versions:
Netbeans 8.2
Maven 4.0.0
Selenium 2.53.1
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompany</groupId>
<artifactId>MyProject</artifactId>
<version>User</version>
<packaging>jar</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.53.1</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>com.mycompany.mypackage.mainClass</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
I think it might be because you don't include in your classpath the dependencies your program is using, to solve this:
Add to the "maven-jar-plugin" under <addClasspath>:
<classpathPrefix>lib/</classpathPrefix>
Then add this plugin to the build section in your pom.xml:
<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>
See the doc for more details: https://maven.apache.org/plugins/maven-dependency-plugin/usage.html

Categories