没有maven,gradle或类似的东西。我怎样才能解决下面的问题?我读过有关java classpath的问题。但我需要深入了解如何找到一个包并将其包含在我的项目中。确切地说,如何在此项目中包括org.apache.http.client.methods.HttpGet
。
这是HelloWorld.java文件
import java.io.IOException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.HttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
public class HelloWorld {
public static void main (String[] args) throws IOException {
Runtime.getRuntime().exec("clear");
System.out.println("Console!!!");
String url = "http://www.example.com";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.addHeader("User-Agent", USER_AGENT);
HttpResponse response = client.execute(request);
BufferReader rd = new BufferReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
}
}
这是命令
javac HelloWorld.java
这是输出
HelloWorld.java:2: error: package org.apache.http.client.methods does not exist
import org.apache.http.client.methods.HttpGet;
^
HelloWorld.java:3: error: package org.apache.http.client does not exist
import org.apache.http.client.HttpClient;
^
HelloWorld.java:4: error: package org.apache.http.impl.client does not exist
import org.apache.http.impl.client.HttpClientBuilder;
^
HelloWorld.java:13: error: cannot find symbol
HttpClient client = HttpClientBuilder.create().build();
^
symbol: class HttpClient
location: class HelloWorld
HelloWorld.java:13: error: cannot find symbol
HttpClient client = HttpClientBuilder.create().build();
^
symbol: variable HttpClientBuilder
location: class HelloWorld
HelloWorld.java:14: error: cannot find symbol
HttpGet request = new HttpGet(url);
^
symbol: class HttpGet
location: class HelloWorld
HelloWorld.java:14: error: cannot find symbol
HttpGet request = new HttpGet(url);
^
symbol: class HttpGet
location: class HelloWorld
HelloWorld.java:15: error: cannot find symbol
request.addHeader("User-Agent", USER_AGENT);
^
symbol: variable USER_AGENT
location: class HelloWorld
HelloWorld.java:16: error: cannot find symbol
HttpResponse response = client.execute(request);
^
symbol: class HttpResponse
location: class HelloWorld
HelloWorld.java:19: error: cannot find symbol
BufferReader rd = new BufferReader(
^
symbol: class BufferReader
location: class HelloWorld
HelloWorld.java:19: error: cannot find symbol
BufferReader rd = new BufferReader(
^
symbol: class BufferReader
location: class HelloWorld
HelloWorld.java:20: error: cannot find symbol
new InputStreamReader(
^
symbol: class InputStreamReader
location: class HelloWorld
12 errors
我该如何解决这个问题?如何添加供应商代码?我怎样才能导入org.apache.http.client.methods.HttpGet? ...
抱歉,我来自PHP。在PHP中我们有作曲家和包装。在Java世界?
答案 0 :(得分:0)
如果您已下载包含apache http stuff的jar文件,请以这种方式更改您的命令行
javac -classpath <the_location_of_the_jar>;. HelloWorld.java
分号分隔一个或多个位置,点表示当前目录
答案 1 :(得分:0)
首先,您提供的代码有一些语法错误: 1)你应该导入你在java文件中使用的所有类; 2)你有“BufferedReader”的拼写.3)你的代码中的USER_AGENT没有定义。 一个修改过的代码会这样: 包com; import java.io。*;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.*;
import org.apache.http.client.*;
import org.apache.http.impl.client.HttpClientBuilder;
public class HelloWorld {
public static void main (String[] args) throws IOException {
Runtime.getRuntime().exec("clear");
System.out.println("Console!!!");
String url = "http://www.example.com";
HttpClient client = HttpClientBuilder.create().build();
HttpGet request = new HttpGet(url);
request.addHeader("User-Agent", "XXXX");
HttpResponse response = client.execute(request);
BufferedReader rd = new BufferedReader (
new InputStreamReader(
response.getEntity().getContent()
)
);
StringBuffer result = new StringBuffer();
String line = "";
while ((line = rd.readLine()) != null) {
result.append(line);
}
System.out.println(result);
}
}
然后,我们可以开始使用javac来编译你的代码。你的代码引用了其他类,比如HttpClient或BufferedReader等。所以你应该通过两种替代方法告诉javac包含引用类的jar:
1)配置系统env变量“CLASSPATH”,其值是以分号分隔的包含所有引用类的jar的列表。
2)为你的javac命令添加-classpath选项,该选项的值与“CLASSPATH”的值相同。对于我的windows环境,我用这个命令测试:
C:\Users\Administrator>javac -classpath "C:\Program Files\Java\jdk1.8.0_66\jre\lib;C:\Program Files\Java\jdk1.8.0_66\lib;C:\com\lib\httpclient-4.5.2.jar;C:\c\lib\httpclient-cache-4.5.2.jar;C:\com\lib\httpcore-4.4.4.jar;C:\com\lib\httpclient-win-4.5.2.jar;C:\com\lib\httpmime-4.5.2.jar" c:\com\HelloWorld.java
我希望这可以帮到你。