wood burning stoves 2.0*
The moose likes I/O and Streams and the fly likes getAbsolutePath() doesn't return full path? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "getAbsolutePath() doesn Watch "getAbsolutePath() doesn New topic
Author

getAbsolutePath() doesn't return full path?

Peter Schueler
Greenhorn

Joined: Nov 20, 2011
Posts: 4
Hi All
I'm stuck with a problem to get the path to my program or code at runtime.
My structure within the IDE workplace is
workplace/
---classes/
-------com/
-----------domain/
---------------package/ and then the classes.
---resources/
-------images/ and then images
-------DerbyDB/ and its files

The program runs well when started from my IDE (JCreator) with


But when I try to run the program from cmd tool in a structure like this:
justanydirectoryname/
---bin/
-------com/
-----------domain/
---------------package/ and then the classes.
---resources/
-------images/ and then images
-------DerbyDB/ and its files
the above mentioned code returns
"justanydirectoryname/resources"
and, naturally, wouldn't find the database because that would be
"justanydirectoryname/bin/resources"

Does anybody know the reason for this behaviour or, even better, how I could run the program from within the IDE and from the command line without having to switch the code?

Any help is appreciated.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

new File("") does not give the directory where your class file is or anything related to it. It gives whatever the "current directory" is in the context of when the JVM was started. When running from within an IDE, that's dependent on the IDE itself and on how it was started.

You cannot, in general, get the file system location where your class file is. This is because it is not, in general, on the file system.

If you want to find something relative to your classes, use Class.getResource()/getResourceAsStream(), or ClassLoader.findResource()
Peter Schueler
Greenhorn

Joined: Nov 20, 2011
Posts: 4
Thank you for your input, I'll try with that.
Still I don't understand why the class isn't on the file system. What am I missing?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Peter Schueler wrote:Thank you for your input, I'll try with that.
Still I don't understand why the class isn't on the file system. What am I missing?


In most cases it is on the file system. It's just that at the layer where ClassLoaders operate, they don't look at it that way, and they don't assume it's on the file system. The most commonly used ClassLoaders are URLClassLoaders, which load from a URL. Even though in many cases--probably most cases--that URL indicates something on the local file system, the ClassLoader doesn't treat it that way. In this manner, it can load classes from any URL, such as the way an applet's classes are loaded from URLs on the applet's web server.

Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18167
    
    8

If you're doing this because you want to get a resource which is in the same directory as some class, then you can use the getResource() method of Class. If the code is in that class, then it would be like this:

This returns you a URL pointing at a file named "resource.jpg" which is in the same directory as the class which "this" belongs to.

Don't make the common mistake of trying to use that URL as a file name, because it isn't. It's a URL. And if you just need an InputStream over that resource, use the getResourceAsStream method.

And as for your original code, that business of concatenating strings along with File.separator is pretty clumsy. There's a "new File(directory, filename)" constructor you can use for that instead.
Peter Schueler
Greenhorn

Joined: Nov 20, 2011
Posts: 4
Thanks again and very much. I solved my initial problem with:


However, I still have a question (and I hope it is not too silly).
I understand that I could load the image directly by using ImageIO.read(url). But I needed the path to access the database; the connection needs a string of "jdbc:derby:database" and wouldn't find anything with an url (no surprise).

I couldn't use the path I was getting by url.getPath(), never mind how the "/" at beginning or end of that path were set.
Does anybody know why that is?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Peter Schueler wrote:
I couldn't use the path I was getting by url.getPath(), never mind how the "/" at beginning or end of that path were set.
Does anybody know why that is?


Because a URL's path doesn't necessarily have any association to any path on any file system. And if it does have such an association, there's no way to tell where it's rooted--that is, what it's relative to.

If you really need a file on the file system, you're going to have to forget about finding it relative to some class or in your classpath and just pass a full path as an arg at startup, or have a user select it, etc.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19557
    
  16

Peter Schueler wrote:

Shouldn't that be With the extra getClass(), you would get the resource relative to java.lang.Class, as that's what MyClass.class.getClass() returns.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Peter Schueler
Greenhorn

Joined: Nov 20, 2011
Posts: 4
If I leave out the "getClass" I don't get anything, no Url, that is.
What I get now is the path to the folder "resources" as a String.
My point here is that I need the absolute path without knowing where the user put the program. It might be in the file system of a Mac (and then it will be in an .app bundle) or on a windows system (practically anywhere within a given folder) or even on a usb stick (again, in the folder containing bin and resources). The only thing I know is where the resources are relative to the original class, meaning the file "myapp.class".

I also don't understand why the only way to find a file once your programm has been stored somewhere would be to search or ask the user - there are portable programs which you only put somewhere and they run, including their resources and all, without the user having to point out where the program files are.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: getAbsolutePath() doesn't return full path?
 
Similar Threads
File Input/Output
Loading Images
Unable to access a file in my Jar file.
Canonical vs Absolute
A pain in the ......Please help?