5

Hi I keep getting an errors enter image description here

test.java:15: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.Cookie;
                                ^
test.java:16: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.HttpState;
                                ^
test.java:17: package org.apache.commons.httpclient does not exist
import org.apache.commons.httpclient.HttpClient;
                                ^
test.java:18: package org.apache.commons.httpclient.methods does not exist
import org.apache.commons.httpclient.methods.GetMethod;
                                        ^
test.java:22: cannot find symbol
symbol  : class HttpClient
location: class test
            HttpClient client = new HttpClient();
            ^
test.java:22: cannot find symbol
symbol  : class HttpClient
location: class test
            HttpClient client = new HttpClient();
                                    ^
test.java:26: cannot find symbol
symbol  : class GetMethod
location: class test
            GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");
            ^
test.java:26: cannot find symbol
symbol  : class GetMethod
location: class test
            GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");
                                   ^
test.java:29: cannot find symbol
symbol  : class Cookie
location: class test
                              Cookie[] cookies = client.getState().getCookies();
                              ^
test.java:31: cannot find symbol
symbol  : class Cookie
location: class test
                                    Cookie cookie = cookies[i];
                                    ^
10 errors

to compile I used

javac -cp ;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java

and this is the code

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.apache.commons.httpclient.Cookie;  
import org.apache.commons.httpclient.HttpState;  
import org.apache.commons.httpclient.HttpClient;  
import org.apache.commons.httpclient.methods.GetMethod; 


public class test{
public static void main (String []args)throws IOException{
    HttpClient client = new HttpClient();  
    client.getParams().setParameter("username", "SomeUSER");  
    client.getParams().setParameter("password", "GF@QT#$WE");  

    GetMethod method = new GetMethod("https://online.investools.com/authentication/auth.iedu");  
        try{  
              client.executeMethod(method);  
              Cookie[] cookies = client.getState().getCookies();  
              for (int i = 0; i < cookies.length; i++) {  
                Cookie cookie = cookies[i];  
                System.err.println(  
                  "Cookie: " + cookie.getName() +  
                  ", Value: " + cookie.getValue() +  
                  ", IsPersistent?: " + cookie.isPersistent() +  
                  ", Expiry Date: " + cookie.getExpiryDate() +  
                  ", Comment: " + cookie.getComment());  
                }  
              client.executeMethod(method);  
        } 
        catch(Exception e) {  
          System.err.println(e);  
        } 
        finally {  
          method.releaseConnection();  
        }

I am very confused and don't know what I'm doing wrong. I think it is simple, but I have checked several times and it exists and jsoup compiles fine. Thanks

3 Answers 3

8

I believe you want org.apache.http.client if you're using HttpClient 4.2 (which it looks like you are). The org.apache.commons.httpclient package is for the older version.

EDIT: Not all the classes which were in httpclient are now in http.client; some are just in http. Additionally, other changes are required - for example, HttpClient is now an interface, so you can't instantiate it like that. Basically, you've got 3.x code, so you should either update it to 4.x or use the 3.x jar files.

Sign up to request clarification or add additional context in comments.

5 Comments

<code>import org.apache.http.client.Cookie; <code>import org.apache.http.client.HttpState; <code>import org.apache.http.client.HttpClient; <code>import org.apache.http.client.methods.GetMethod;
@user1093111: "it didn't work" is hardly informative. It's possible that more work is required to convert 3.x code to 4.2, but we can't easily help you if you don't give us more information.
Sorry, its the same cannot find symbol error. Exactly like the top. 10 errors all the same. I re-downloaded and extracted the jar file as well.
@user1093111: It looks like this code was written for 3.x, yes. Not all the classes are in org.apache.http.client - some are just in org.apache.http. Read the docs for more information.
It compiled with 3.x, then I ran it and got 'code'Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/logging/LogFactory at org.apache.commons.httpclient.HttpClient.<clinit>(HttpClient.java:65) at test.main(test.java:21) Caused by: java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory at java.net.URLClassLoader$1.run(URLClassLoader.java:202) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
1

i found my missing version from here: http://mvnrepository.com/artifact/commons-httpclient/commons-httpclient

dependencies {
    compile 'commons-httpclient:commons-httpclient:3.1'
}

1 Comment

You should definitely drop 1-2 sentences and explain your solution. Just dropping code is frowned upon here. In which way does this answer the question?
0

use jar -xvf httpclient-4.2.java temp

check if org/apache/commons/httpclient/Cookie is there, if not there it is wrong/corrupted jar

Also try javac -cp .;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java instead of javac -cp ;./httpclient-4.2.jar;jsoup-1.6.3.jar test.java

1 Comment

java.io.FileNotFoundException: httpclient-4.2.java (The system cannot find the file specified) when using jar -xvf httpclient-4.2.java temp

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.