【问题标题】:Using Eclipse's JDT, how do I identify the classpath for a project?使用 Eclipse 的 JDT,我如何识别项目的类路径?
【发布时间】:2019-08-20 17:24:19
【问题描述】:

我正在编写一个插件,它将为在 Eclipse 的 Project Explorer 中选择的 Java 类生成单元测试。该插件使用名为 Randoop 的第三方程序来生成测试,因此我使用 ProcessBuilder 来实现这一点:

ProcessBuilder builder = new ProcessBuilder(command);

传递给 ProcessBuilder 的命令是字符串列表,类似于

["java", "-classpath", "path1;path2;etc", "randoop.main.Main", ...]

在插件中,我试图根据 Eclipse 知道的类路径为 Randoop 生成类路径。以下是我目前掌握的一些内容:

IClasspathEntry[] resolvedClasspath = javaProject.getResolvedClasspath(true);

for (IClasspathEntry entry : resolvedClasspath) {
    if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) {
        IPath outputLocation = entry.getOutputLocation();

        if (outputLocation != null) {
            buf.append(outputLocation.toString());
        }
        else {
            buf.append(entry.getPath().toString());
        }
    }
    else {
        buf.append(entry.getPath().toString());
    }
    buf.append(CLASSPATH_SEP);
}

这不太对。似乎可以指定库 jar 文件,但在识别与 CPE_SOURCE 条目对应的类文件的路径方面做得不好。例如,我看到 /myPkgFragRoot/src/main/java 的类路径条目而不是 myPkgFragRoot/target/classes

我似乎对 Eclipse 处理类路径的方式一头雾水,因此我正在寻求帮助。首先,我想知道我的高级方法是否错误。似乎我正在编写大量代码来生成不正确的类路径。从IJavaProject 获取类路径是否有比获取getResolvedClasspath 的结果并遍历它们并操作各个条目更简单的方法?其次,如果没有更简单的方法,我应该如何定位构建项目产生的类文件?

【问题讨论】:

    标签: eclipse eclipse-plugin eclipse-jdt


    【解决方案1】:

    如果outputLocationnull,则必须使用默认输出位置javaProject.getOutputLocation() 而不是entry.getPath()

    Javadoc of IClasspathEntry.getOutputLocation():

    返回:

    完整路径 [...],如果使用默认输出文件夹,则为 null

    如果在 Project > Properties: Java Build Path 选项卡 Source 中未选中 Allow output folder for source folder 复选框,@987654328 @ 将始终返回 null

    【讨论】:

      猜你喜欢
      • 2012-07-13
      • 2010-09-10
      • 2013-11-30
      • 1970-01-01
      • 2017-08-19
      • 1970-01-01
      • 1970-01-01
      • 2015-02-21
      • 2012-03-17
      相关资源
      最近更新 更多