The Artima Developer Community
Sponsored Link

Java Answers Forum
path of a class

4 replies on 1 page. Most recent reply: Sep 14, 2005 10:27 PM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

path of a class Posted: Sep 6, 2005 3:44 AM
Reply to this message Reply
Advertisement
Is there a way I can geet the directory where a class file lies?
Something like getAbsoluteFilePath(class)?

I need to read a file wich lies in the same dir as the class.
The program must be callable from any directory, so currentDir or something similar does not work.


Amol Brid

Posts: 43
Nickname: amolbrid
Registered: Feb, 2004

Re: path of a class Posted: Sep 6, 2005 4:03 AM
Reply to this message Reply
Is this ur looking for?

public String getAbsoluteFilePath(String className) {
	String currentDir = System.getProperty("user.dir");
	String pathSep = System.getProperty("file.separator");
	return currentDir + pathSep + className.replace('.', pathSep.charAt(0));
}



Regards,
Amol Brid

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: path of a class Posted: Sep 6, 2005 5:19 AM
Reply to this message Reply
Not really.

Maybe I didn't explain it good enough.

Assume that the classes or the jar file lies in c:\myprogram\

In this dir I have the run file called run.cmd wich basically launches the java program.

Opening a command shell I change dir to c:\runtest\

Here I type "c:\myprogram\run.cmd"

The result of the getProgramPath method should be "c:\myprogram\", but it is "c:\runtest", since I did not change directory.

The reason for all this is that I want to read some .ini settings from a file stored in the same dir as the jar file, but don't want to change dir.

Jeff Thomson

Posts: 14
Nickname: thomson
Registered: Sep, 2005

Re: path of a class Posted: Sep 14, 2005 5:35 AM
Reply to this message Reply
If the directory is already in classpath you can get the resource(ini file) directly by using getResource(resourceName) method of ClassLoader. This will return the URL of the resource from which you can open a stream for doing your operations.

----------------
Jeff Thomson
http://www.devsquare.com
Online Application Development

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: path of a class Posted: Sep 14, 2005 10:27 PM
Reply to this message Reply
Thanks, sounds promising. I'll try that.

Flat View: This topic has 4 replies on 1 page
Topic: need help on exercise Previous Topic   Next Topic Topic: unexpected error, hotspot VM!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use