Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

finding container directory of an executing class?

0 views
Skip to first unread message

Pipe...@hotmail.com

unread,
Sep 29, 2005, 12:20:06 AM9/29/05
to
Hi,

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

jessu

unread,
Sep 29, 2005, 12:26:33 AM9/29/05
to
object.getClass().getResource("yourclassname").getPath();

------------
FeedFeeds : A new way to read news and blogs!
http://www.feedfeeds.com

Roedy Green

unread,
Sep 29, 2005, 1:50:47 AM9/29/05
to
On 28 Sep 2005 21:20:06 -0700, Pipe...@hotmail.com wrote or quoted :

>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.

Pipe...@hotmail.com

unread,
Sep 29, 2005, 3:34:43 AM9/29/05
to
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. What am I doing wrong?

Ingo R. Homann

unread,
Sep 29, 2005, 3:43:25 AM9/29/05
to
Hi,

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

Pipe...@hotmail.com

unread,
Sep 29, 2005, 4:22:39 AM9/29/05
to
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();

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());
}

}
-------------------------------------------------------------------------

Ingo R. Homann

unread,
Sep 29, 2005, 4:25:55 AM9/29/05
to
Hi,

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

Roedy Green

unread,
Sep 29, 2005, 4:50:14 AM9/29/05
to
On 29 Sep 2005 00:34:43 -0700, Pipe...@hotmail.com wrote or quoted :

>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.

Pipe...@hotmail.com

unread,
Sep 29, 2005, 5:53:26 AM9/29/05
to
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.

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?..

Andrew Thompson

unread,
Sep 29, 2005, 6:06:21 AM9/29/05
to
Pipe...@hotmail.com wrote:

> 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.

Hemal Pandya

unread,
Sep 29, 2005, 8:05:44 AM9/29/05
to

Pipe...@hotmail.com wrote:

>
> I want to find out in which directory abc.class lies.

Another method is to use
<classname>.class.getProtectionDomain().getCodeSource().getLocation().

0 new messages