gpt4 book ai didi

java - 软件包org.apache.commons.httpclient不存在导入org.apache.commons.httpclient。*;编译Java时

转载 作者:行者123 更新时间:2023-12-02 11:08:07 27 4
gpt4 key购买 nike

我正在尝试从终端编译Java程序,但出现以下错误:

~/Desktop/test/myapp/HTTPRequest.java:3: error: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.*;
^
~//Desktop/test/myapp/HTTPRequest.java:4: error: package org.apache.http.impl.client does not exist
import org.apache.http.impl.client.CloseableHttpClient;
^
~//Desktop/test/myapp/HTTPRequest.java:5: error: package org.apache.http.impl.client does not exist
import org.apache.http.impl.client.HttpClientBuilder;
^
~//Desktop/test/myapp/HTTPRequest.java:6: error: package org.apache.http does not exist
import org.apache.http.HttpResponse;
^
~//Desktop/test/myapp/HTTPRequest.java:7: error: package org.apache.http does not exist
import org.apache.http.*;
^
~//Desktop/test/myapp/HTTPRequest.java:18: error: cannot find symbol
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
^
symbol: class CloseableHttpClient
location: class HTTPRequest
~//Desktop/test/myapp/HTTPRequest.java:18: error: cannot find symbol
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
^
symbol: variable HttpClientBuilder
location: class HTTPRequest
~//Desktop/test/myapp/HTTPRequest.java:19: error: cannot find symbol
HttpGet get = new HttpGet(url);
^
symbol: class HttpGet
location: class HTTPRequest
~//Desktop/test/myapp/HTTPRequest.java:19: error: cannot find symbol
HttpGet get = new HttpGet(url);
^
symbol: class HttpGet
location: class HTTPRequest
~//Desktop/test/myapp/HTTPRequest.java:23: error: cannot find symbol
HttpResponse response = httpClient.execute(get);
^
symbol: class HttpResponse
location: class HTTPRequest
10 errors

我的Java代码是这样的:
package test.myapp;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;

public class HTTPRequest {

private static String AUTHORIZATION_HEADER = "Authorization";
private static String ACCEPT_HEADER = "Accept";
private static String CONTENTTYPE_HEADER = "Content-type";
private static String APPLICATION_JSON = "application/json";

public static void getRequest() throws Exception {
String url= "";
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet get = new HttpGet(url);

get.setHeader(AUTHORIZATION_HEADER, "==");
get.setHeader(ACCEPT_HEADER, APPLICATION_JSON);
HttpResponse response = httpClient.execute(get);

System.out.println("Response is : " + response.getStatusLine().getStatusCode());
}

public static void main(String[] args) {
try {
getRequest();
}catch (Exception e) {
System.out.println("recvd an error: " + e.getMessage().toString());
}

}


}

我正在尝试以这种方式编译Java程序:
javac -cp ".;~/Desktop/test/commons-httpclient-3.1.jar;~/Desktop/test/httpclient-4.3.4.jar" ~/Desktop/test/myapp/HTTPRequest.java

我也只尝试了3.1和4.3.4 jar文件,但是仍然遇到相同的错误

最佳答案

实际上有3个问题:

  • 您的类路径中没有httpcore,它实际上是程序所引用的库。您只需要在类路径中使用httpcorehttpclient,就可以摆脱commons-httpclient
  • 您可以在类路径中使用分号作为分隔符,而在Windows之外的任何操作系统上都应使用冒号。我们仅在Windows操作系统上使用分号。
  • 不能使用代字号(〜)定义类路径,而需要提供完整路径more information in this answer。我引用:

  • The tilde (~) is a shell function and needs expanding before being sent to the Java process

    关于java - 软件包org.apache.commons.httpclient不存在导入org.apache.commons.httpclient。*;编译Java时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41616136/

    27 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com