Java: java.lang.ClassNotFoundException: org.jdom2.JDOMException [duplicate] - 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

Creating a jar from maven project Intellij

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.

Simple Json ClassNotFoundException when executing JAR built by gradle

I recently developed a simple JavaFX Game which used Simple Json. The dependency has been defined in Gradle dependencies and the game runs fine with Gradle run command. But when I create a jar file using - Gradle Jar and run it the following error shows up:
Caused by: java.lang.ClassNotFoundException: org.json.simple.parser.ParseException
Here is the picture of my build.gradle file:
How do i Fix this?
This is the default package behavior. The package phase of tools such as Gradle and Maven only copy the application sources (Java classes and resources from src directory), and NOT include files from dependencies.
Therefore the jar file produced by default does not have dependency classes or resources.
There are many solutions to run a java program from the jar file with dependencies:
Place the app jar as well as dependency jars in a directory (lib) and add it to the classpath when running the main class.
java -cp lib/*.jar com.game.MainClass
Create a fat (uber) jar. Gradle plugins like Shadow can generate a single jar with all dependencies packed in it. Just specify the main class in Manifest.
java -jar myAppFat.jar
Use plugins like Launch4J to generate Windows executables. This is great if you want to ship simple "click to run" apps to users.

How to export external jars along with the project while creating jar of a project [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.

Create Jar With External Jar Libraries (Intellij 2017) [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 build a runnable jar with internal jar files that have internal jar files?

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.

Categories