包org.apache.commons.httpclient不存在import org.apache.commons.httpclient。*;在编译Java时

时间:2017-01-12 14:51:56

标签: java compiler-errors

我正在尝试从终端编译我的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文件,但我仍然得到相同的错误

1 个答案:

答案 0 :(得分:2)

实际上有3个问题:

  1. 您的类路径中没有httpcore,这实际上是程序引用的库。您只需在类路径中httpcorehttpclient,就可以摆脱commons-httpclient

  2. 您在类路径中使用分号作为分隔符,而您应该在除Windows之外的任何操作系统上使用冒号。我们仅在windows os上使用分号。

  3. 波浪号(〜)不能用于定义类路径,您需要提供完整路径more information in this answer。我引述:

  4.   

    波浪号(〜)是一个shell函数,需要扩展才能生成   发送到Java进程