How do I find the container directory for a particular file when it
gets executed?
Ex:
c:\Tomcat\shared\lib\xyz.jar (xyz.jar contains abc.class)
I want to find out in which directory abc.class lies. If I try to find
the current directory from within the class using any of the following,
System.getProperty("user.dir");
String currentPath = new File(".").getPath();
String currentPath = new File(".").getCanonicalPath();
String currentPath = new File(".").getAbsolutePath();
I get "c:\win32" as my directory, probably because its the "current"
working directory for execution.
1) How can I get the path returned to be:
c:\Tomcat\shared\lib\ ?
2) Are these any Tomcat APIs that might help me get the full path of
where this file lies?
Thx for any help.
Rohit
------------
FeedFeeds : A new way to read news and blogs!
http://www.feedfeeds.com
>How do I find the container directory for a particular file when it
>gets executed?
If you mean class files:
see http://mindprod.com/jgloss/classpath.html#WHERE
if you mean exe files you exec, see
http://mindprod.com/projects/which.html
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.
Roedy, I tried out your code with a simple example.
c:\Level1\Level2\Test.java
String where = MyClass.class.getResource( "MyClass" ).getPath();
String where = Test.class.getResource( "Test" ).getPath();
This throws a null pointer exception. What am I doing wrong?
Pipe...@hotmail.com wrote:
> Hi thx for the replies.
>
> Roedy, I tried out your code with a simple example.
>
> c:\Level1\Level2\Test.java
>
> String where = MyClass.class.getResource( "MyClass" ).getPath();
> String where = Test.class.getResource( "Test" ).getPath();
>
> This throws a null pointer exception.
In which line?
> What am I doing wrong?
Are there *really files called "MyClass" and "Test" in this directory?
Or are there only the files "MyClass.class" or "Text.class"?
Ciao,
Ingo
String currentDir = Test.class.getResource("Test").getPath();
Aftr compilation I also tried removing the .java from the directory and
executed the .class. Still get the same exception.
This is my file:
----------------------------------------------------------------
public class Test{
public static String getCommonDataPath()
{
String currentDir = Test.class.getResource("Test").getPath();
return currentDir;
}
public static void main(String [] args)
{
System.out.println("currentDir : " + getCommonDataPath());
}
}
-------------------------------------------------------------------------
Pipe...@hotmail.com wrote:
> My directory contains the files Test.java and Test.class. At Runtime, I
> get a NullPointerException pointing to the line:
>
> String currentDir = Test.class.getResource("Test").getPath();
As I expected. What exactly do you expect? There is *no* resource/file
called "Test"!
Hint (is this really neccessary to say explicitely?): Try
String currentDir = Test.class.getResource("Test.class").getPath();
or perhaps:
String currentDir = Test.class.getResource(".").getPath();
or perhaps:
String currentDir = Test.class.getResource("/").getPath();
Ciao,
Ingo
>String where = Test.class.getResource( "Test" ).getPath();
>
>This throws a null pointer exception. What am I doing wrong?
Oops. as a resource, I should be looking for "MyClass.class" not
"MyClass"
I will fix that.
I also tried this out with one of my files packaged in a .jar, nested
in the Tomcat directory structure.
Curiously, the path I get is prefixed by "file:" i.e. something like
"file:\C:\Program Files\.....\Tomcat\webapps\commonfiles"
is it because this is getting executed from c:\win32 rather than the
actual location?
This doesn't seem to be causing any problems - windows seems to
understand it well. but if i were to deploy on Unix, that would mean
trouble.
when I tried the earlier example with "Test.java" and ran from the
container directory for Test.java, I got the path without the "file:\"
prefix?..
> Thanks Ingo. That fixed my problem. Finally have it working!! Never
> thought of explicitly naming the file!
>
> I also tried this out with one of my files packaged in a .jar, nested
> in the Tomcat directory structure.
>
> Curiously, the path I get is prefixed by "file:" i.e. something like
> "file:\C:\Program Files\.....\Tomcat\webapps\commonfiles"
>
> is it because this is getting executed from c:\win32 rather than the
> actual location?
?
> This doesn't seem to be causing any problems - windows seems to
> understand it well. but if i were to deploy on Unix, that would mean
> trouble.
No trouble at all. So long as the resource is found, it
will return a valid URL that points to the resource.
You might note that your resources in .jar archives will
have a '!' after the jar name to indicate the resource was
found inside a jar, rather than an oddly named directory.
>
> I want to find out in which directory abc.class lies.
Another method is to use
<classname>.class.getProtectionDomain().getCodeSource().getLocation().