How do I import "HttpClient" in Eclipse? I downloaded HttpClient from http://hc.apache.org/downloads.cgi just now. I added it to my Eclipse new java project and want to run a example copy from website.
This example uses import org.apache.commons.httpclient.*; But, what pity is, it shows that Eclipse can't resolve this.
Now, I want to know the right way to import new released HttpClient to my project.
Is it necessary to add some jar to classpath? What is it?
This is the whole example I run. I guess new released "HTTPClient" changed its import jar, is it true?
package http.demo;
import java.io.IOException;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
public class SimpleHttpClient {
public static void main(String[] args) throws IOException {
HttpClient client = new HttpClient();
client.getHostConfiguration().setHost( "www.imobile.com.cn" , 80, "http" );
method = getPostMethod();
client.executeMethod(method);
System.out.println(method.getStatusLine());
Stringresponse=newString(method.getResponseBodyAsString().getBytes("8859_1"));
System.out.println(response);
method.releaseConnection();
}
private static HttpMethod getGetMethod(){
return new GetMethod("/simcard.php?simcard=1330227");
}
private static HttpMethod getPostMethod(){
PostMethod post = new PostMethod( "/simcard.php" );
NameValuePair simcard = new NameValuePair( "simcard" , "1330227" );
post.setRequestBody( new NameValuePair[] { simcard});
return post;
}
}
It works, it Solved:
first download file JAR from web apache https://hc.apache.org/downloads.cgi.
extract file zip
open your eclipse project
Do right click libs on the Package Explorer and choose Build Path -> Configure Build Path
Choose Java Build Path on the left side box
Click tab Libraries.
Add External JAR, choose your extracted file on point (2)
You may choose all file JAR on extracted file, it depends on your imported at your project.
You drag the jar file to your project so you can see it inside Eclipse.
To give it special meaning to Eclipse, right click on the jar file inside Eclipse and select Build Path -> Add to Build Path.
Now your imports should resolve properly.
Go to:
https://hc.apache.org/downloads.cgi
download the *****.tar.gz file
extract it
go inside the lib folder, there you'll find all the JARs
open Eclipse, right click on your project -> Properties -> Java Build Path ->
Libraries tab -> Add External JARs - > choose all the JARs in lib (step 4)
to test I recommend you try to run some code that uses this library like:
http://www.mkyong.com/java/apache-httpclient-examples/
you'll probably see a red underline, hover it and choose Import.....
good luck
Related
I am trying to connect to a MongoDB database hosted on mlab using the Java driver on a servlet.
import org.bson.Document;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientURI;
import com.mongodb.client.MongoCollection;
import com.mongodb.client.MongoDatabase;
public class MongoConnection {
protected void connectToMongo(String loc){
String dbName = "readings";
String collection = "data";
MongoClientURI uri = new MongoClientURI("mongodb://user:pass#ds143109.mlab.com:43109/readings");
MongoClient client = new MongoClient(uri);
MongoDatabase db = client.getDatabase(dbName);
MongoCollection<Document> readings = db.getCollection(collection);
Document doc = Document.parse(loc);
readings.insertOne(doc);
client.close();
}
}
The problem is I am getting the following error:
java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
I looked at one answer (How to resolve ClassNotFoundException: com.mongodb.connection.BufferProvider?) that highlighted to me that I need other jars, I have since downloaded them however I am still getting this error.
I am using Eclipse and adding the three jars to the build path, navigating through the menu by right clicking on the project then following Build Path -> Configure build path -> Java build path -> libraries -> add external JARs.
Is this the right way to do it? Is there something else I am supposed to do as well/instead?
You have java.lang.NoClassDefFoundError - that means your class is missed during runtime (not during build/compile time). So you should open your "Run Configurations" dialog for the project (project context menu -> "Run As" -> "Run Configurations...") and make sure you have bson-xxx.jar, mongodb-driver-xxx.jar, and mongodb-driver-core-xxx.jar somehow listed in Classpath tab. And yes, like Xavier Bouclet said - if you run it under application server - this jars should be added to your server's classpath.
You have to make sure that the mongodb jars are exported to server if your call to the database are made from the servlet.
Check how you deploy your app on your local server dans make sure the jars are there.
I was facing similar issue with my Mule 4 project.
Failed to invoke lifecycle phase "initialise" on object:
That was pointing to:
java.lang.NoClassDefFoundError: com/mongodb/MongoClientURI
So I had to updated POM file in plugin section (mule-mave-plug, idk what would it be in java project):
<sharedLibraries>
<sharedLibrary>
<groupId>org.mongodb</groupId>
<artifactId>mongodb-driver-legacy</artifactId>
</sharedLibrary>
</sharedLibraries>
So I was searching for a command line tool to de-compile .jar files, I found this "jd-cmd" at https://github.com/kwart/jd-cmd and it worked fine, but now I saw it has an api that I can use, I can't seem to do it the wright way. I am working on intellij IDE, added the external jars to my project, all I want is that the output will be at a folder of my choice in the filesystem just like it works at the command line with no problems.
here's an example code I tried from the idea that didn't do the job:
import java.io.File;
import jd.core.input.JDInput;
import jd.core.input.ZipFileInput;
import jd.core.output.DirOutput;
import jd.core.output.JDOutput;
import jd.ide.intellij.JavaDecompiler;
public class App {
public static void main(String[] args) {
JavaDecompiler javaDecompiler = new JavaDecompiler();
//choose input plugin for your decompiled file (class, zip, directory)
JDInput jdIn = new ZipFileInput("path/to/myFavorityLib.jar");
//choose output plugin (zip, directory, console)
JDOutput jdOut = new DirOutput(new File("/tmp/decompiled"));
//decompile
jdIn.decompile(javaDecompiler, jdOut);
}
}
This code didnt generate decompiled files at "/tmp/decompiled"
ok, so i have managed to do it .
it was a dependency problem , the github developer mentioned that jd-lib folder should be added as a dependency library , but actually it dosent matter that much ,you need to add a jar file :after doing all the instructions and downloding what ever maven will give you, at the folder jd-cli there a downloaded jar "jd-cli.jar" , use this as a dependency for your project and it work just fine!
I have physically copy xmlrpc-client-3.1.3.jar to./lib,then right click on it to select "Build Path>Add Path".
On Menu Bar,select Project >Properties>Java Build Path.
On "Libraries" tab ,xmlrpc-client-3.1.3.jar found and
"Order and Export" tab has checked on xmlrpc-client-3.1.3.jar-XXX/lib
By the time,I code as below
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
public class someClass{
....
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL("http://somedomain.com/index.php/api/xmlrpc/"));
}
I get the error
The type org.apache.xmlrpc.common.XmlRpcHttpRequestConfigImpl cannot
be resolved. It is indirectly referenced from required .class files
I use eclipse on project development
I had same issue but now I have managed to resolve it. Your just missing the xmlrpc client common jar file.
Just download the following package
xmlrpc-client-common-3.1.3.jar
Add it to your project using same procedures you did for adding xmlrpc-client-3.1.3.jar
CHEERS
And you can download it here. Using xmlrpc client jar alone will give you error; so just download the xmlrpc common, add jar as external, clean your project and your ready to go. happy codings,
-cheers
I have the following code...
public class first
{
public static void main (String[] args)
{
WebSpec spec = new WebSpec().safari();
spec.safari().open("http://www.google.com"); //opens google.com in Safari
}
}
written in eclipse.
WebSpec is underlined in red giving an error saying "cannot be resolved to be a type"
I assume this is because some of the libraries are not in the right spot? I have tried many different things.
What do I need to do?
Be sure to include all .jar's included in the webspec download in your library. There are .jar's in the lib folder and also one .jar in java\dist of webspec. Eclipse allows you to import libraries by highlighting your project then Project > Properties > Java Build Path > Libraries (tab).
I am trying to import:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
But I am being told these do not exist?
I downloaded:
httpclient-4.0.1.jar and httpmime-4.0.1.jar
... and placed these in the same folder as my .java files that are trying to use httpclient.
Any ideas?
I still cannot get it to work... Within the folder "Libraries" I have: apache-mime4j0.6.jar commons-codec-1.3.jar commons-logging-1.1.1.jar httpclient-4.0.1.jar httpcore-4.0.1.jar httpmime-4.0.1.jar For the java file properties it has: compile classpath runtime classpath boot classpath In each of those, it seems to refer to the jars I have imported. Still getting does not exist. :-(
I have tried to do this in Eclipse too and now those files appear in "Referenced libraries" however it still doesn't work. lol
The two jars you have mentioned need to be placed in the classpath of the project in Netbeans, not in the source directory.
In my Netbeans 6.7.1 on Mac, in the Prjects tab, you cna right click on the project and select Properties. That will bring up the project properties dialog. In there, choose the libraries item from the tree on the left. From there, choose the Add Jar/Folder in the Compile view. To add the jar to your project, use the chooser to locate it and then select it.
EDIT:
I have just downloaded the HTTPClient package and I think I see the problem:
in 4.0.1, the package structure is not as you have it defined. Instead of:
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.params.HttpMethodParams;
use:
import org.apache.http.client.*;
import org.apache.http.client.methods.*;
import org.apache.http.client.params.HttpMethodParams;
In Eclipse, press Ctrl + Shift + O to organize your imports. This will look for all unknown classes on the classpath and try to import them. You can also place your cursor on a class name and press Ctrl + Shift + M to attempt to import that single class. This is sometimes helpful for class name collision (i.e. if two packages have a HttpClient class, you can click on the desired class).
If the jars are in Referenced Libraries, then they should be on your classpath. You can verify this by right clicking the project and selecting something like Build Path > Configure Build Path, then click the libraries tab.
Also, you probably have build automatically selected by default, but if you don't, you'll need to build your project. You may also want to attempt to clear the build path and re-build it. I've seen my Eclipse get out of synch a few times and this fixed it, albeit somewhat of a fluke.
If you're using Maven, this sort of thing can sometimes occur if you have an incorrect dependency scope (i.e. runtime, or test vs. compile).
For what it's worth, unless you're utilizing the entire package, there is no reason to import an entire package's contents (i.e. import package.*).
It seems that HttpClient has changed his sintaxis from 3 to 4 version... I had same problems that all of you trying to import packages until I found this example:
http://w3mentor.com/learn/java/android-development/android-http-services/example-of-http-get-request-using-httpclient-in-android/
This is sample is Android oriented but works on any Java Application!!! Im using netbeans 6.9.1, httpclient-4.1.1.jar, commons-codec-1.4.jar and commons-logging-1.1.1.jar
Hope you can solve your problems!!!
Had the same problem and i managed to get the solution. Here it is:
1) Download the org.apache.commons.httpclient.jar.zip file from
http://www.java2s.com/Code/Jar/o/Downloadorgapachecommonshttpclientjar.htm
and save it anywhere on your computer.
2) Right click on your NetBeans project and select Properties
3) On Project Properties Categories, select Libraries
4) Click the Add JAR/Folder button
5) Now browse to the file location where you saved your downloaded org.apache....jar.zip
file and click open button.
6) Now the file has been installed, click OK and you are done.
Note that this might require you to restart your netbeans IDE.