Class not found after jar export - java

It should be simple but I can't run my JAR file.
My error:
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/pdfbox/pdmodel/font/PDFont
I'm trying to follow some post like this: Stackoverflow
But nothing.
I think this error is because I haven't take external libraries on my project.
How to create a JAR file with libraries in Intellij ?
Anyone have a solution ?

Check if your classpath includes the jar

go to File from File menu
click on Project Structure
choose Modules at the left panel
select Dependencies tab
and start adding external jars.
After adding the dependencies, you can export your project as jar if you wish.
Again go to File
Project Structure
Project Settings
Artifacts
extract Jar
If you still get the error, please cross check your classpath for the missing class, you might have got classnotfound exception or ExceptionInInitializerError before.

Click on
File -> Project Structure->Artifacts->'+'->Jar->From modules with dependencies-> Select the module folder and Main Class.Click on Apply and then you export the artifact
Now with all the required libraries in the class path your program should work as expected

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.

Adding external library to artifact jar in IntelliJ IDEA

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 do I create a runnable JAR in IntelliJ as I would in Eclipse

My process for creating a runnable JAR from a project with many libraries with Eclipse has been.
Export > Runnable JAR > Select launch configuration > Package required libraries into generated JAR > Finish
This creates a single JAR in my export destination which I FTP to my server and run fine.
I recently switched to IntelliJ for various reasons and it has been an improvement in all cases except for building my JARs which forces me to go back to eclipse temporarily.
With IntelliJ I:
Open Project Structure > Artifacts and create a new JAR from modules with dependencies. I then have my output layout with the name of the JAR, a META-INF inside it and all my libaries with the format Extracted <lib.jar/> (sorry I can't upload screenshots on this VPN.)
I build my artifact (which is about 15MB bigger) and FTP it to my server, try to run it and I get the error:
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
How do I mimic how I do it in Eclipse?
EDIT
The two JARs that are created differ considerably:
The valid jar that Eclipse creates looks like this:
Whereas the invalid .JAR looks like this:
You're facing two issues one seems major and one is minor:
Major: signature exception
Minor: size of the jar is 15 MB greater than the jar produced by eclipse.
The solution of both of the issues lies in the way you are building the artifact. First of all, remove all extracted *.jar then add all the jar's from the available elements pan as shown in the figure.
It is obvious that adding packaged(compressed) jars only, will decrease the size. But it also solves the signing issue. For more explanation, please have a look at this article. I'll only quote one line.
It's probably best to keep the official jar as is and just add it as a
dependency in the manifest file......
It seems that some of the dependencies are signed and repackaging messes up the META-INF of your project.
If you are using Maven you need to put META-INF directory under /main/resources folder instead of main/java.
Reference: Intellij - Nikolay Chashnikov
Then you can execute the runnable jar normally with java -jar filename.jar
You can these steps:
1) File -> Project Structure -> Project Settings -> Artifacts -> Jar -> From modules with dependencies
2) Check the Include in project build checkbox.
3) Right mouse click on module -> build module 'ModuleName'
you could try using a gradle script. eclipse will make you a gradle project. you can use that build file with any ide. it has a built in jar task.

how to pack external jar into output jar file in eclipse

I'm currently working with J2me by Mobile Tools for Java (MTJ) on Eclipse. My program need to encrypt SMS messages by RSA so i try to use bouncycastle library. I download bouncycatle version 1.47 and add to to my buildpath. No problem occurs when building project. But when i try to run on emulator, i get this error:
TRACE: <at java.lang.NoClassDefFoundError: org/bouncycastle/crypto/engines/RSAEngine>, startApp threw an Exception
It's a common problem, NoClassDefFound. To solve this, i know i must include the class file of bouncycastle in classpath. But here come the problem, i cant pack bouncycastle class file into the jar which MTJ created. For regular project, all i need to do is tick on the checkbox in "Order and Export" tab to have all class files in the "ticked" referenced library packed in to output jar. I do the same here but after i create package, i check the new jar file and no class of bouncycastle is inside (check using WinRar).I did some
I'm using Eclipse Indigo Release 2, build-id: 20120216-1857, MTJ version 1.1.2.
I also tried to create package by eclipse Pulsar 1.3.1 but the problem still persists.
Is there any suggestion? is this a bug?
Regards.
"Order and Export" is not working as you think it is. If you have two projects - ProjectA and ProjectB and ProjectA dependends on ProjectA, then if ProjectA needs some.jar and you find out that ProjectB needs some.jar too, then "Order and Export" for that jar in ProjectA makes it "visible" also for ProjectB.
For JAR files you cannot add another jar into the JAR files (it works for WARs and EARs). There are two possibilities how to solve this
you can unzip required JAR and add class files to your jar, jar -u can help you (this seems difficult, but if you are using ant or maven you will configure that once)
you can try to use MANIFEST headers

Add python module to JAR classpath in Eclipse

I have a Java project that utilizes Jython to interface with a Python module. With my configuration, the program runs fine, however, when I export the project to a JAR file, I get the following error:
Jar export finished with problems. See details for additional information.
Fat Jar Export: Could not find class-path entry for 'C:Projects/this_project/src/com/company/python/'
When browsing through the generated JAR file with an archive manager, the python module is in fact inside of the JAR, but when I check the manifest, only "." is in the classpath. I can overlook this issue by manually dropping the module into the JAR file after creation, but since the main point of this project is automation, I'd rather be able to configure Eclipse to generate properly configured JAR automatically. Any ideas?
*NOTE*I obviously cannot run the program successfully when I do this, but removing the Python source folder from the classpath in "Run Configurations..." makes the error go away.
Figured it out, had to add the source folder with the Python module in it as a class folder in the Build Path project properties. Not sure if this next part is necessary or not, but since the module is not compiled, I added the folder again as "Attached Source" after adding the class folder.
Have a look at the maven-jython-compile-plugin and its demo project at http://mavenjython.sourceforge.net/ . It allows bundling jython with dependencies into a standalone jar.

Categories

Resources