![](/img/trans.png)
[英]When developing eclipse plugin, can I get absolute path of jar files using “Add Jars”?
[英]How to get the absolute path of project files in eclipse using plugin
我正在尝试创建一个插件,它会给我一个在 eclipse 中打开的项目中所有文件的绝对路径列表。
我试过了,但我只能获得活动窗口的路径..
我的操作代码是:
IWorkbenchPart workbenchPart = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActivePart();
IFile file = (IFile) workbenchPart.getSite().getPage().getActiveEditor().getEditorInput().getAdapter(IFile.class);
if (file == null)
try {
throw new FileNotFoundException();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String path = file.getRawLocation().toOSString();
System.out.println("path: " + path);
在这里,我只获取活动窗口的路径..但我想要项目中所有文件的绝对路径列表..主要是 src 文件夹下的文件...
请指导我,如果我可以用同样的方式来做,或者我需要为此使用一些不同的 API。
经过我的研究,我发现下面的代码将获取 Eclipse 当前工作区的项目目录的路径:
//get object which represents the workspace
IWorkspace workspace = ResourcesPlugin.getWorkspace();
//get location of workspace (java.io.File)
File workspaceDirectory = workspace.getRoot().getLocation().toFile()
注意:您需要导入org.eclipse.core.resources
和org.eclipse.core.runtime
才能使用这些 API
给定一个IResource
,您可以使用org.eclipse.core.resources.IResource.getLocation()
方法,该方法返回一个带有“本地文件系统中该资源的绝对路径的IPath
,如果无法确定路径,则返回 null”。 然后,您可以使用返回字符串表示形式的org.eclipse.core.runtime.IPath.toOSString()
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.