finding container directory of an executing class?

P

Piper707

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
 
P

Piper707

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

Ingo R. Homann

Hi,

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
 
P

Piper707

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

}
-------------------------------------------------------------------------
 
I

Ingo R. Homann

Hi,

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
 
R

Roedy Green

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

Piper707

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

Andrew Thompson

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top