Login  Register

Re: Java

Posted by dscho on Jul 02, 2012; 2:09pm
URL: http://imagej.273.s1.nabble.com/Java-tp4999262p4999281.html

Hi David,

On Sun, 1 Jul 2012, David Webster wrote:

> On Sun, Jul 1, 2012 at 1:20 PM, Johannes Schindelin <
> [hidden email]> wrote:
>
> > On Sat, 30 Jun 2012, David Webster wrote:
> >
> > > I don't know if this would help as I don't recall seeing .config
> > > files in the same dir as my class file (e.g. the plugins folder).
> >
> > You can call getResource() with the .class file itself as parameter.
>
> I know I'm being terribly clueless, but what class does getResources()
> belong to?

Example: let's assume you have a class called MyClass and its .class file
is outside a .jar file. Then

        String path = MyClass.class.getResource("MyClass.class").getPath();
        File directory = new File(path).getParentFile();

will give you the directory in which MyClass resides, as a java.io.File
object. If the class resides inside a .jar file and you want the directory
in which the .jar file lives, it gets slightly more complicated:

        String path = MyClass.class.getResource("MyClass.class").getPath();
        if (path.startsWith("jar:")) path = path.subtring(4);
        if (path.startsWith("file:")) path = path.subtring(5);
        int bang = path.indexOf("!/");
        if (bang > 0) path = path.substring(0, bang);
        directory = new File(path).getParentFile();

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html