Results 1 to 8 of 8

Thread: Code location *resolved*

  1. #1

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Code location *resolved*

    Is there any way I can find the load base of a class in Java? E.g. I have the class
    com.cornedbee.Freaky
    and the class file is located at
    c:\java\classes\com\cornedbee\Freaky.class
    that I get
    c:\java\classes\
    ?

    I desperatly need this, or another way to have a uniform way of locating my resource files without passing parameters to my app.

    [EDIT: Kind of resolved. I can't get the location of my class file, but I can load things from there using ClassLoader.getResource. ]
    Last edited by CornedBee; Jan 7th, 2004 at 05:30 AM.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  2. #2
    Retired G&G Mod NoteMe's Avatar
    Join Date
    Oct 2002
    Location
    @ Opera Software
    Posts
    10,190
    Maybe you can get getpPath??


    getAbsolutePath:
    fully qualified directory + name + extension. It won't give you the precise case of the file! It will echo back to you the case you used.


    read about it here....never used it before. So I am not 100% how it is working.

    http://mindprod.com/jgloss/file.html

  3. #3
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    So you want C:\java? I think the closest you could get to that using any methods from java.io.File would be to use either getParent() or getParentFile(). You would end up getting c:\java\classes\com\cornedbee in the end.

    Why not just get the full path using getAbsolutePath() and parse?

  4. #4
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    ClassLoader has protected Package[] getPackages(). Not if you would be able to use this for what you want though.

  5. #5

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    How would I get the location of my class file in the first place? That's the difficulty.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  6. #6
    The Devil crptcblade's Avatar
    Join Date
    Aug 2000
    Location
    Quetzalshacatenango
    Posts
    9,091
    Code:
    import java.io.*;
    
    class Test 
    {
    	public static void main(String[] args) 
    	{
    		File f = new File("");
    
    		System.out.println(f.getAbsolutePath());
    	}
    }
    Or did I misunderstand?
    Laugh, and the world laughs with you. Cry, and you just water down your vodka.


    Take credit, not responsibility

  7. #7
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    Maybe you could try somthing like this. Im not quite sure if this would work because a NullPointerExcepiton is being thrown at System.out.println(p.getName());. null seems to be returned because a package object isnt being created by the class loader of the Q class. Also the getName() method of the package class should return the whole package structure ie. java.classes.com.cornedbee.
    Code:
     import java.io.*; 
    
     class Q{
      private int i; 
      private int x;
    
      public Q(int i, int x){
        this.i = i; 
        this.x = x;    
      }    
     }
    
     public class X{
      public static void main(String[] args){
        Q q = new Q(22,33);
          
        try{
         Class c = Class.forName("Q");
         Package p = c.getPackage();
         System.out.println(p.getName());
      
       }catch(ClassNotFoundException e){
         System.err.println(e); 
       
      }
     }
    }

  8. #8

    Thread Starter
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594
    I'm sorry if this still isn't clear. I don't know what new File("") is supposed to do, but I very much doubt it will give me the location of the current class file, at best it will give me the current working directory (not necessarily the same).

    Here's the deal. I store the saved data of my app in an XML file that has a DOCTYPE declaration with a DTD. This DTD is stored in the dtd subdirectory of my application base. On my computer, my application base is
    d:\work\daten\java\NetBeans\Projects\JKnowledger
    but of course this will change on other computers.
    My application's main class is com.cornedbee.jknowledger.main.JKnowledger, the class file resides at
    d:\work\daten\java\NetBeans\Projects\JKnowledger\com\cornedbee\jknowledger\main\JKnowledger.class
    and the XML DTD resides at
    d:\work\daten\java\NetBeans\Projects\JKnowledger\dtd\data.dtd

    The XML file contains a system identifier as reference to the DTD:
    <!DOCTYPE data SYSTEM "resource:dtd/data.dtd">

    I have a custom entity resolver to make use of this resource: URI. This entity resolver takes the path and resolves it relative to the application base. This class is
    com.cornedbee.util.ResourceEntityResolver
    and resides at
    d:\work\daten\java\NetBeans\Projects\JKnowledger\com\cornedbee\util\ResourceEntityResolver.class

    However, on another computer this class might be at
    c:\program files\jknowledger\com\cornedbee\util\ResourceEntityResolver.class

    My question is, how do I find out where this file actually is (and thus, what the application base is and how I should resolve the resource: URIs)? I want some way to do it without passing it as parameter to my app.

    Thx in advance.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

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



Click Here to Expand Forum to Full Width