www.webdeveloper.com
Results 1 to 8 of 8

Thread: How to import/use .jar files?

  1. #1
    Join Date
    Mar 2005
    Posts
    3

    How to import/use .jar files?

    I'm familiar with how to do imports in C/C++, but this thing with jar files has me mystified -- and the question seems too basic to find the answer anywhere.

    A coworker gave me a bunch of .jar files, each with a number of classes I need to incorporate into my code. However I don't know where the heck to put the files, or how to write the import statement to import the file correctly.

    I've been told I have to define the CLASSPATH environment variable, so I set it to C:/java/tools/, but just placing my jar files in there doesn't seem to work. I feel like I'm missing something really basic here.

    Help please?

  2. #2
    Join Date
    Nov 2003
    Location
    Jerryville, Tejas
    Posts
    11,715
    You put the JARs on the classpath and the CLASSES in your imports. For Windows that looks like:

    set CLASSPATH=C:/java/tools/lib1.jar;C:/java/tools/lib2.jar;%CLASSPATH%

    Then your imports in your code are for fully qualified classes (best practice) or wild-carded packages.

    import java.io.*;
    import java.math.BigDecimal;
    "Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." Brian W. Kernighan

  3. #3
    Join Date
    Mar 2005
    Posts
    3
    Hey, thanks! That worked -- but one question. Given the .class files are encoded, how do I know the exact path to the object in question for the import statement, or even what objects are available, or what their public properties and methods are?

    In C++ there would be a header file that would be a public declaration of all the object handles, but here I see nothing? Is there a tool I would use to do this?

    For example, one of the .jar files contains an "activityID.class" file that contains an object "activityID". But the import I was told to use was something like:

    import com.pm.public.someotherthing.somethingmore.activityID

    even though the actual file is in C:\java\tools\activity_lib.jar

    Seems like this is something that would be covered in the basic documentation, but again, I see naught.

  4. #4
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    if java doc was not created for the classes that you are using then its a tough one.
    Essentially in Java any reasonable developer will produce a Java doc with their distributable jars.
    Now if you were to use any Java IDE then your work is mostly done(e.g eclipse, or IntelliJ).
    If you use one of the above and add the jar files to the libraries that are required for the project then context sensitive menu will appear to display any packages available, classes in those packages and public methods and properties etc.
    Java IDE work just like C/C++ IDE's
    To access public methods of a class
    SomeClass sc = new SomeClass();
    sc.AnyPublicMethod()
    or property will display after the dot operator.
    Last edited by Khalid Ali; 04-06-2005 at 11:28 AM.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  5. #5
    Join Date
    Mar 2005
    Posts
    31
    JAR Files [17/18]


    Each applet file means separate request
    .class files, images, audio files, etc.
    Java ARchive file can hold all applet files
    Use jar tool to archive necessary files
    Use jarsigner tool to digitally sign JAR file
    Use ARCHIVE attribute in APPLET tag
    <APPLET... ARCHIVE="foo.jar" ...>
    Archive searched before requesting server file
    Operation Command
    To create a JAR file jar cf jar-file input-file(s)
    To view the contents of a JAR file jar tf jar-file
    To extract the contents of a JAR file jar xf jar-file
    To extract specific files from a JAR file jar xf jar-file archived-file(s)
    To run an application packaged as a JAR file
    (version 1.1) jre -cp app.jar MainClass

    To run an application packaged as a JAR file
    (version 1.2 -- requires Main-Class
    manifest header) java -jar app.jar

    To invoke an applet packaged as a JAR file <applet code=AppletClassName.class
    archive="JarFileName.jar"
    width=width height=height>
    </applet>

  6. #6
    Join Date
    Mar 2005
    Posts
    3
    Thanks for the responses -- so basically, unless the develop sends me documentation with the jar file, I'm pretty well up a creek? Like I said, comingfrom a C++ environment, that seems a little weird, but I can live with it. Does the Jar file at least have residual directory information so I can see what the package names ought to be? I know I can extract the individual classes... but that does me little good if I can't reference the full package name.

    Thanks again!

  7. #7
    Join Date
    Dec 2002
    Location
    Calgary, Canada
    Posts
    6,120
    a jar file is nothing but a zipped file just with different extension.
    You can open a jar file in your favourite zip/unzip utility (I use WinRAR) and view the package structure as well as classes in them.
    Cheers

    Khalid

    Message Posting Guidelines In These Forums, Please read these before posting any question.
    Web site: webapplikations.com
    Web Resources Page:Web Resources

  8. #8
    Join Date
    Aug 2013
    Posts
    2
    I also have problem relating with the jar file..
    what should i do??
    I got Packages does not exist error..
    Help me please..

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles