Results 1 to 5 of 5
  1. #1
    chrondog is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default How do I import a custom library?

    I put my program in
    Java Code:
    C:\Users\Documents\Java Programs\MyProgram.java
    I am using HTMLUnit and put the jar in the same folder
    Java Code:
    C:\Users\Documents\Java Programs\htmlunit.jar
    I want my program to use the htmlunit libraries without having to put it inside the jar. How do I do this?
    The only thing that felt like progress so far was doing this in the command prompt
    Java Code:
    java -cp "C:\Users\Documents\Java Programs\htmlunit.jar" com.gargoylesoftware.htmlunit.WebClient
    The WebClient part is a class in the library. The only reason I have it there is because it's the only thing that gave me a different result. I could replace it with another class in the library I'm sure and get the same error.

    But then it tells me I need a main method in this folder in order to set the class path. That doesn't make sense to me because why would a library have a main method?
    That's what my program is for. Isn't the classpath a location that the compiler will check for libraries? I know some of you may say you can just do
    Java Code:
    import htmlunit.com.gargoylesoftware.htmlunit.*;
    But when I use the WebClient class, it says

    Java Code:
    C:\Users\Documents\Java Programs\Testing.java:14: error: cannot access WebClient
    		WebClient webClient = new WebClient();
    		^
      bad class file: .\htmlunit\com\gargoylesoftware\htmlunit\WebClient.class
        class file contains wrong class: com.gargoylesoftware.htmlunit.WebClient
        Please remove or make sure it appears in the correct subdirectory of the classpath.
    1 error
    
    Tool completed with exit code 1
    Can someone please give me a guide on this?
    Last edited by chrondog; 02-20-2013 at 05:20 PM.

  2. #2
    chrondog is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Importing a custom library

    Was my post hard to understand? I'll restate what I asked in different words.

    I have a package I downloaded named htmlunit.jar.

    How can I import the classes in htmlunit.jar just like I can do "import java.awt.*;"?

    Just like I can link the standard SDK to any of my programs, I want to link htmlunit so I can use it.

    EDIT: I got it working with the NetBeans IDE. But still, I'd like to know how it is done manually.
    Last edited by chrondog; 02-20-2013 at 11:46 PM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Importing a custom library

    You mention you've put your java file somewhere, but where do you have your *class* files? Have you compiled your project yet into class files?

  4. #4
    chrondog is offline Member
    Join Date
    Feb 2013
    Posts
    3
    Rep Power
    0

    Default Re: Importing a custom library

    I was using TextPad which doesn't have "projects"; just a java file and the compiled class file. So yea I do have it compiled.

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Importing a custom library

    Just import it in your class.

    When you compile your class you need to include the jar file that has those classes in your classpath (-cp), and also when you run your class file.
    Thos are its dependencies...the 3rd party things your code needs in order to run.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Importing a web library
    By Boreeas in forum New To Java
    Replies: 9
    Last Post: 04-24-2011, 04:09 AM
  2. Importing a cvs library
    By malaguena in forum New To Java
    Replies: 2
    Last Post: 03-15-2011, 12:48 AM
  3. importing a library into my program
    By sniffer139 in forum NetBeans
    Replies: 1
    Last Post: 03-22-2010, 02:29 PM
  4. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 03:12 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 12:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •