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
my Java Project uses a "/libs" folder containing ~100 .jar files. Almost all of them are not in an official maven repository.
1.) In the moment I manually added to whole folder to the classpath with my Eclipse IDE. That enables to compile and run the App using the Eclipse IDE. But if I want to maven to compile and create jar-with-dependencies, maven of course does not know about the "/libs" folder.
2.) I know that I can add a jar file to my local maven repo with mvn install:install-file but this would take a very long time because I would also have to open every jar and find the whole package name to insert as '-DgroupId' and the Name of the Main Class to add as '-DartifactId'
3.) My Questions:
3.1) Is there an easy way to let maven just include all jars in a folder like I did with my Eclipse IDE? I know that would break the principle of maven that every jar is identified with group and artifact id, but it would be a quick solution.
3.2) If it is not possible to add a folder with jars as a dependency in maven, is there a faster way to add a jar file into a local repo. It would be easier if there is a maven command where groupId and artifactId are automatically discovered by the jar that I do not have to open every jar file and find the Main Class and its classpath
Quick answer: No.
In the past, I have written a script for that because there is not support in Maven for this.
I created a new maven project in IntelliJ and set packaging to jar and when I build it, there is a jar file target folder. But when I run the file I get the error as
no main manifest attribute, in QeToolHelper-0.1-SNAPSHOT.jar
I have created jar file using maven-assembly-plugin. I have added these lines in pom.xml for that.
Tried few methods described in other similar questions but to no avail.
Have you looked at SpringBoot and associated plugins. It can be used to build a standalone java applications.
With regards to your specific issue, that error is symtomatic of a failure to declare main class in your manifest. The main-class attribute in the manifest tells the jvm what class to load from the built jar. Looks like the settings in your maven config are failing to generate the resources correctly.
I was able to solve this issue by creating jar package from maven.
Running the build package created the jar in target directory.
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 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.
I have a huge project (Application) with another project inside it (Core). Application has a big set of libraries inside as does Core. I'm using Eclipse and so I'm using the Export Runnable Jar option to create Application.jar but when I run it part of the code uses the Core which has a dependency on an image library within that. When I look inside the runnable jar file all the libraries for the Application project are there but when I look inside the Core project jar file the libraries aren't there. To give you an idea of what I mean the structure I'm expecting looks like this:
Application
- Core.jar
+ ImageLibrary.jar
+ OtherLibraries.jar
:
+ OtherLibraries.jar
:
I'm using Maven to build the projects individually. But I'm not really an expert with Maven as I've only being using it for a short time.
Is it possible to build Application so that Core also has its libraries with it?
Thanks in advance,
Alexei Blue.
In your Maven build for Application, is Core not specified as a dependency? If so, it should be included with all its dependencies. You should probably check whether the dependencies themselves have the correct scope in Maven to be included.
Okay so I figured this one out in the end. When I was using Maven to build the project it was doing so and then not updating the build path in eclipse. So I in the core directory I ran:
# mvn clean install
# mvn eclipse:eclipse
As the image library was an addition to the Core dependencies I had to ensure that the build path for eclipse was set otherwise eclipse won't pick up the changes. The eclipse:eclipse command did this for me but I think it only works in projects that are one module, however it reads any dependencies from your pom file and ensures your project can see and access them.
And then in the App directory I re built the project:
# mvn clean install
Then from within eclipse I exported the App project as a Runnable Jar which worked fine.
Hope this helps anyone having the same problem.
Cheers,
Alexei Blue.