commons-logging.jar,commons-httpclient-3.1.jar和commons-codec-1.5.jar三个都下了,放在jdk的lib目录下,而且都加到环境变量classpath(.;C:\Program Files\Java\jdk1.6.0_02\lib\tools.jar;C:\Program Files\Java\jdk1.6.0_02\lib\commons-logging.jar;C:\Program Files\Java\jdk1.6.0_02\lib\commons-httpclient-3.1.jar;C:\Program Files\Java\jdk1.6.0_02\lib\commons-codec-1.5.jar,我的电脑右键设置的)中,环境变量path也设了(加了C:\Program Files\Java\jdk1.6.0_02\bin;进去).用javac编译能通过,但是用java命令解释执行就出现以下错误:
E:\spider>javac RetrivePage.javaE:\spider>java RetrivePage
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lo
gging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66) at RetrivePage.<clinit>(RetrivePage.java:9)
RetrivePage.java就是实现很简单的一个页面抓取功能,代码如下:import java.net.*;
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.logging.*; public class RetrivePage {
private static HttpClient httpClient = new HttpClient();
// 设置代理服务器
static {
// 设置代理服务器的IP地址和端口
httpClient.getHostConfiguration().setProxy("172.17.18.84", 8080);
}
public static boolean downloadPage(String path) throws HttpException,IOException {
InputStream input = null;
OutputStream output = null;
// 得到post方法
PostMethod postMethod = new PostMethod(path);
//设置post方法的参数
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name","lietu");
postData[1] = new NameValuePair("password","*****");
postMethod.addParameters(postData);
// 执行,返回状态码
int statusCode = httpClient.executeMethod(postMethod);
// 针对状态码进行处理 (简单起见,只处理返回值为200的状态码)
if (statusCode == HttpStatus.SC_OK) {
input = postMethod.getResponseBodyAsStream();
//得到文件名
String filename = path.substring(path.lastIndexOf('/')+1);
//获得文件输出流
output = new FileOutputStream(filename); //输出到文件
int tempByte = -1;
while((tempByte=input.read())>0){
output.write(tempByte);
}
//关闭输入输出流
if(input!=null){
input.close();
}
if(output!=null){
output.close();
}
return true;
}
return false;
} /**
* 测试代码
*/
public static void main(String[] args) {
// 抓取lietu首页,输出
try {
RetrivePage.downloadPage("http://www.lietu.com/");
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解压commons-logging.jar里面有LogFactory这个文件啊,怎么就说找不到呢?而且我都加了这个包了。版本问题么?我用commons-logging-1.1.jar和commons-logging-1.1.1.jar以及commons-codec-1.4.jar都试过了。特郁闷!哪位大哥知道错在哪里,小弟在此先谢了!
E:\spider>javac RetrivePage.javaE:\spider>java RetrivePage
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/lo
gging/LogFactory
at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:66) at RetrivePage.<clinit>(RetrivePage.java:9)
RetrivePage.java就是实现很简单的一个页面抓取功能,代码如下:import java.net.*;
import java.io.*;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.logging.*; public class RetrivePage {
private static HttpClient httpClient = new HttpClient();
// 设置代理服务器
static {
// 设置代理服务器的IP地址和端口
httpClient.getHostConfiguration().setProxy("172.17.18.84", 8080);
}
public static boolean downloadPage(String path) throws HttpException,IOException {
InputStream input = null;
OutputStream output = null;
// 得到post方法
PostMethod postMethod = new PostMethod(path);
//设置post方法的参数
NameValuePair[] postData = new NameValuePair[2];
postData[0] = new NameValuePair("name","lietu");
postData[1] = new NameValuePair("password","*****");
postMethod.addParameters(postData);
// 执行,返回状态码
int statusCode = httpClient.executeMethod(postMethod);
// 针对状态码进行处理 (简单起见,只处理返回值为200的状态码)
if (statusCode == HttpStatus.SC_OK) {
input = postMethod.getResponseBodyAsStream();
//得到文件名
String filename = path.substring(path.lastIndexOf('/')+1);
//获得文件输出流
output = new FileOutputStream(filename); //输出到文件
int tempByte = -1;
while((tempByte=input.read())>0){
output.write(tempByte);
}
//关闭输入输出流
if(input!=null){
input.close();
}
if(output!=null){
output.close();
}
return true;
}
return false;
} /**
* 测试代码
*/
public static void main(String[] args) {
// 抓取lietu首页,输出
try {
RetrivePage.downloadPage("http://www.lietu.com/");
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
解压commons-logging.jar里面有LogFactory这个文件啊,怎么就说找不到呢?而且我都加了这个包了。版本问题么?我用commons-logging-1.1.jar和commons-logging-1.1.1.jar以及commons-codec-1.4.jar都试过了。特郁闷!哪位大哥知道错在哪里,小弟在此先谢了!
解决方案 »
- ajax如何发送请求到action中?
- org.springframework.beans.factory.BeanCreationException
- java 入门
- struts2配置问题
- IE小问题
- Spring2+Hibernate3.1+Oracle9i(9.2.0.1.0)无法写入CLOB字段(附详细情况)
- Spring是否支持事物回滚?
- 在struts的jsp页面中怎样隐含传一个字符数组
- DBCP object created 2003-32-20 11:32:09 by the following code was never
- 怎样在weblogic中部署我写好的ejb?
- 查询为空的问题
- 关于业务逻辑的try catch的问题
加了环境变量,要重新开一个控制台才生效的。
有的系统需要重新登录或重新启动。