So I'm a C++ Programmer who is trying to learn Java in order to write a client-side https receptor application, corresponding to a particular website.
My Problem:I found this wonderful-looking Java library online, called "HttpClient" (See https://hc.apache.org/httpclient-3.x/tutorial.html).
Unfortunately, the only way to install the library is manually (download a .zip file with all the .jar files in it). I understand that the include paths are meant to look something like this
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I have no idea how to configure the "CLASSPATH" Environment Variable in order to reflect this (please note that I know HOW to set the variable, just not WHAT to set it to).
My Question:
Could someone please explain to me (in laymen's terms) how to download & setup this library onto my Windows 10 PC such that I can implement the above include statements in my code?
You should install a build/dependency management tool such as maven (there are others). Then you will simply need to add the following to your project pom.xml and maven will handle the rest.
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
If you'd like to manually handle adding the .jar (and it's dependencies) you can specify "CLASSPATH=path/to/your.jar" or when running use "java -cp path/to/your.jar
If your project is maven then you can add dependencies in pom.xml as say above
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.2</version>
</dependency>
If your project is not a maven type then simply download the jar and add this jar file to the lib Folder. Your dependency will automatically resolved. No bug will raised due to dependency.
all you need to do is put the jar in your project's classpath..This can be done in multiple ways depending upon whether you are using any build tools like maven,gradle etc.
But since you are new to java,I suggest you download the jar from here and extract it and put it into any folder which is added to your classpath.
If you have created a webproject then by default WebContent->WEB-INF->lib is already in your calsspath. So just paste the .jar file into this file and you will be good to go.
If you are using maven or gradle or ivy then refer here: http://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.5.2
N.B.:you don't need to add anything to your windows classpath or modify any path variables of your OS. these are all project specific classpaths. So do it your project. Use a build tool like maven if you wish to access these more easily. Also recommended is to use eclipse or netbeans or some other mordern IDE
Just make sure that each of the .jar files it comes with are in your CLASSPATH.
e.g.
export MY_INSTALL_DIR=/some_dir/httpcomponents-client-4.5.2/lib
export CLASSPATH=$MY_INSTALL_DIR/commons-codec-1.9.jar:$MY_INSTALL_DIR/commons-logging-1.2.jar
and so on... I did the first two CLASSPATH elements above, and you ultimately need one element for each of 10 or so JARs I see in the dist.
Think of the .jar files as something like .so or .dll files - the compiled binaries. Think of this as the equivalent of putting .so or .dll files in your PATH.
In my example, I set the CLASSPATH via environment properties, like a .bashrc file or the like. Since you're on Windows, you would use the Windows environment variables available via the Advanced System Settings control panel. You can also set them via the command line (javac -classpath and java -classpath), via your build tools, or other ways. I found a couple of helpful links that discuss this: this one and this other one.
Long-term, you should learn how to use Maven, like the other answers here suggest, but first get to grips w/ Java basics. Just so you know, Maven is something like nmake from your world, plus the ability to automatically download and manage 3rd party dependencies, plus a whole lot of other features.
Related
I have a Java application that uses Maven, so the Maven POM file (pom.xml) lists all of the dependencies needed to compile and run the application.
I'd like to use groovy during my development and testing and then plug in the code into the Java files when I'm done. I haven't done this yet so it might not be feasible but if it is, that is what I'd like to try and do.
I would like to know if there is a function I can call that will read my pom.xml file and add all of the dependencies to my classpath so I can just import the packages that I need and goovy will already have them loaded (or at least know where to load them).
The groovy script might look like this:
addAllPomDependenciesToMyClasspath();
import my.code.pkg1;
import my.more.code.pkg2;
(new ClassInMyCode()).doCoolStuff();
Without this, I need to specify all my jars on the CLASSPATH and it would look something like this (I haven't yet done this because there are too many dependencies).
#!/bin/sh
groovy -cp "../lib1/target/MyJar1.jar:../lib2/target/MyJar2.jar:$HOME/.m2/repository/com/google/guava/guava/30.1.1-jre/guava-30.1.1-jre.jar: KEEP LISTING THESE UNTIL ALL DEPENDENCIES ARE INCLUDED"
Perhaps a better solution would be just to create a gradle file. Let me know if that is the preferred way (if there is no way to autoload all of the maven dependencies).
What have I tried
I've tried searching for solutions and found several ways to add your dependencies to the class path but they don't answer my question.
Some results were:
How to add multiple jars to the classpath of groovyConole/groovysh?
How do I auto load a database jar in Groovy without using the -cp switch?
Dynamically load jar in groovy
Including all the jars in a directory within the Java classpath
I'm guessing there is a "groovy" way to do this that approaches the problem differently and that is why I haven't found an answer to this question.
Misc Notes
(Q) How can I add multiple jar files to my groovy classpath? (A) Use "-cp jar1;jar2" (in Windows use semi-colons to separate the names of the jar files, and in Unix use colons to separate the filenames. See https://stackoverflow.com/a/219801/3281336
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)
Firstly this is a very n00b question. But being a junior dev I've never needed to import and work with other Java Frameworks. The standard library was always good enough for me to write the classes I needed to write.
But now getting exposed to more "advanced" concepts, I need to start working with external frameworks, e.g. JSON for Java, Apache's HttpClient for java and so on. And I'm looking for a basic understanding on how this works and how to go about importing these libraries so you can start working with the classes...
So my initial understanding is that each of these fraemworks will provide you with a .jar file that contains all the classes for the framework. Which you then import into your project and lo and behold you'll be able to use the classes/library in your project by just importing it e.g. 'import org.json.*;'
Is the understanding correct?
Correct.
You just add the libraries to your classpath and are now able to use classes from these libs.
How you add the libs to your classpath depends on your actual development environment. If you use Apache Maven for example, you just have to define the dependencies (libs) in your projects pom.xml and Maven downloads them automatically for you.
hth,
- martin
EDIT: The following only applies if you are not using automated build-tools like Maven or Ivy
Yes this is correct. To use a third party .jar file, download and place it in a convenient location (either system-wide or project-specific depending on your needs) and then include it in your classpath.
When executing from the command line do:
java -cp /path/to/library:. path.to.main
The :. is necessary so that the JVM will find your main method.
In an IDE you should be able to include the library in your classpath via the options menu.
Then you can just use the third party library like any other:
import name.of.library.class;
//Do something
I have a Java protect with the c3p0 JAR. Compiling my project in eclipse works. No errors or something. But when i try to run my application it says the following:
java.lang.NoClassDefFoundError: com/mchange/v2/c3p0/DataSources
Do i need to give the jar file the other jarfile from c3p0? Or could i link them when launging the application?
Note: I've understood your problem to be when running outside Eclipse.
The standard Java classloader responsible for loading your classes does not understand jar-files inside jar-files, so you need to do something else:
You can merge all classes from all the jar-files you use into a single jar file. (recommended for you at this point, not general recomendation)
You can put jar-files inside the single jar-files and use a special classloader which understands this.
You can put the referenced jars "next" to the single jar file containing your class files, and have the runnable jar include the necessary MANIFEST.MF voodo needed to refer to them.
The File->Export->Runnable jar option in Eclipse can do all three based on what you choose. Pick the one best suited to how you will get the classes to the final users.
Personally I like the "jars next to the generated jar" as it is the closest to what is supported out of the box by Java, while keeping the original jar files. The simplest is most likely to merge all classes, but when you get more advanced you will find that it has some disadvantages - at this point you will most likely not encounter them.
Add C3P0 as external jar in eclipse
It looks like you did not added c3p0 in the classpath when you run the sample.
However you could use maven and m2e plugin for eclipse and add the following
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
beside the other dependencies you have.
after mvn eclipse: eclipse and you will have your project with a correct classpath.
I have a Netbeans Java Project under Subversion (svn). This is shared with another guy working on the project.
The project requires an external library that we have as a jar file. Now, when either of us makes a change and commits it, the project's library dependencies fail as we have different paths for the library ( as we are working on different machines the location of the jar file is different ).
This means that every time I update my repository, I have to resolve library dependencies.
Is there a way I can prevent this?
1st way: You can use NetBeans Library support.
Go to Tools->Libraries, and new Library with your jar. Name it the same way on both machines and include to your NB project as Library rather then direct path to jar.
2nd way: use relative path.
Agree on having jar located at ../lib/foo.jar and use this path in NB project
3rd way: use property file.
Add file named, for example, build.properties but don't commit it to svn. So both of you will have different local version of that file.
Content should be something like next:
myproject.library.foojar=C:/foopath/foo.jar
In your build.xml include this file:
<property file="../build.properties"/>
In your nbproject/project.properties find a reference to the jar -- it will look like:
file.reference.foo.jar=C:/foopath/foo.jar
change it to
file.reference.foo.jar=${myproject.library.foojar}
Also you may want to add build.properties into ignore list for svn to avoid commiting it.
I recommend you to use Maven to control library dependencies(jars). It is easy to use and NetBeans has a module to use it easily.
Maven download all the dependencies you declared into a local repertory so you don't need to worried about managing libraries, Maven do it for you.
Also with this module you can search libraries in the Maven repertory(Has a lot of java librarys) only you need to type the name and maven download it for you.
Here there are some links for how to use Maven With NetBeans:
http://platform.netbeans.org/tutorials/nbm-maven-quickstart.html
http://today.java.net/article/2009/10/14/working-maven-netbeans-671