Cannot cast eclipse project to IJavaProject(无法将 eclipse 项目转换为 IJavaProject)
问题描述
我有以下代码
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法将 eclipse 项目转换为 IJavaProject
基础教程推荐
- 降序排序:Java Map 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01