我有以下代码
IJavaProject targetProject = null;
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
for (IProject project : root.getProjects()) {
if (project.getName().equals(projName)) {
try {
if (project.hasNature("org.eclipse.jdt.core.javanature")) {
targetProject = (IJavaProject) project;
}
} catch( ... ) {
// etc ...
}
我想要做的实际上是返回一个与特定名称匹配的项目作为 IJavaProject.如您所见,我通过以下方式检查以确保相关项目具有 java 特性:
What I am trying to do is essentially return a project that matches a particular name as an IJavaProject. As you can see, I check to ensure that the project in question has a java nature by calling:
if (project.hasNature("org.eclipse.jdt.core.javanature")) {
唉,我收到一个ClassCaseException"声明
Alas, I get a 'ClassCaseException' stating
java.lang.ClassCastException:
org.eclipse.core.internal.resources.Project cannot be cast to org.eclipse.jdt.core.IJavaProject
知道为什么吗?我会认为一旦 IProject 具有 Java 特性,它就可以转换为 IJavaProject.由于服务不可用,我目前无法访问 JDT Core API 这里.
Any idea why? I would have thought that once an IProject has a java nature, it can be cast to a IJavaProject. I can't get access to the JDT Core API at the moment as the service is unavailable here.
您答案中的代码不应该工作(错字?).以下是创建 IJavaProject 的方法:
The code in your answer shouldn't work (typo?). Here is how you can create an IJavaProject:
import org.eclipse.jdt.core.JavaCore
...
if (project.hasNature(JavaCore.NATURE_ID)) {
targetProject = JavaCore.create(project);
}
IProject
是 Eclipse 资源 API 中的一种类型,而 IJavaProject 是 Eclipse Java 模型中的一种类型.它们不是相同的抽象,但所有 IJavaProject 都有一个 IProject.
IProject
is a type in the Eclipse Resources API and IJavaProject is a type in the Eclipse Java Model. They are not the same abstractions, but all IJavaProjects have an IProject.
这篇关于无法将 eclipse 项目转换为 IJavaProject的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!