Adding external library to artifact jar in IntelliJ IDEA - java

How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?

You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:

If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.

Related

How do you add and use jar files for testing purposes in IntelliJ 2020.2.1? [duplicate]

How can I add an external library to a project in IntelliJ IDEA so that when I build an artifact it still has access to the classes in the library?
I have created a new Jar artifact from Project Structure, then added the external JAR to the Libraries, then checked it in the Modules List, and finally added it to the Output for the Artifact. None of these work. When I build and try running my application, it throws an error:
Exception in thread "main" java.lang.NoClassDefFoundError: <path of the class trying to use>
What am I missing, or am I doing this completely wrong?
You have 2 options here:
extract the dependency into the artifact jar so that the app is the single executable jar with all the dependencies
link the dependent jars via the Manifest.MF and copy them near the application main jar
I've prepared a sample project that demonstrates both approaches: HelloWithDependencies.zip.
The artifacts are produced into out\single and out\linked directories.
Relevant configurations:
If you are using maven to build your application then this is not the correct way to add external library. You should either
Do an install of your library like below mvn install:install-file -Dfile=myJar.jar -DgroupId=com.yourproject -DartifactId=yourproject -Dversion={version} -Dpackaging=jar.
Use system path like explained here.
Option 1 is prefered since you don't have to keep jar in your project.

How to include a jar file that has dependent jars with it as a dependency itself in an another project?

I just want to know how to include a jar file that has all dependent jars with it, as a dependency itself of an another project. I have tried export as runnable jar option and though it does work when I run the project as standalone, however I get noclassdeffound errors when I include the jar itself as a dependency for another project. To summarize suppose I have project A which depends upon some external jars a.dep1 and a.dep2 I include them in the jar by exporting the project A as a runnable jar file. Now I wish to use project A itself as a dependency in project B and for that purpose I include the jar of project A in my project B. But when trying to run I get the noclassdeffound errors. I don't want to use maven plugins. Is this possible?
for such cases you should be using maven
then you need to create a fat jar.
a fat jar will include all the dependencies it needs inside .
such a jar can be created using the assembly plugin you can see an example here:
assembly plugin
in general if you are using maven you do not have to do this as maven will bring all the dependencies your jar needs based on the pom file of your first jar.
A jar is just a collection of files; you have free rein on what you want to include in it via the command line. https://docs.oracle.com/javase/tutorial/deployment/jar/basicsindex.html should teach you what you need to know

How do I export Maven Libraries together with my program?

I've created a Discord bot in Eclipse and want to export it (Jar file). The problem is: I can't run it. Each time I try to start it it gives me this error: java.lang.NoClassDefFoundError: net/dv8tion/jda/core/entities/Game
I'm using "JRE System Library [JavaSE-1.8]" for my program.
This here is my pom.xml file
https://pastebin.com/bcs51jm9
After some research it seems like Eclipse doesn't implement the Maven Librarys into the Jar-file.
How can I fix this?
Exporting a jar with Eclipse from the wizard is generally the way when you don't have a build tool that allows to do it in a straight way.
You use Maven. So you have this tool.
What you need is configuring your pom.xml to enable the assembly plugin execution with the jar-with-dependencies descriptorRef.
It allows to specify the main class and to create a fat jar that contains all required dependencies at runtime according to the Maven dependencies declaration defined in your pom.
By executing mvn assembly:single you could so generate an additional jar : a jar-with-dependencies.

Maven dependency for self placed jars in project level

I created the java class and converted into jar files. So, I want to use those jar files which I have placed in project level in some folder like "External Jar".
So I need to write a dependency in maven that when someone imports my project they should be able to run the program.
Basically you created your own jar and you want to publish this jar, so that when somebody else clone/use your project, this jar comes with (assuming that you have a maven project and dependency of your jar is included in pom.xml).
To achieve this, you need to publish your jar to maven , you can follow many of the online docs like http://kirang89.github.io/blog/2013/01/20/uploading-your-jar-to-maven-central/ on how to publish jar to maven central.
Edit:- As suggested by khmarbaise, please use official reference http://central.sonatype.org/ for central repository.

How to get my external jar in executable jar's classpath in eclipse?

I have added to my pom.xml a section that specifies the mainClass and allows it to essentially create an executable jar. I have included a bunch of dependencies that maven manages as well. It compiles, and if I run the program with no options, it executes fine, displaying usage information. However, if I actually pass in the parameters, it fails and says NoClassDefFoundError: com/sas/isd/midasapi/ParticipantDetailExt, which is in a jar I included as an external jar. I am confused that it compiled and runs to show usage information, but it fails to find the class after as the ParticipantDetailExt is a class that is imported. Wouldn't it identify that it was not found during compiling? How do I get it so that my on jar with ParticipantDetailExt is seen when I run my exectutable jar? Is there a classpath thing or pom thing I need to do in addition to the adding jar as external jar?
I assume that you are running mvn clean package or mvn clean install to create your jar.By default the jar created by a maven project does not include dependencies in that jar.
Option 1# create a jar-with-dependencies, see: How can I create an executable JAR with dependencies using Maven?.
Option 2# If you are just looking to copy dependencies to a lib folder see: http://maven.apache.org/plugins/maven-dependency-plugin/copy-dependencies-mojo.html

Categories

Resources