Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / September 2005

Tip: Looking for answers? Try searching our database.

finding container directory of an executing class?

Thread view: 
Piper707@hotmail.com - 29 Sep 2005 05:20 GMT
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 - 29 Sep 2005 05:26 GMT
object.getClass().getResource("yourclassname").getPath();

------------
FeedFeeds : A new way to read news and blogs!
http://www.feedfeeds.com
Roedy Green - 29 Sep 2005 06:50 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Piper707@hotmail.com - 29 Sep 2005 08:34 GMT
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?

> >How do I find the container directory for a particular file when it
> >gets executed?
[quoted text clipped - 7 lines]
> Canadian Mind Products, Roedy Green.
> http://mindprod.com Again taking new Java programming contracts.
Ingo R. Homann - 29 Sep 2005 08:43 GMT
Hi,

> Hi thx for the replies.
>
[quoted text clipped - 6 lines]
>
> 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
Piper707@hotmail.com - 29 Sep 2005 09:22 GMT
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());
}

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

> Hi,
>
[quoted text clipped - 18 lines]
> Ciao,
> Ingo
Ingo R. Homann - 29 Sep 2005 09:25 GMT
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
Piper707@hotmail.com - 29 Sep 2005 10:53 GMT
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?..

> Hi,
>
[quoted text clipped - 15 lines]
> Ciao,
> Ingo
Andrew Thompson - 29 Sep 2005 11:06 GMT
> Thanks Ingo. That fixed my problem. Finally have it working!! Never
> thought of explicitly naming the file!
[quoted text clipped - 7 lines]
> 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.
Roedy Green - 29 Sep 2005 09:50 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Hemal  Pandya - 29 Sep 2005 13:05 GMT
> I want to find out in which directory abc.class lies.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.