IntelliJ adding jar dependencies - java

I have a Maven project and I want to add a JAR file as an external dependency to the project.
After clicking 'New' in Project Structure I expect to see an option for "JAR file". Unfortunately, I don't see it. Do you have any idea why or what I can do to add external JAR file as a dependency?

It seems like you're in the wrong tab of project structure settings.
Follow those instructions: Correct way to add external jars (lib/*.jar) to an IntelliJ IDEA project

Related

Generating JAR on IntelliJ

I have some problems while creating a jar of my intelliJ project. First of all I just do the usual procedure:
Go to project structure
Select artifacts
Create a new one with modules and dependecies
Go to build artifact, build it
Execute it at console with java -jar "ddddd.jar"
So when I execute the project (not the jar) all runs ok, libreries and all works, but not in the JAR.
Terminal of IntelliJ:
My libraries are located in the project like: project>lib>"some jars here">otherjars>"here some jars and respective dll
This is because the "other jars" need the dll to work. I already tried to run them without the .dll but doesn't work without it.
Also I have them in the project on Modules>Dependencies I have the folder selected as a library.
Any help would be appreciated.
Edit 1:
Here I add some pictures about how I have my libraries structured.

Is there any alternate to acquire dependencies without using POM.xml?

My question is, I don't want to write XML code for dependencies in the POM.xml again and again.
For example, I want to use the log4j JAR in my project. instead of writing XML code is there any option to download the related JAR files?
Alternate method is downloading the jars from sites like maven or java2s.com and add them to your build path.
You can add a jar in Eclipse by right-clicking on the Project → Build Path → Configure Build Path. Under Libraries tab, click Add Jars or Add External JARs and give the Jar.
Hope that helps
Of course you can download (or copy) these jars, and then manually add them to your build path.
For instance in eclipse, you can select all the libraries under the "Maven Dependiences" and copy them to other folder eg. in other project. Then you need to select these libraries and using the right mouse button add them to build path, done :)
You do not need to write them again and again. You can define and install a parent pom with the common dependencies you use and refer to it in each project pom.
First you could download jars you need in your project from corresponding websites.
Next,follow the steps below:
Step 1: Create a folder under the project to save the JAR package.We usually store jar packages from the outside in the [lib] folder.
Step 2: Copy the jar to the lib folder.
Step 3: Right-click on the project name, and select it in turn [Build Path] - > [Configure Build Path...]
Step 4: In the open window, select the Libraries page and click on the right button the [add JARs...] then select the jar package we just copied into the project, and then click [OK] to close the window.
Finally, we can use this jar package in Eclipse
Yes you can add them through the java build path from your IDE. However, I would recommend that you add these jars as dependencies in the pom.xml and build your project through maven. This will make sure that you will not run into any error regarding the missing dependencies in case any jar gets deleted accidentally or if you want the same set of jars for a new project. And understanding a pom.xml is not a difficult task

Eclipse Java Build Path Jar Library classes not found even if available in jar

Recently I wanted to add the nebula shelf to my eclipse plugin. Therefore I added the org.eclipse.nebula.widgets.pshelf.source_1.1.0.201701302244.jar the build path of my eclipse project.
Sadly eclipse can't find the class files inside of the jar, even after cleaning and rebuilding the whole project.
Picture:
Checking the same jar file with jd-gui results in:
Picture:
I exported the *.java files with jd-gui and copied them to my project. Using this method my project built successfully and worked as expected.
Can someone tell me how I can use the jar file without exporting all of the java files manually? Thanks for your help! :-)
Do not use source jars. You can tell eclipse while debugging to use the source jar.
Add the jar to build path: right click on jar -> build path -> add to build path
Don't try to add Eclipse plugins directly to the build path of another plugin as it won't work.
Instead add the plugin to your target platform or import it in to your workspace.
Then add the plugin to your plugin's Dependencies. In the MANIFEST.MF editor you do this on the 'Dependencies' tab in the 'Required Plug-ins' list.

Use Maven Library without Drinking Maven Koolaid

I want to use the diffplug/Durian library, but do not want to use Maven. Is there a way to use a Maven library without using Maven itself in a project?
This has a link to the .jar and lists it's dependencies.
http://mvnrepository.com/artifact/com.diffplug.durian/durian/3.4.0
Download the jar - then all the jar's dependencies and their dependencies and add them to the class path.
I found that IntelliJ IDEA allows for exporting of a Maven module to a JAR, that can be used in non-Maven projects.
https://www.jetbrains.com/help/idea/2016.1/downloading-libraries-from-maven-repositories.html
And needed to know what a "Maven Coordinate" was. I found it for the library in question at https://mvnrepository.com/artifact/com.diffplug.durian/durian/3.4.0, and it is the string "com.diffplug.durian:durian:3.4.0".
Basically, follow Project Structure->Project Settings->Libraries->"+"->New Project Library->From Maven. The resultant dialog takes a Maven coordinate, and has a "Download To" option, that will make a nice JAR at the specified location, from the Maven library you import. Can add source and javadocs as well. After doing the download, you navigate to the system folder containing the new JAR, and stick it in your real non-Maven project (an Eclipse project, in my case).

How to package dependencies into java project with eclipse

I get this error when I try to run my project.
java.lang.NoClassDefFoundError: org/yaml/snakeyaml/Yaml
I am trying to include the dependency SnakeYaml.jar in my project so my project is only one jar instead of requiring an external dependency.
in my project SnakeYaml.jar is located at /Libs/SnakeYaml.jar
but it is not being loaded at runtime, what can I do to get it to load the resource at runtime?
The best solution to avoid such problems is creating maven based project by eclipse. when you export your project as jar or war files it puts all dependencies in the jar or war files
In Eclipse, to put the dependency packaged inside your project executable jar you can do this:
I cannot post images due to my reputation so will try to explaing it writting.
Make sure the dependency is in your class path: right click the project -> Build Path
Right click your project again and select "Export"
Now choose "Runnable JAR file" (you can filter the list by typing)
In the next dialog choose the right option for you, in your case it could be "Package required libraries into generated JAR"
Hope it helps

Categories