2017-01-12 4 views
-1

Ich versuche, mein Java-Programm aus dem Terminal zu kompilieren und ich erhalte die folgenden Fehler:Paket org.apache.commons.httpclient existiert nicht import org.apache.commons.httpclient. *; Beim Kompilieren 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 

Mein Java-Code ist wie folgt:

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()); 
     } 

    } 


} 

ich zu kompilieren Ich versuche meine Java-Programm auf diese Weise:

javac -cp ".;~/Desktop/test/commons-httpclient-3.1.jar;~/Desktop/test/httpclient-4.3.4.jar" ~/Desktop/test/myapp/HTTPRequest.java 

ich habe auch mit nur 3,1 und nur 4.3.4 jAR-Dateien versucht, aber ich bekomme immer noch den gleichen Fehler

+0

Warum die unten Abstimmung? – user1692342

Antwort

2

Es gibt tatsächlich 3 Ausgaben:

  1. Sie haben noch httpcore in Ihrem Classpath, die tatsächlich die Bibliothek, die Ihr Programm bezieht. Sie brauchen nur httpcore und httpclient in Ihrem Klassenpfad, Sie können commons-httpclient loswerden.

  2. Sie verwenden Semikolons als Trennzeichen in Ihrem Classpath, während die angeblich Doppelpunkte auf jedem Betriebssystem aber Windows verwenden. Wir verwenden Semikolons nur für Windows OS.

  3. Die Tilde (~) kann nicht zum Definieren Ihres Klassenpfads verwendet werden, stattdessen müssen Sie die vollständigen Pfade angeben, more information in this answer. Ich zitiere:

Die Tilde (~) ist eine Funktion, Shell und muss erweitert, bevor zum Java gesendet Prozess sein

Verwandte Themen