Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: import class from .jar file

  1. #1
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default import class from .jar file

    Hello everybody,

    I have a few questions regarding how to use a .class file I unpacked from a .jar file I downloaded from the Internet.

    I downloaded the commons-io-2.4.jar file from commons . apache . org /proper/commons-io/

    In it you can see that there is a class called FileUtil.class:

    jar tf commons-io-2.4.jar | grep FileUtil
    org/apache/commons/io/FileUtils.class

    I extracted this with the following command:

    jar xf commons-io-2.4.jar FileUtils.class

    It is now in ..uk/ac/ebi/bioinvindex/services/datasearch/commons/FileUtils.class

    Now, I have a java file where I would like to use FileUtils.

    How do I do that? How do I import it?

    Thanks, Matthew


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,388
    My Mood
    Mellow
    Thanks
    143
    Thanked 599 Times in 586 Posts

    Default Re: import class from .jar file

    Welcome to the Forum! Please read this topic to learn how to post code correctly and other useful tips for newcomers.

    A typical approach is to place the .jar file somewhere on the CLASSPATH (I have a folder for just for 3rd party .jar files) and then just import the desired components as you would any other import. .class files in those packages often have their own imports/dependencies on other pieces of the .jar file, so that if you separate the one file you think you need, you may find you'll have other broken imports if the whole .jar file is not on the CLASSPATH. It's easier to leave the .jar file together than taking it apart and having to manage the pieces.

  3. #3
    Junior Member
    Join Date
    Jun 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: import class from .jar file

    Okay, so you mean I don't have to extract any vlass files, just leave the original jar file anywhere on CLASSPATH.
    How should I write the import ... ; line in my Java code?
    Thanks for your quick reply.
    Matt

    --- Update ---

    Never mind, I figured it out!

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,388
    My Mood
    Mellow
    Thanks
    143
    Thanked 599 Times in 586 Posts

    Default Re: import class from .jar file

    Good job!

    What I was going to say (for the benefit of anyone else with the same question) is to follow the path you used to find and extract the desired .class file and put that in an import statement, something like:

    import org.apache.commons.ftp.FTPClient;

    imports the FTPClient.class file from the org.apache.commons .jar file.

Similar Threads

  1. classpath, .jar, and import statements
    By Skywola in forum Java Theory & Questions
    Replies: 1
    Last Post: October 20th, 2013, 07:03 AM
  2. Execution of .jar file: Could not find main class....Error'
    By suyog53 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: October 14th, 2012, 02:04 PM
  3. HOW TO AVOID JAR,CLASS FILE HACKING
    By huwijk in forum Java Theory & Questions
    Replies: 5
    Last Post: January 30th, 2012, 07:04 AM
  4. how to import my own class in my .java file
    By amr in forum What's Wrong With My Code?
    Replies: 1
    Last Post: December 17th, 2010, 08:31 PM
  5. Replies: 5
    Last Post: November 13th, 2010, 12:53 PM

Tags for this Thread