eclipse: Find Resource with a locationURI - eclipse

How can i find a Resource with a locationURI?
Path path = new Path('/home/foo/eclipse/runtime-EclipseApplication/someproj/B.txt');
IResource res = ResourcesPlugin.getWorkspace().getRoot().findMember(path);
I know there is a way by using the locationURI from current project to trim the "needless" part, but is there a "better" way to slove this issue.

You can use the following construction:
ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(URIUtil.toURI(fileName));
where fileName - absolute path of File.getAbsolutePath()
and URIUtil from org.eclipse.core.filesystem

You can use a FileLocator to find the file within your bundle, if that is what you want (instead of finding a file in the runtime workspace), see Retrieve a file from within a folder in the workspace

Related

How can I get the absolute path of the preference file for a particular plugin in eclipse plugin development?

I want to read the absolute path of the preference file for a particular plugin which is generally stored at:
Platform.getLocation() + "\.metadata\.plugins\org.eclipse.core.runtime\.settings\myPlugin.prefs"
Is there any way to get it directly somehow using eclipse provided Apis instead of hard-coding the 2nd part of the path?
This should give the correct location:
Bundle rt = Platform.getBundle(IPreferencesConstants.RUNTIME_NAME);
IPath path = Platform.getStateLocation(rt).append(".settings").append("myPlugin").addFileExtension("prefs");
The actual location calculation is in org.eclipse.core.internal.preferences.EclipsePreferences

Exporting an JAR file in Eclipse and referencing a file

I have a project with a image stored as a logo that I wish to use.
URL logoPath = new MainApplication().getClass().getClassLoader().getResource("img/logo.jpg");
Using that method I get the URL for the file and convert it to string. I then have to substring that by 5 to get rid of this output "file:/C:/Users/Stephen/git/ILLA/PoC/bin/img/logo.jpg"
However when I export this as a jar and run it I run into trouble. The URL now reads /ILLA.jar!/ and my image is just blank. I have a gut feeling that it's tripping me up so how do I fix this?
Cheers
You are almost there.
Images in a jar are treated as resources. You need to refer to them using the classpath
Just use getClass().getResource: something like:
getClass().getResource("/images/logo.jpg"));
where "images" is a package inside the jar file, with the path as above
see the leading / in the call - this will help accessing the path correctly (using absolute instead of relative). Just make sure the path is correct
Also see:
How to includes all images in jar file using eclipse
See here: Create a file object from a resource path to an image in a jar file
String imgName = "/resources/images/image.jpg";
InputStream in = getClass().getResourceAsStream(imgName);
ImageIcon img = new ImageIcon(ImageIO.read(in));
Note it looks like you need to use a stream for a resource inside an archive.

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?
try this:
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IResource res = root.findMember("/ProjectName/lib/something.jar");
System.out.println(res.getLocation().toString());
Paths in Eclipse are complicated, and there are a few other situations that you haven't mentioned, like classpath containers (JRE is an example), linked resources outside of the workspace and classpath variables.
To simplify, I suggest you use getResolvedClasspath instead, that returns only 'simple' classpath entires (no variables, no containers). According to its Javadoc, it returns absolute paths, and their interpretation depends on the kind of the entry:
CPE_LIBRARY - if it's an external library, it's a filesystem path (and it has no associated resource, meaning you can't find it with findMember). Otherwise, it's a path based on the workspace root
CPE_PROJECT - absolute path to the project
CPE_SOURCE - absolute path to the source folder
All absolute path are interpreted in the workspace. If you need the file-system path, you need to go through getLocation.
As a side-note, there is no 1-to-1 mapping between file-system entities and workspace resources. Because of workspace links, you may have several workspace paths pointing to the same (file-system) location.
This is the best I could come up with, and it totally is an ugly hack, yes:
private IPath getCorrectAbsolutePath(IJavaProject project, IPath path) throws IllegalArgumentException {
final String projectName = project.getProject().getName();
if (path.segmentCount() > 1 && path.segment(0).equals(projectName)) {
IPath projectAbsolutePath = project.getProject().getLocation();
IPath relativePath = path.removeFirstSegments(1);
return projectAbsolutePath.append(relativePath);
} else {
if (!path.isAbsolute())
path = path.makeAbsolute();
if (!path.isAbsolute())
throw new IllegalArgument("Cannot make IPath absolute: " + path.toString());
return path;
}
}
I have a suggestion, look picture attachment, find "location" property from properties of any plugin of eclipse , maybe can find it where is from. but sorry I have no 10 reputation, you can see picture from picture url

Eclipse Plugin: Obtaining an IFile from String

I am doing some serialization which also contains an IFile path that needs to be stored as string.
I am using this IFile in a plugin project. For debugging or running Eclipse starts a new workspace. This testing-workspace has its root somewhere relatively to the plugin folder. My problem is, when I turn my IFile to an absolute path, my Eclipse testing-workspace considers the file as outside the workspace and throws exceptions.
If I use the project relative path, the IFile creation from string fails and IFile is null.
I truly want to believe that it works the way I need it, but I really would like to see it. Is there a way to reconstruct a valid IFile from a project relative path?
Currently, I am doing the reconstruction from String->IFile like this:
//name is a string with the absolute path
IPath location = new Path(name);
IFile file = project.getFile(location.lastSegment());
But, like already mentioned, works only with an absolute path, which doesn't work in the eclipse testing-workspace.
Thanks for a hint
You are very close. You want:
IPath path = new Path(name);
IFile file = project.getFile(path);
The name should be an absolute (to the workspace) or project-relative path. This interface is defined in IContainer.getFile(IPath). I changed the variable from "location" because location is usually meant as the actual (local) OS file path. To get the path use:
IPath path = file.getProjectRelativePath();

ResourceException when creating IMarker on IFile (linked resource)

I have some problems updating an "old" Eclipse plugin. Here is what I would like to do and what the original plugin did:
(parse compiler output on console with file name and error information --> still works)
--> set link to the location within the file
--> set marker to location in the file
What I did in the past was to get the IFile from the path String of the file and generated link and marker from it:
IFile ifile;
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath path = new Path(fileName);
IFiles[] files = workspace.getRoot().findFilesForLocation(path);
...
ifile = iFiles[0];
Map attributes = new HashMap();
attributes.put(IMarker.SEVERITY, new Integer (severity));
MarkerUtilities.setLineNumber(attributes, lineNumber);
MarkerUtilities.setMessage(attributes, message);
MarkerUtilities.createMarker(ifile, attributes,
IMarker
Since findFilesForLocation is deprecated, I tried to find another way but I am not succeeding whatsoever. Using the changed code to get the IFile always results in a exception: org.eclipse.core.internal.resources.ResourceException: Resource '/path/to/file.c' does not exist.
Is it possible that this relates to the fact that the source file is only linked into the project, and not physically within the project?
IWorkspace workspace = ResourcesPlugin.getWorkspace();
IPath location = new Path(fileName);
IFile ifile = workspace.getRoot().getFile(location);
Can anyone help?
Thank you!
I am guessing that fileName is the fully qualified path to the file you want to get. I'm also guessing that the file that you are looking for is already in the workspace, even if it is linked (if not, then this won't work. You will first need to add the file to a project before getting the IFile for it).
You need to do something like this:
IFiles[] files = workspace.getRoot().findFilesForLocationURI("file:" + fileName);
Then this will find all files in the workspace that correspond to the file in the file system.
The reason why your attempt above is giving you a ResourceException is that you are trying to pass in a file system path to get an IFile object from the workspace. The Eclipse workspace is an abstraction over the underlying filesystem and cannot directly work with absolute paths.
For the Resources APIs, Paths usually means a path in the workspace and Location usually refers to a place in the filesystem or outside the workspace. If you already have a workspace path to start with, just ask the IWorkspaceRoot for the IFile and get on with what you're doing.