IntelliJ IDEA: Include external library in production directory - java

I am using IntelliJ IDEA to develop a Java project. To include the mysql-connector-java library I use, I created a folder named lib in the root of the project and added the JAR to it. After that I added it as a global library using the context menu.
When debugging it, everything is working fine. But when I navigate to the production directory and try to run it from the command line it cannot find the library. How can I include it in the production directory to allow accessing it when running my main class? Or is there another way to get this done? I just want to make sure, that all external libraries my project is depending on are properly included.

in the command line you should use -cp to add jars or directories to classpath (as explaind here ). if you want to run with intellij Idea then you create a run configuration (which I thing you've already done) and then in the configuration select you module in option use class path of module part of the configuration.

You have to add the library/jar to your BuildPath.
Just take a look at the "Correct way to add a library to an IntelliJ project"

Related

How to use a library's code in IntelliJ instead of the jar file

I'm using gradle for my build tool. I have a project that uses a jar file and I have the jar file imported into my gradle for my project which works great when doing build and deploy.
Problem is that I'm developing the jar file along with the project. When I change the library code, I have to build and move the jar file into my project. PITA.
Instead, I'd like to set up my IntelliJ environment so it uses the module's code first and then looks at the jar file later/never.
It seems like something that IntelliJ should be set up to do, but searching turned up nothing and I can't seem to find anything banging around in the IDE.
Anyone tackle this?
Currently such setup is not supported for Gradle project. There is however a hidden option that you may try to set:
external.system.substitute.library.dependencies = true
via "Help | Edit Custom Properties" and restart.
The corresponding request for this is https://youtrack.jetbrains.com/issue/IDEA-134885

How Can i add Library Jar files into the modules

I am trying to use loaded jar files of my class path into my module. I
know requires will accept only modules names.
my jar files are present inside the Spring Jar Files folder.
I tried to use #Configuration annotation inside my appConfig.java file and IDE could not recognize it.
Your help is much appreciated as I am new to the programming world.
Normally, I use Maven or Gradle to handle all the jars.
It seems you have just started with Spring.
I suggest you try Spring Boot with Maven or Gradle to manage the library and the code lifecycle (compile, build, test).
This is one tutorial for example: https://spring.io/guides/gs/spring-boot/
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.
Example link
The above solution is obviously a "Quick" one. However, if you are working on a project where you need to commit files to the source control repository, I would recommend adding Jar files to a dedicated library folder within your source control repository and referencing few or all of them as mentioned above.
I'd suggest go for the second one if you are planning to build this as a proper project and put it in a source control repo.

Where to put Java libraries?

I'd like to know how I can add Java libraries to an Eclipse project on a development machine so that they can be added to an Eclipse project without causing errors when someone who has the library in a different location. For example, one developer might add an external JAR in C:\Java, but another might have the same JAR somewhere else. (Where's C:\Java on Mac OS?) I thought I might set the CLASSPATH environment variable, but I can't figure how to add an external JAR from the CLASSPATH environment variable. I'd like to do this so that it works with any workspace. Is this possible?
This is specifically for use with Anypoint Studio, but I think the same problem would exist with any Eclipse-based IDE.
In general, it's recommended to either embed JARs directly into the project, usually in a /lib folder of the project, as described here; or to use a tool like Gradle or Maven to manage dependencies, both of which have nice plugins to support their use in Eclipse.
Another alternative would be to use a Classpath Variable to refer to the JAR(s), which abstracts the physical location so that it can be set on a per-workspace basis.
I think the best way to add library to eclipse project is creating a directory - lib in your project directory. Then add the whole lib to you eclipse class path. You can follow these step to add a lib to class path -
Right click on project and select properties
Select Java Build Path
click Add Library and create User Library
Now add External Jars to this library create at step 3.
By this a .classpath file is crated in you project directory and the CLASSPATH problem will be resolved
I guess the best way to do that would be using Maven, or a similar build system that can construct your Classpath base on dependencies.
You can add the dependencies to your pom and having the jars in your local maven repository in the machine.
http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html
Because in any other approach you will need to maintain everything manually, and when having different OS the path will change.
Eclipse is just the IDE that will help to write code and assemble the project (JAR, WAR, ... ). You can add your external jars from wherever you want, and when you want to export your project (with eclipse) you may choose to package the required libraries into the jar.
However, I recommend always to use maven (or something like ) to avoid this kind of problems.
Part1:(import .jar file as library in Eclipse)
You make a new project to Eclipse(name:Project1)
When you open it you see JRE System Library[java version something]
1.right click on JRE System Library
2.Go->Build Path->Configure Build Path
3.You can see (Up right Corner the button[add jars or add external jars]
*Here i advise you to choose the first(add jars) but..
*First copy(or move) the (name).jar inside the project((example):Project 1)
*Now you can add it with the button(add jars).
*In this way when you finish your project the (name).jar will be
imported inside the project(If you export it as a .jar from Eclipse)
..Now you can call any method of (name).jar just(import it into the class
you want to use and call it)

java.lang.NoClassDefFoundError: org/htmlparser/util/ParserException

I'm trying to make this http://htmlparser.sourceforge.net/ code run in eclipse.
There instructions are simply "To use the library, you will need to add either the htmllexer.jar or htmlparser.jar to your classpath when compiling and running."
I've added htmllexer.jar and htmlparser.jar to my build path and everything compiles fine. But at runtime eclipse can't seem to find those jar files. I'm not sure how to add those jar files to the runtime classpath....
My eclipse looks like this:
For a web project you have to drop those libraries straight in the /WEB-INF/lib folder of the project. This folder participates in webapp's default runtime classpath. In Eclipse, you don't need to fiddle with the build path properties this way, Eclipse will do all the necessary magic. Don't forget to undo the changes you made before.
If I understand well, you are creating a web application. In that case, you have to configure properly the Deployment assembly section of your project properties.

After building the project from pom.xml using Maven, how do I use its resources

It's been tedious. This is the API I am trying to use. Its resources were set up in a pom.xml which I built using Maven. On built up, it gave me the project socrata-publisher that has
src/main/java the source folder with packages com.socrata.api com.socrata.data, com.socrata.util where each contains only .java
files
JRE System Library and Maven Dependency hierarchies where each contains a number of jar files
Problem is com.socrata.api and the 2 other contains classes which I want to deploy in a project outside socrata-publisher. I tried using import com.socrata.api but it didn't work. Moreover, since its a Java project and not android it doesn't have the is Library option in preferences which could rather give me the solution. Both socrata-publisher and tutorial (where i want to use the resources and which is the android application) lie in the same directory eclipseApps in My Documents.
Here's a little visual queue. Help will be greatly appreciated.
You will need to first of all get the output of the socrata project and all its dependencies.
In command line, going to the project folder of the socrata project, where the pom.xml file is, run MVN INSTALL. You shall see a jar file called socrata-api.jar in $HOME/.m2/repository. If you are using windows and installed MAVEN by default, $HOME should be your user profile folder. Once you see the jar file, add it to your tutorial build path.
I think what you actually want to do is just set up the "socrata-publisher" as a project dependency for your "tutorial" project. That will allow you to reference the built Socrata libraries from the code in your project.
Right click on the project and select "Properties". From within that dialog select "Java Build Path" on the left, then the "Projects" section, and click the "Add" button to add the "socrata-publisher" project.
I think that'll work better than creating a separate jar file that you then include, and then you can also keep the socrata-publisher code up to date using Git.

Categories