0

Without maven, gradle or something like this. How can I solve the problem below? I've read questions about java classpath. But I need to deep understand how to find a package and include it inside my project. Precisely, how to include like org.apache.http.client.methods.HttpGet in this project.

This is the HelloWorld.java file

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

This is the command

javac HelloWorld.java

This is the output

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

How can I solve this problem? How can I add vendor code? How can I import org.apache.http.client.methods.HttpGet? ...

Sorry, I came from PHP. In php we have composer and packagist. In Java world?

sensorario
  • 20,262
  • 30
  • 97
  • 159

2 Answers2

0

If you have downloaded the jar file containing the apache http stuff, change your commmand line this way

javac -classpath <the_location_of_the_jar>;. HelloWorld.java

The semicolon separates one or more locations, the dot means current directory

F Vicedo
  • 46
  • 3
0

First,the code you present has some syntax error: 1)you should import all the classes you used in the java file;2)you have spelling for "BufferedReader".3)USER_AGENT in your code has no definition. One modified code would like this: package 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);
    }
}

Then, we can start use javac to compile your code.your code has referred other classes such as HttpClient or BufferedReader etc. So you should tell javac the jars that contains the referred classes by two alternative means: 1)configure the system env variable "CLASSPATH",the value of which is semicolon separated list of jars that contains all you referred classes. 2)add -classpath option to you javac command,the option's value is the same as that of "CLASSPATH".For my windows environment ,i tested with this command: 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

I hope this could help you.