Locating Jar Contents

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Greg Ofiesh

    Locating Jar Contents

    Dear news group friends,

    I am new to Java, only been working with it for a month or so. Here is
    my problem:

    I need to package my tree of classes into a Jar file. One of those
    classes must be able to determine at run time what Jar file it came
    from, and more importantly where that Jar file is located at. This is
    because it must pass the Jar file path and file name to another class
    that searches the Jar file for a data file located in the root folder
    in the Jar file.

    Does anyone know an easy way to have a class find the location of the
    Jar file it came from? Any help would be wonderful.

    Greg Ofiesh
  • Raymond DeCampo

    #2
    Re: Locating Jar Contents

    Greg Ofiesh wrote:[color=blue]
    > Dear news group friends,
    >
    > I am new to Java, only been working with it for a month or so. Here is
    > my problem:
    >
    > I need to package my tree of classes into a Jar file. One of those
    > classes must be able to determine at run time what Jar file it came
    > from, and more importantly where that Jar file is located at. This is
    > because it must pass the Jar file path and file name to another class
    > that searches the Jar file for a data file located in the root folder
    > in the Jar file.
    >
    > Does anyone know an easy way to have a class find the location of the
    > Jar file it came from? Any help would be wonderful.
    >
    > Greg Ofiesh[/color]

    Greg,

    Assuming you are using the default class loader and not anything funky
    (a J2EE environment would probably qualify as "funky"), you could use
    the Class.getResour ce() method to get the URL for the class file:

    // Start ResourceExample .java
    public class ResourceExample
    {
    public static void main(String args[])
    {
    System.out.prin tln(ResourceExa mple.class.getR esource(
    "ResourceExampl e.class"));
    }
    }
    // End ResourceExample .java


    # Start example output
    [tmp]$ java -cp . ResourceExample
    file:/tmp/ResourceExample .class
    [tmp]$ jar cvfm re.jar re.mf ResourceExample .class
    added manifest
    adding: ResourceExample .class(in = 937) (out= 533)(deflated 43%)
    [tmp]$ java -jar re.jar
    jar:file:/tmp/re.jar!/ResourceExample .class
    # End example output


    Of course, after rereading your original post, you may be able to do
    this by just using ClassLoader.get Resource() directly in your second
    class. For example, if you needed "xyz.txt" from the jar file you could do

    URL res = this.getClass() .getClassLoader ().getResource( "/xyz.txt");

    The only potential issue is if you have multiple "xyz.txt" files in the
    CLASSPATH.

    HTH,
    Ray

    --
    XML is the programmer's duct tape.

    Comment

    • Piet Blok

      #3
      Re: Locating Jar Contents

      gregofiesh@yaho o.com (Greg Ofiesh) wrote in
      news:d9196028.0 407201609.16765 609@posting.goo gle.com:
      [color=blue]
      > Dear news group friends,
      >
      > I am new to Java, only been working with it for a month or so. Here is
      > my problem:
      >
      > I need to package my tree of classes into a Jar file. One of those
      > classes must be able to determine at run time what Jar file it came
      > from, and more importantly where that Jar file is located at. This is
      > because it must pass the Jar file path and file name to another class
      > that searches the Jar file for a data file located in the root folder
      > in the Jar file.
      >
      > Does anyone know an easy way to have a class find the location of the
      > Jar file it came from? Any help would be wonderful.
      >
      > Greg Ofiesh
      >[/color]

      Hi Greg,

      Searching for a solution for a different problem I saw this (yet
      unaswered?) question.

      You might try to get the URL to your class from the ClassLoader. From
      this URL you might extract the path to that JAR file. Like this:

      import java.net.*;

      URL url = ClassLoader.get SystemResource( "com/mycompany/MyClass.class") ;
      // note that you must type slashes and not dots
      System.out.prin tln("path to my class: " + url.toString()) ;

      As you hopefully will see, there should be the path you are looking for.

      (I did not test this as I wrote it down, but I did a similar thing with
      some other file and that worked)

      Piet

      Comment

      • Piet Blok

        #4
        Re: Locating Jar Contents

        gregofiesh@yaho o.com (Greg Ofiesh) wrote in
        news:d9196028.0 407201609.16765 609@posting.goo gle.com:
        [color=blue]
        > Dear news group friends,
        >
        > I am new to Java, only been working with it for a month or so. Here is
        > my problem:
        >
        > I need to package my tree of classes into a Jar file. One of those
        > classes must be able to determine at run time what Jar file it came
        > from, and more importantly where that Jar file is located at. This is
        > because it must pass the Jar file path and file name to another class
        > that searches the Jar file for a data file located in the root folder
        > in the Jar file.
        >
        > Does anyone know an easy way to have a class find the location of the
        > Jar file it came from? Any help would be wonderful.
        >
        > Greg Ofiesh
        >[/color]

        Hi Greg,

        Searching for a solution for a different problem I saw this (yet
        unaswered?) question.

        You might try to get the URL to your class from the ClassLoader. From
        this URL you might extract the path to that JAR file. Like this:

        import java.net.*;

        URL url = ClassLoader.get SystemResource( "com/mycompany/MyClass.class") ;
        // note that you must type slashes and not dots
        System.out.prin tln("path to my class: " + url.toString()) ;

        As you hopefully will see, there should be the path you are looking for.

        (I did not test this as I wrote it down, but I did a similar thing with
        some other file and that worked)

        Piet

        Comment

        Working...