Could not find or load main class : Executable JAR from Maven - java

I am trying to create an executable JAR from Maven with maven-assembly-plugin. My Jars are getting created but without my source code. I have already mentioned goal as single in POM and creating jar with this command: "mvn clean package assembly:single". But I am still getting this error: "Error: Could not find or load main class com.som.bau.report.MainClass".
Please see below my 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>VFIE_SOM_BAU_Report</groupId>
<artifactId>SOM_BAU_Report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<jdk.version>1.7</jdk.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>com.som.bau.report.MainClass</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<dependencies>
<!-- Dependencies code goes here ---!>
</dependencies>
</project>

You should append packaging type to root element.
...
<modelVersion>4.0.0</modelVersion>
<groupId>VFIE_SOM_BAU_Report</groupId>
<artifactId>SOM_BAU_Report</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
...

I found the solution for it.
Need to add this tag under build tag.
<sourceDirectory>src</sourceDirectory>

Related

ANTLR4 CharStream class missing

I am using ANTLR4 to parse command lines for my Java Shell project.
When I run a JUnit test in VSCode, everything is fine. However, when I am building the Docker image and I try to run the shell in interactive mode, I get this error:
Error: Unable to initialize main class uk.ac.ucl.jsh.Jsh
Caused by: java.lang.NoClassDefFoundError: org/antlr/v4/runtime/CharStream
Is there any issue with my pom.xml file, or is the problem coming from somewhere else? This is my 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>uk.ac.ucl.jsh</groupId>
<artifactId>jsh</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>jsh</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
<version>4.7.2</version>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<argLine>-XX:MaxPermSize=512m</argLine>
</properties>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.4</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>uk.ac.ucl.jsh.Jsh</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.antlr</groupId>
<artifactId>antlr4-maven-plugin</artifactId>
<version>4.7.2</version>
<executions>
<execution>
<goals>
<goal>antlr4</goal>
</goals>
</execution>
</executions>
<configuration>
<visitor>true</visitor>
<listener>true</listener>
<outputDirectory>
${basedir}/src/main/java/
</outputDirectory>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<reporting>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<reportSets>
<reportSet>
<reports>
<!-- select non-aggregate reports -->
<report>report</report>
</reports>
</reportSet>
</reportSets>
</plugin>
</plugins>
</reporting>
</project>
This doesn't really look like an ANTLR problem. You'll need to look into how you're doing your build and how you're putting your Docker image together.
There are a couple of solutions to this.
Look into how to have Maven produce an Uber jar (i.e. a jar file that bundles up all of it's dependencies)
Ensure that your Docker image has the ANTLR runtime included and in your class path.
The first of those is probably the more "normal" solutions these days.

How to make sure that "java" finds the all the required classes when IDE can?

I have a Maven project with only one dependency :
<dependency>
<groupId>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
I am using an IDE and it installed all the required dependencies itself. I have written a simple code that uses the classes from net.sf.jasperreports. To create a the package I did mvn package and it created the jar. When I try to run the jar as java -jar myjar.jar, I get an error saying Caused by: java.lang.ClassNotFoundException: net.sf.jasperreports.engine.JRDataSource
I cannot understand how to resolve this. The IDE is able to find the classes but when invoked from command-line, it fails to find the classes. Why is that? What should I do?
I even tried to keep the jasperreports-6.8.0.jar at /Library/Java/Extensions/ but that too did not work.
Here is how the pom.xml looks like:
<?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.suhail</groupId>
<artifactId>JasperCSVDataSource</artifactId>
<version>1.0-SNAPSHOT</version>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<configuration>
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
</plugin>
</plugins>
</build>
<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>net.sf.jasperreports</groupId>
<artifactId>jasperreports</artifactId>
<version>6.8.0</version>
</dependency>
</dependencies>
</project>
This is the part that uses the maven shade plugin in my project, which is of course totally different from yours:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<shadedArtifactAttached>true</shadedArtifactAttached>
<artifactSet>
<includes>
<include>org.apache.poi:poi</include>
<include>org.apache.poi:poi-ooxml</include>
<include>org.apache.poi:poi-ooxml-schemas</include>
<include>org.apache.commons:commons-collections4</include>
<include>org.apache.xmlbeans:xmlbeans</include>
<include>org.apache.commons:commons-compress</include>
</includes>
<excludes>
<!-- Exclude main jar to avoid error 'Failed to create shaded artifact,
project main artifact does not exist' -->
<exclude>${project.groupId}:${project.artifactId}</exclude>
</excludes>
</artifactSet>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.1.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<appendAssemblyId>false</appendAssemblyId>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
Please note that this part of the pom.xml goes into the <plugins> ... </plugins> section, in my case it is below the maven compiler plugin and below the maven jar plugin. You can explicitly include certain dependencies or explicitly exclude certain ones.
The result will be 2 files in your target folder, I personally use the one that has the original name, the other one will have the original name extended by shaded or similar.
If you get an error stating No main manifest attribute in ... then change your configuration of the maven jar plugin as follows:
<configuration>
<archive>
<manifestFile>${project.build.outputDirectory}/META-INF/MANIFEST.MF</manifestFile>
</archive>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</configuration>
Use Maven assembly plugin instead Maven jar plugin.
see what-are-the-differences-between-maven-jar-plugin-and-maven-assembly-plugin!
Maven assembly plugin creates a fully deployable package with all dependencies packed in it.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<!-- get all project dependencies -->
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<!-- MainClass in mainfest make a executable jar -->
<archive>
<manifest>
<mainClass>com.suhail.main.CommandLineRunner</mainClass>
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<!-- bind to the packaging phase -->
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>

Maven package add jars from dependencies to jar

I'm trying to create a jar from my maven project containing the jar files from dependencies in a specific folder.
The framework I'm writing a plugin for requires external dependencies to be supplied as jar files in the plugin jar.
For example:
xxxx.jar
/myapp/myjavaclasses
/lib/externalDependencies.jar
My pom file:
<?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>AAAAA</groupId>
<artifactId>BBBBBBB</artifactId>
<packaging>jar</packaging>
<version>1.1</version>
<dependencies>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.8</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<configuration>
<archive>
<manifestEntries>
<Rundeck-Plugin-Classnames>CCCCC</Rundeck-Plugin-Classnames>
<Rundeck-Plugin-Version>1.1</Rundeck-Plugin-Version>
<Rundeck-Plugin-Archive>true</Rundeck-Plugin-Archive>
<Rundeck-Plugin-File-Version>${project.version}</Rundeck-Plugin-File-Version>
<Rundeck-Plugin-Libs>lib/httpclient-4.5.8.jar lib/httpcore-4.4.11.jar</Rundeck-Plugin-Libs>
</manifestEntries>
</archive>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.5.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
In the above pom file we have the dependency
org.apache.httpcomponents
, this dependency contains the following jars
httpclient-4.5.8.jar
httpcore-4.4.11.jar
these jars need to be imported in my final jar under the folder /lib.
Thanks to #ernest_k came to a solution using
<plugin>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>copy-dependencies</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/lib</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
and
<resources>
<resource>
<directory>.</directory>
<includes>
<include>lib/**/*.jar</include>
</includes>
</resource>
</resources>

maven compilation error : The filename, directory name, or volume label syntax is incorrect

Am compiling my pom.xml file and am getting the below compilation error
Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.5.1:copy-dependencies (copy-dependencies) on project ElasticSearchUtility: Error copying artifact from C:\Users\10641516\.m2\repository\org\apache\lucene\lucene-sandbox\7.1.0\lucene-sandbox-7.1.0.jar to D:\Karthikeyan\ElasticSearch\ElasticSearch_Tesing\target\dependency-
[ERROR] jars\lucene-sandbox-7.1.0.jar: The filename, directory name, or volume label syntax is incorrect
But the required .jar file is available in the specified path.
I have created two java file my maven project one is POJO class and another one is java main class file. I want to make my project as an executable jar file which i want to run externally using java -jar command.
Please find my 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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>ElasticSearchUtility</groupId>
<artifactId>ElasticSearchUtility</artifactId>
<version>1.0.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.1.2</version>
</dependency>
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>rest</artifactId>
<version>5.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<!-- Set a compiler level -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
<!-- Make this jar executable -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<configuration>
<excludes>
<exclude>**/log4j.properties</exclude>
</excludes>
<archive>
<manifest>
<addClasspath>true</addClasspath>
<mainClass>ProjectJar.project.App</mainClass>
<classpathPrefix>dependency-jars/</classpathPrefix>
</manifest>
</archive>
</configuration>
</plugin>
<!-- Copy project dependency -->
<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>
</projects>
The sawn off error message:
... D:\Karthikeyan\ElasticSearch\ElasticSearch_Tesing\target\dependency-
suggests that you need to change the outputDirectory configuration of the maven-dependency-plugin to:
<configuration>
<!-- exclude junit, we need runtime dependency only -->
<includeScope>runtime</includeScope>
<outputDirectory>
${project.build.directory}/dependency-jars/
</outputDirectory>
</configuration>
to get rid of the newline in the middle the value.

Launch multi module maven/spring project

i'm new to maven and i've finished my app i can lauch it from eclipse and there is no error when i run mvn clean package but i tried to lauch it with mvn spring-boot:run and i 've this error :
java.lang.ClassNotFoundException: com.o2xp.ats.utils.App
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
at org.springframework.boot.maven.AbstractRunMojo$LaunchRunner.run(AbstractRunMojo.java:522)
at java.lang.Thread.run(Thread.java:748)
but i've specified the path of my main class in the parent pom :
<groupId>com.o2xp</groupId>
<artifactId>ats-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>ats-parent</name>
<modules>
<module>ats-api</module>
<module>ats-impl</module>
<module>ats-client</module>
</modules>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<start-class>com.o2xp.ats.utils.App</start-class>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<!-- Version -->
<immutables.version>2.5.5</immutables.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.6.1</version>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.5.4.RELEASE</version>
<configuration>
<mainClass>com.o2xp.ats.utils.App</mainClass>
</configuration>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
the main class is in a sub module from the module ats-impl which is a module of ats-parent
can you explain me what is wrong with my configuration and if it's possible to maybe have a clear and detailed tutorial it would really help me to understand how this is working.
Try to add repackage goal to the plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
<configuration>
<classifier>exec</classifier>
</configuration>
</execution>
</executions>
</plugin>
I've succeed by using this command mvn exec:java -pl ats-utils -Dexec.mainClass=com.o2xp.ats.utils.App i specify that i had to run this command from the ats-impl module (look at my pom).

Categories

Resources