13

I'm on Windows and I've tried the -classpath under javac to no avail. Also tried moving the jar to the src directory, but java file still won't compile. Trying to do a simple import of httpclient in the script.

   import org.apache.http.client.*;
user
  • 86,916
  • 18
  • 197
  • 190
user1296537
  • 155
  • 1
  • 1
  • 6
  • Post what you add to the classpath and your directory structure. Otherwise, we can't help you. – Radu Murzea Apr 30 '12 at 16:15
  • Can you provide more information like the entire javac command, source/lib organization, etc? – srkavin Apr 30 '12 at 16:15
  • 3
    Do you really want to add jars to the **JDK**? Maybe you mean to the compilation classpath? – yair Apr 30 '12 at 16:17
  • 1
    *"tried the `-classpath` under `javac` to no avail."* Then your question should be "How do I make -classpath under javac work?". ***Do not add 3rd party Jars to the JRE (or JDK).*** – Andrew Thompson Apr 30 '12 at 16:22
  • Theres no reason to move the jar under the `src` directory, because youre not compiling the jar, youre using it to compile `.java` files, which are indeed under `src` directory. What exactly is the missing class? are you sure it is in the jar youre using? could be a dependency of that jar – jambriz Apr 30 '12 at 16:36
  • that will do please :D on the question.."How do I make -classpath under javac work?". puzzled why the classpath won't add httpclient-4.0.1.jar – user1296537 Apr 30 '12 at 16:44

2 Answers2

11

The -classpath command line argument (to both java and javac) expect that you will list specific JAR files (and/or "exploded" directories containing class files). This is unlike the Windows/UNIX PATH environment variable which just lists directories containing executables.

So for example, if you want to compile com.example.Foo that depends on lib/bar.jar you might use the following incantation:

javac -classpath lib/bar.jar com/example/Foo.java

But my guess is that you are also getting a specific error message, and knowing that error message would be useful in helping you further.

Adam Batkin
  • 51,711
  • 9
  • 123
  • 115
  • Using httpclient-4.0.1.jar. No matter how I link it in the classpath my java file won't compile. I'm getting no symbol errors all related to java unable to locate the package. I'm wondering if the import syntax in my java file needs to reflect the package I'm using is not a part of the jdk installation? – user1296537 Apr 30 '12 at 16:51
  • But you haven't said what your error message is yet. "When I run `javac ...` I get the following error: *whatever*". If you aren't getting any errors, then nothing is wrong...Unless you are expecting something different (in which case: What were you expecting to see that you didn't see?) – Adam Batkin Apr 30 '12 at 17:03
  • I'm getting no symbol errors from any of the classes referenced from httpclient.jar in my java file. such as, "cannot find symbol, class defaulthttpclient()" – user1296537 Apr 30 '12 at 17:25
  • If you aren't getting any errors, then it sounds like it is working. What makes you think that it isn't working? – Adam Batkin Apr 30 '12 at 19:33
  • Please edit your question and paste your entire console interaction there. We need to see your full command line (what you typed) and all of the output that `javac` printed to the console. – Adam Batkin May 01 '12 at 16:23
  • Thanks for your help. Having a new bizarrer problem now. Now that my java file has been compiled correctly courtesy of Netbeans and the classpath/jars issues settled, I'm getting NoClassDefFound Errors when I run my signed applet, though the java file originally built fine with all the needed imported classes of HttpClient. I added all of HttpClient's dependent lib/jars to the applet archive="" syntax but still the java console gives NoClassDefFound errors. There's a separate thread on that here. http://stackoverflow.com/questions/10341467/java-upload-applet-request/10354260 – user1296537 May 03 '12 at 00:20
7
  1. Install your JDK and leave it alone. Do not copy .jar files into your JDK folders!

  2. I'd recommend getting an IDE like Eclipse or Netbeans, if you don't already have one.

  3. I would set up a new project, create or import your source, and set a class path for the project.

  4. If that doesn't work, please tell us:

    a) your OS and version

    b) your JDK and version

    c) your IDE

    d) cut/paste the EXACT error message

    e) cut/paste the relevant code

paulsm4
  • 114,292
  • 17
  • 138
  • 190