Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eclipse PDE: given a relative path like /ProjectName/lib/something.jar, how do you get a full filesystem path?

I'm trying to find a path to a jar file that's in the raw classpath. getRawClasspath returns a collection of IClasspathEntry objects. I can call getPath on those.

But getPath returns something weird: an IPath that starts with the project name, like:

/ProjectName/lib/something.jar

What's the right way to turn this relative path into a full-qualified OS path? At first I thought I could just add the path to the workspace root, but that doesn't work since there are often intermediate directories between the workspace and the project.

And more generally, how do I know what to do with an IPath returned by a method? It seems like I never know what that IPath is; relative to the project, relative to the workspace, relative to the project but with the project name as the first element, relative to the phase of the moon... It's all baffling, and the documentation is never helpful - or at least I don't know where to look.

UPDATE

I'm even more confused now. The problem still is that, when you have an IClasspathEntry, it's still unclear to me how to resolve it to a filesystem path.

The answer that says "if a path starts with a / it's an absolute path (relative to the workspace) isn't correct. The problem is that the getPath method on an IClasspath returns one of two things: a path starting with a slash that's relative to the workspace, or an IPath starting with a / that's an actual filesystem path. Yes, two completely different things are shoved into one type. You get the filesystem variant when the jar is outside the workspace, and you get the "absolute" variant when it's in the workspace.

I think part of the answer is that an IPath, by itself, is only a fancy string. You have to know where it came from to make sense out of it. It doesn't carry the right sorts of information to be useful on its own.

So what's the right way to deal with this?

like image 259
James Moore Avatar asked Oct 08 '22 19:10

James Moore


1 Answers

try this:

    IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
    IResource res = root.findMember("/ProjectName/lib/something.jar");
    System.out.println(res.getLocation().toString());
like image 114
Dollyn Avatar answered Oct 12 '22 11:10

Dollyn