IntelliJ - Maven adding external jar file but java.lang.NoClassDefFoundError - java

I am using IntelliJ version 11.0.7 (2020.1.3) created a simple maven project and added my jar to it by
File -> Project Structure -> New Project Library -> Java -> Selected my jar -> Ok -> Ok
in that jar file, all the dependencies present which requires to run the application.
There are no compile-time errors but when I run my maven project then it is throwing this exception:
Exception in thread "main" java.lang.NoClassDefFoundError: com/fasterxml/jackson/core/JsonProcessingException
After adding this jar it is throwing an exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.
Is there any way to auto-generate all the dependencies and add to External Libraries from the jar when I added to it?

in that jar file, there are all the dependencies present which requires to run the application.
Are you sure that all dependencies present? Your next statement saying After adding this jar it is throwing exception about the next missing jar, likewise when I added all the dependencies which are used inside that jar then everything works fine.
Is your jar file hosted in maven repository? If yes, simply declare it in maven pom.xml file, it will manage all the transitive dependencies. If it is not in maven repository, you need to run mvn install command to install that into your local maven repository, later on refer it in your pom.xml file. It will auto resolve your transitive dependencies as well, if you package your jar file properly which include correct pom.xml inside.

Finally, I added this in my pom.xml where I generated the Jar file
To get Maven to build a Fat JAR from your project you must include a Fat JAR build configuration in your project's POM file. You configure Maven to build a Fat JAR from your project by including the maven-assembly-plugin in your POM file's plugin section. Maven refers to an output product that it builds as an assembly. Hence the name maven-assembly-plugin.
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
Now it is working as expected.
Reference: http://tutorials.jenkov.com/maven/maven-build-fat-jar.html

Related

java.lang.NoClassDefFoundError: org/telegram/telegrambots/meta/exceptions/TelegramApiException

I'm trying to deploy my first java application using Maven. In this case, this is just a simply telegram bot, but I get this error when trying to run it locally. After a little investigation, I found that java.lang.NoClassDefFoundError is an error that occurs when a jar file is not able to access a specific class in runtime, and in order to solve this, is necessary to add that class on classpath.
I understand that when working on Maven, there is a simple way to add classes on the classpath, and it's by adding the right dependency on the pom.xml file.
So this is what i've added:
<dependencies>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-abilities</artifactId>
<version>5.0.1.1</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots</artifactId>
<version>5.0.1</version>
</dependency>
<dependency>
<groupId>org.telegram</groupId>
<artifactId>telegrambots-meta</artifactId>
<version>5.0.1.1</version>
</dependency>
</dependencies>
And I think it was successfully added on the classpath because this is what I get when I read the MANIFEST.MF file on my jar file:
Manifest-Version: 1.0
Created-By: Apache Maven 3.6.3
Built-By: agujared
Build-Jdk: 15.0.1
Class-Path: telegrambots-abilities-5.0.1.1.jar commons-lang3-3.11.jar ma
pdb-3.0.8.jar kotlin-stdlib-1.2.71.jar kotlin-stdlib-common-1.2.71.jar
annotations-13.0.jar eclipse-collections-api-11.0.0.M1.jar eclipse-coll
ections-11.0.0.M1.jar eclipse-collections-forkjoin-11.0.0.M1.jar lz4-1.
3.0.jar elsa-3.0.0-M5.jar slf4j-api-1.7.30.jar telegrambots-5.0.1.jar j
ackson-annotations-2.11.3.jar jackson-jaxrs-json-provider-2.11.3.jar ja
ckson-jaxrs-base-2.11.3.jar jackson-module-jaxb-annotations-2.11.3.jar
jackson-core-2.11.3.jar jakarta.xml.bind-api-2.3.2.jar jakarta.activati
on-api-1.2.1.jar jackson-databind-2.11.3.jar jersey-hk2-2.32.jar jersey
-common-2.32.jar osgi-resource-locator-1.0.3.jar jakarta.activation-1.2
.2.jar hk2-locator-2.6.1.jar aopalliance-repackaged-2.6.1.jar hk2-api-2
.6.1.jar hk2-utils-2.6.1.jar javassist-3.25.0-GA.jar jersey-media-json-
jackson-2.32.jar jersey-entity-filtering-2.32.jar jersey-container-griz
zly2-http-2.32.jar jakarta.inject-2.6.1.jar grizzly-http-server-2.4.4.j
ar grizzly-http-2.4.4.jar grizzly-framework-2.4.4.jar jakarta.ws.rs-api
-2.1.6.jar jersey-server-2.32.jar jersey-client-2.32.jar jersey-media-j
axb-2.32.jar jakarta.annotation-api-1.3.5.jar jakarta.validation-api-2.
0.2.jar json-20180813.jar httpclient-4.5.13.jar httpcore-4.4.13.jar com
mons-logging-1.2.jar commons-codec-1.11.jar httpmime-4.5.13.jar commons
-io-2.8.0.jar telegrambots-meta-5.0.1.1.jar guava-30.0-jre.jar failurea
ccess-1.0.1.jar listenablefuture-9999.0-empty-to-avoid-conflict-with-gu
ava.jar jsr305-3.0.2.jar checker-qual-3.5.0.jar error_prone_annotations
-2.3.4.jar j2objc-annotations-1.3.jar
Main-Class: domain.Main
As you can see, telegrambots-meta-5.0.1.1.jar is part of the classpath attribute.
How can I solve this?
By the way, I'm using Heroku Cloud to deploy this
Sounds like you want and need to create a runnable/ executable JAR file (with external dependencies).
This requires your build process to be enhanced by this step, regardless of where it is executed Heroku, Jenkins, Bamboo or on your local - this is a maven setting and will affect each of them.
Also on your local you can run the build of your project by executing mvn clean package in your IDE and afterwards to run the created JAR from the target folder with: java -jar ${yourJarName}. It'll likely fail for the same reason.
This is, because Maven dependencies are added with a so called scope. These are for example:
compile
provided
runtime
test
Whereby compile is the default one and being implicitly applied in case you don't specify it - like in your case. (You can read more about the scopes here)
This means Maven will add your dependency to your IDE at compile time, but it will be missing at the runtime, when your trying to execute it.
The solution is to create a runnable/ executable JAR file (also called *fat JAR *) containing all the needed dependencies.
You can do it directly within Maven with the help of the maven-assembly-plugin like so:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Then you need to build your JAR like:
mvn clean compile assembly:single
Note: The compile goal must be added before assembly:single or otherwise the code on your own project is not included.
To ease the handling of the process this goal commonly is tied to a Maven build phase to execute automatically. This ensures the JAR is built when executing mvn clean package, mvn clean install or performing a deployment/ release:
<build>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>fully.qualified.MainClass</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- bind to the packaging phase -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
Like this you can simply build your project with the mvn clean package command (probably the most common one) and it'll include the creation of the runnable/ executable JAR file. This will include all your needed dependencies and should resolve your java.lang.NoClassDefFoundError issue.
Just a short additional note
Creating runnable/ executable JAR file respectively fat JAR is not the only solution and maybe in some contexts unwanted. Since fat JAR files include all their needed dependencies, they are fairly big with all the related drawbacks (requires more bandwith to transmit, download size increases, same dependencies might be carried in multiple different JARs, ...).
For this reasons the fat JAR creation is avoided in Web Application Development with Java EE. Dependencies are only added at compile time, since it is known that a Servlet Container or Application Container like Tomcat or Wildfly will provide these at runtime to avoid the java.lang.NoClassDefFoundError. Therefore the different applications (JARs or in this context called WARs) don't need to provide the dependencies themself.
In your case it might also be the solution that you'll still build the thin JAR, but will provide the needed dependencies at runtime (e.g. separately downloading it and then specifying in the classpath before the execution).

Resolve classes of maven dependencies in eclipse

Consider the following dependency hierarchy:
Now I have maven-jibx-plugin in project D which generates the compiled classes in target/classes folder. But when I run my spring-boot project A the generated classes from project D could not be resolved.
Resolve dependencies from workspace is also checked from maven preferences of project A
To me it looks like Eclipse and Maven do not recognize project D as a related project.
There are two possible solutions:
In Eclipse you can add project D as a dependent project to project A's build path. Go to the project's Properties dialog. Select Java Build Path and then switch to the Projects tab. There you should add project D.
Or alternatively you rely on Maven's dependency management. Therefore you have to add a dependency to project A's POM file. First add a <dependency> ( if not already done ) to the <dependencies> section. Now comes the important part! Maven can only resolve this dependency if you have installed the compiled maven artifact ( the jar file ) in your local maven repository. On the shell switch into project D's directory and then run mvn install
I hope that did the trick.
Have you tried adding generated sources folder as a source folder in eclipse? You can do this from eclipse (right click the generated sources or any folder in it > Build Path > Use as Source Folder) or you can use maven build helper plugin by adding something like below to your pom.xml
<!-- MAVEN ADD GENERATED-SOURCES TO CLASSPATH -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>${maven.plugin.build-helper.version}</version>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>add-source</goal>
</goals>
<configuration>
<sources>
<source>target/generated-sources/annotations</source>
</sources>
</configuration>
</execution>
</executions>
</plugin>

maven - referenced libraries not found in packaged jar [duplicate]

This question already has answers here:
Add external library .jar to Spring boot .jar internal /lib
(8 answers)
Closed 4 years ago.
When I mvn clean package the project and run it with java -jar target/project.jar it throws an error that it cannot find some class from an external jar.
Steps taken
All external jars are in a folder of my project: /jars or /jars/morejars/
Adding the jars to the build path: In Eclipse I right click on project, go to build path and select add external archives. I can see that eclipse creates a library folder "referenced libraries" below the "Maven dependencies" folder. When I check project -> Properties -> Java Build Path -> Libraries I can see all the imported jars.
Some of those external jars are described as <dependency> (with <systemPath) in pom.xml, so they will not be seen in Referenced Libraries but in Maven dependencies (interestingly, the class that cannot be found when running my packaged project is in an external jar that doesn't reside in Referenced Libraries but Maven dependencies)
e.g.
<dependency>
<groupId>external.jar.groupid</groupId>
<artifactId>external.jar.artifactid</artifactId>
<version>1.0</version>
<scope>system</scope>
<systemPath>${basedir}/jars/external-jar.jar</systemPath>
</dependency>
Use the maven assembly plugin (as described here), this is my full <build> config:
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
</build>
Run mvn clean package -> BUILD SUCCESS
java -jar target/project.jar -> Cannot find some.class from external.jar
Thanks for the help
The problem is clearly with dependencies. While your application starts loading it looking for required classes to run properly. While it loading a class from a jar library and that library class also refer to some other class from a another jar library file but that library (jar file) is not available in your class path, it will give you the above error. So check for maven dependency and get all the jars required by your application. Also based on error message also you can add the jar reference in your pom.xml file. For example if you get an error like Failed to load com.apache.some.Example.class just google the Example.class jar file and get it from maven repository.
Hope this will help you.

Maven project packaging is war but also building/installing jar

I have a Maven 3.3 project, and the main output is a war file:
<artifactId>pro</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
I am using the maven plugin to also build a jar file, which goes into target/pro-1.0-SNAPSHOT.jar and this works.
I would like to install this jar to the local maven repo, so I'm using the maven install plugin to do this:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
<configuration>
<classifier>jar</classifier>
<packaging>jar</packaging>
<file>target/pro-1.0-SNAPSHOT.jar</file>
</configuration>
<executions>
<execution>
<id>do-jar-install</id>
<phase>install</phase>
<goals>
<goal>install</goal>
</goals>
</execution>
</executions>
</plugin>
The whole build works fine except the jar install step is installing the war, and not the jar. How do I override this?
I'm looking here to see what I can use: http://maven.apache.org/plugins/maven-install-plugin/examples/installing-secondary-artifacts.html
Here is the log from my build:
**
Building jar: /Users/mike/code/workspace/pro/target/pro-1.0-SNAPSHOT.jar
[INFO] --- maven-install-plugin:2.5.2:install (do-jar-install) # pro
[INFO] Installing /Users/mike/code/workspace/pro/target/pro-1.0-SNAPSHOT.jar to /Users/mike/.m2/repository/net/mikeski/pro/1.0-SNAPSHOT/pro-1.0-SNAPSHOT.war
Note the last line - it's picking up the jar but installing a war
How can I fix that?
As requested, here is the jar plugin config:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.4</version>
<executions>
<execution>
<id>create-jar</id>
<phase>install</phase>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
</plugin>
It creates a jar of the files in the project, and it's the correct jar. If I copy the war that is installed in my repo to a jar (so I just change the extension) my other project can pick it up just fine.
Your current output is the result of multiple considerations, one of them being maven-jar-plugin too permissive in version 2.4.
First of all, you need to remember that inside a Maven repository, all the artifacts share a single naming convention, which is artifactId-version(-classifier).packaging. This means that whatever the local name of the file your build is producing (let it be foo.jar), it will be installed and it will be deployed with this conventional name. All that matters when artifacts are installed are the Maven coordinates, i.e. the groupId, the artifactId, the version, the classifier and the packaging.
What is happening here is:
Your project has a packaging war. Running Maven with mvn install, the default-install phase will be invoked first and the maven-install-plugin:install goal will be run a first time, installing your WAR project. On your logs, you will find:
[INFO] --- maven-install-plugin:2.5.2:install (default-install) # test-war ---
[INFO] Installing ...\test-war\target\test-war-0.0.1-SNAPSHOT.war to ...\test-war\0.0.1-SNAPSHOT\test-war-0.0.1-SNAPSHOT.war
[INFO] Installing ...\test-war\pom.xml to ...\test-war\0.0.1-SNAPSHOT\test-war-0.0.1-SNAPSHOT.pom
Then, you are using the maven-jar-plugin:jar goal to create a JAR. This plugin creates a JAR for the current Maven project - so it will create it alright, but the Maven coordinates of this new artifact will be exactly the same as those of your WAR project (you didn't specify a classifier). Therefore, you effectively replace the file of the main artifact (which is a WAR), by a JAR file: you end up with a local file having an extension of jar (because the maven-jar-plugin created it this way) that is the file of the main artifact of a Maven project of packaging war. Quite confusing.
Remember that I said that the maven-jar-plugin was too permissive? If you update to version 3.0.2 of the plugin, you will get an error right here (MJAR-198):
You have to use a classifier to attach supplemental artifacts to the project instead of replacing them. -> [Help 1]
which summarizes what is said above.
Finally, you declared another execution of the maven-install-plugin called do-jar-install, that is supposed to install this local JAR file. And this is what it does: it installs the local JAR file inside your target folder to your local Maven repository using the coordinates of the artifact. The confusion comes from the fact that the type (packaging) of the artifact is in fact WAR, so what gets installed is a WAR file (being effectively a JAR)...
Now that we've explained the issue, the question is: what do you want to do? It looks like you want to attach to your WAR project an additional artifact composed of the classes of it. There is no need for all this configuration, you can just use the attachClasses parameter of the maven-war-plugin.
<plugin>
<artifactId>maven-war-plugin</artifactId>
<version>2.6</version>
<configuration>
<attachClasses>true</attachClasses>
</configuration>
</plugin>
You received this because double invocation of plugin :
execution phase
package - default phase
If there is no strong reason and all you care is to have just one JAR as artifact from the project, use one among them.
something like :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
<version>2.6</version>
<configuration>
<outputDirectory>${outputDirectory}</outputDirectory>
</configuration>
</plugin>

maven create jar with pom

I'm working on an Android project and have some core code(that has some dependencies) that i'd like to version and make into a library/artifact(?) that I can pull into other projects. I'm using Maven to build and my editor is IntelliJ. I've never created a .jar but I think that's what I want to do.
In IntelliJ, i've gone to File->Project Structure->Artifacts->+ but I'm lost. I don't know how to define which source directories and files to include in the jar and I'm unsure if I need to include the actual jars of its dependencies in the jar? or define those dependencies in a pom.xml file and include the pom.xml file in the jar?
Any clarification would be much appreciated.
Maven project's dependencies are defined in its pom.xml file (basically, changing the dependencies from here is what are you visually doing using Intellij, by File->Project Structure->Artifacts). The source files that Maven takes are the ones which are into the src/main directory of your project. The build is done by default excluding the dependencies, so if you want to include them in your final jar, you have to specify this in your pom file (plugins):
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>attached</goal>
</goals>
</execution>
</executions>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
After that you have to execute mvn install command to have your jar created. You can do it running maven externally or from your IDE (if you have some Maven plugin installed) and the jar will be created in project's target folder.
The settings in IntelliJ IDEA will only create the JAR when you build using the IntelliJ IDEA compiler. To have maven create the JAR and deploy it to the maven repository, you need to use the maven assembly plugin. There is a predefined configuration for creating a jar with dependencies.
The Maven Shade Plugin can also create JARs and handle more advanced use cases. But you'll probably want to start with the assembly plugin/

Categories

Resources