Findout the "codebase" ?

Discussion in 'Java' started by Meir, Jun 29, 2003.

  1. Meir

    Meir Guest

    Hi,

    I need to findout the exact folder from which my classes are loading.
    This could be a regular folder containing my .CLASS files,
    or a JAR file.

    How is it done ?
    Can I know whether it is a JAR file ?
    Can I distinguish the file-system hierarchy from the JAR
    internal hierarchy ?

    Alternatively: Is there a current-working-dir in Java applications ?
    How do I get/set ?

    Thanks in advance,
    Meir
     
    Meir, Jun 29, 2003
    #1
    1. Advertising

  2. Meir

    Jon A. Cruz Guest

    Meir wrote:
    > Hi,
    >
    > I need to findout the exact folder from which my classes are loading.
    > This could be a regular folder containing my .CLASS files,
    > or a JAR file.
    >


    Why?

    Why do you need to?


    > How is it done ?
    > Can I know whether it is a JAR file ?
    > Can I distinguish the file-system hierarchy from the JAR
    > internal hierarchy ?



    The different thing to do is just call getClass().getResouce() on some
    class in your code.
     
    Jon A. Cruz, Jun 29, 2003
    #2
    1. Advertising

  3. (Meir) writes:
    > It finds-out the folder from which the main module is loading, and
    > uses a relative sub-floder named "INI".


    If you want to use files, and not the new resource API:

    (a) Use the class loader of what you call the "main modul" (there is no
    such thing in Java), and let it figure out the location, and

    (b) use Properties files and the properties class:

    If you have an instance (e.g. if you are in a non-static method)

    InputStream in = this.getClass().getResourceAsStream(
    "/properties/config.properties" // your properties file name
    );

    If you don't have an instance (e.g. in a static method):

    InputStream in = <name-of-your-class>.class.getResourceAsStream(
    "/properties/config.properties" // your properties file name
    );

    (replace <name-of-your-class> with what it says). And then:

    Properties configData = new Properties();
    configData.load(in);
    in.close();

    And that's it. It will look similar if you want to stick with your XML.

    Note that you are supposed to use "/" and not "\\", even if you are on
    windows. This is a resource name, and not a file name. When the class
    loader searches for the resource, it converts the resource name to a
    file name, if necessary (actually, it converts it to a URL, but that is
    a detail).

    Using the class loader to look up the file works, because this is the
    same mechanism that is used to locate the class file itself. You don't
    have to know where it is, as long as the VM knows. And the VM has to
    know, otherwise it can't run your code.

    For more details (e.g. using a laeading '/' or not using one), you want
    to read the Class and ClassLoader API documentation.

    /Thomas
     
    Thomas Weidenfeller, Jul 1, 2003
    #3
  4. Try one of:

    1.

    public class Foo {
    public static void main(String[] args) {
    System.out.println(Foo.class.getClass().getProtectionDomain().getCodeSource().getLocation().toString());
    }
    }

    2. http://www.javaworld.com/javaworld/javatips/jw-javatip105.html

    (Meir) wrote in message news:<>...
    > Hi,
    >
    > I need to findout the exact folder from which my classes are loading.
    > This could be a regular folder containing my .CLASS files,
    > or a JAR file.
    >
    > How is it done ?
    > Can I know whether it is a JAR file ?
    > Can I distinguish the file-system hierarchy from the JAR
    > internal hierarchy ?
    >
    > Alternatively: Is there a current-working-dir in Java applications ?
    > How do I get/set ?
    >
    > Thanks in advance,
    > Meir
     
    Sandip Chitale, Jul 1, 2003
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. dbuchanan
    Replies:
    0
    Views:
    3,758
    dbuchanan
    Dec 9, 2005
  2. Dinesh Upare

    Problem using Codebase

    Dinesh Upare, Aug 25, 2003, in forum: ASP .Net
    Replies:
    0
    Views:
    368
    Dinesh Upare
    Aug 25, 2003
  3. BentleyInc

    codeBase tag for custom http handler

    BentleyInc, Oct 3, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    2,739
    Steven Cheng[MSFT]
    Oct 5, 2005
  4. Jon

    One codebase, multiple configs?

    Jon, Apr 6, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    414
  5. Jonas Smith
    Replies:
    3
    Views:
    92
    Jonas Smith
    Jun 6, 2008
Loading...

Share This Page