I m trying to create a virtual folder inside a folder in eclipse. I want to link some files to this virtual folder but I m not able to link.
Below is the sample code:
IFolder folder = modelFolder.getFolder("Script_include");
folder.create(IResource.VIRTUAL, false, null);
IPath newLocation = new Path("/Users/dinesh.lodhi");
folder.createLink(newLocation, IResource.NONE, null);
What I exactly want is to create some virtual folders inside one of the folder at the startup of my plugin and want to link some of resources into it.
The given path must be either an absolute file system path, or a relative path whose first segment is the name of a workspace path variable.
I was mistakenly using folder.getFullPath() for getting location.
and one more thing there is no need to
folder.create(IResource.VIRTUAL, false, null);
as createLink() method itself Creates a new file/folder resource as a member of this handle's parent resource.
Thanks
Related
In my Scala code I want to create a folder "c:/temp" and then create a file "file.txt" within that folder. I don't want to have to use "c:/temp/file.txt". So, I want to use the relative path of the file to create it within that folder.
Imagine how a human creates a folder and then a file? He creates a folder; goes in the folder, and then creates the file inside that folder. That's what I want to do.
=====
Added the following to make this more clear:
Let's say I created the folder and I have a File object called myFolder that represents that folder. What I want is to be able to do something like myFolder.createFile("file.txt").
val subFile = new File(myFolder, "file.txt")
From the description of the File(File parent, String child) constructor found at the docs page:
Creates a new File instance from a parent abstract pathname and a child pathname string.
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
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();
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.
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