Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Main class directory



The file path and class path are two distinct concepts. Keep in mind what
you are trying to do will fail if your classes are in a jar file; the file
system doesn't recognize a jar as a "file system" - it's just a single file,
so it can't look inside to find individual files.

The way to do what you want is to use getResource() method of Class. Ie,

URL someObject.getClass().getResource(com.myPacackageList.foo.myFile);

then you can call openStream() on the URL to get an InputStream to your
"file".

Or you could also just call getResourceAsStream() of Class, and get an
InputStream directly.

But the key here is you won't be able to work with a File object, you'll
have an InputStream, but that is something you can work with.

Again, if your file is part of a jar archive, it's not accessible by any
file-system calls.

Rob

> -----Original Message-----
> From: Eric VERGNAUD [mailto:email@hidden]
> Sent: Sunday, August 31, 2003 8:06 AM
> To: email@hidden
> Subject: Main class directory
>
>
> Hi all,
>
> This is not a mac specific question.
>
> I'm porting my app developed using CW8 on MacOSX to Windows using Sun
> ONE
> Studio 5.
>
> Everything works fine, but the following: when debugging, the IDE sets
> the
> working directory to java.home rather than the main package directory.
> This
> prevents some of my classes from finding files that reside beside the
> jar
> package. I'm loading these files using the following:
>
> String path = System.getProperty("user.dir") + "/MyFile.ext";
>
> I'm trying to find a way to retrieve the main class package directory,
> something like:
>
> String path = System.getProperty("mainclass.dir") + "/MyFile.ext";
>
> Or
>
> String path = MyMainObject.getClass().getPackage().getPath();
>
> But I cannot find it. Anyone knows how to do this ?
>
> Thanks,
>
> Eric
> _______________________________________________
> java-dev mailing list | email@hidden
> Help/Unsubscribe/Archives:
> http://www.lists.apple.com/mailman/listinfo/java-dev
> Be sure to read the FAQ http://developer.apple.com/java/faq/ before
> posting
> Do not post admin requests to the list. They will be ignored.
_______________________________________________
java-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/java-dev
Be sure to read the FAQ http://developer.apple.com/java/faq/ before posting
Do not post admin requests to the list. They will be ignored.

References: 
 >Main class directory (From: Eric VERGNAUD <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.