|
-
Dec 22nd, 2003, 04:38 PM
#1
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.
-
Dec 22nd, 2003, 05:49 PM
#2
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
-
Dec 23rd, 2003, 12:19 AM
#3
Dazed Member
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?
-
Dec 23rd, 2003, 12:40 AM
#4
Dazed Member
ClassLoader has protected Package[] getPackages(). Not if you would be able to use this for what you want though.
-
Dec 23rd, 2003, 05:49 AM
#5
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.
-
Dec 23rd, 2003, 09:46 PM
#6
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
-
Dec 24th, 2003, 12:01 AM
#7
Dazed Member
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);
}
}
}
-
Dec 24th, 2003, 05:47 AM
#8
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|