Reputation: 5207
I am developing a java application which can send video to a php server. I had done it on android and the code below is working perfect on android but now when I tried this code in pure java(not android) I got exception. This is my java desktop code that uploads a video to php
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://b....vedioup.php");
FileBody filebodyVideo = new FileBody(new File(Path));
StringBody title = new StringBody("Filename: " + Path);
StringBody description = new StringBody(
"This is a description of the video");
MultipartEntity reqEntity = new MultipartEntity();
reqEntity.addPart("videoFile", filebodyVideo);
reqEntity.addPart("title", title);
reqEntity.addPart("description", description);
httppost.setEntity(reqEntity);
// DEBUG
System.out
.println("executing request " + httppost.getRequestLine());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
// DEBUG
System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
} // end if
if (resEntity != null) {
resEntity.consumeContent();
} // end if
httpclient.getConnectionManager().shutdown();
}
and this is the output in console.
Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory
at org.apache.http.impl.client.AbstractHttpClient.<init>(AbstractHttpClient.java:187)
at org.apache.http.impl.client.DefaultHttpClient.<init>(DefaultHttpClient.java:146)
at BlankFoem.uploadFile(BlankFoem.java:351)
It is clear that its an exception of class not found but how can I fix it.
Thanks
Upvotes: 1
Views: 155
Reputation:
Oh man you just have to add
apache.commons.logging jar
in to your project. Silly
Upvotes: 0
Reputation:
just add this jar file apache.commons.logging
to your project and you are done
Upvotes: 1
Reputation: 34628
You are using a deprecated class, DefaultHttpClient
. Here is its API Documentation. As you can see, it is marked as deprecated, and they recommend using HttpClientBuilder
instead.
Using the deprecated class calls a parent class (also deprecated) which requires a logging factory. I assume the apache.commons.logging
jar is not installed in your classpath, and therefore you get this particular exception.
It would be better to use HttpClientBuilder
as they recommend rather than installing the logging jar.
And the reason that it worked on android was because you probably had an older version of the apache HttpClient library there.
Here is a link to the new Apache HTTP Components library, including all the documentation and downloads.
Upvotes: 1