-
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. ]
-
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
-
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?
-
ClassLoader has protected Package[] getPackages(). Not if you would be able to use this for what you want though.
-
How would I get the location of my class file in the first place? That's the difficulty.
-
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?
-
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);
}
}
}
-
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.