访问自定义库时出现NoSuchMetodError异常

时间:2012-04-13 02:02:20

标签: java exception nosuchmethoderror java-compiler-api jsr199

我有关于java.lang.NoSuchMethodError的问题。该程序与Compiler API(JSR 199)有关。当我为此创建一个原型时,它会运行,但当我尝试使它成为库时,它会抛出NoSuchMethodError异常。

这是第一个原型:

public class DynaCompTest {

    public static void main(String[] args) {
        String fullName = "HelloWorld";

        StringBuilder sourceCode = new StringBuilder();
        sourceCode.append("public class HelloWorld {\n")
            .append("\tpublic static void main(String[] args) {\n")
            .append("\t\tSystem.out.println(\"Hello World\")\n")
            .append("\t}\n")
            .append("}");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));

        DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<>();
        List<JavaFileObject> jFiles = new ArrayList<>();
        jFiles.add(new CharSequenceJavaFileObject(fullName, sourceCode));

        compiler.getTask(null, fileManager, diagnostics, null, null, jFiles).call();

        for (Diagnostic diagnostic : diagnostics.getDiagnostics()) {
            System.out.format("Error on line %d in %s\n", diagnostic.getLineNumber(), diagnostic);
        }
    }
}

public class CharSequenceJavaFileObject extends SimpleJavaFileObject {

    private CharSequence content;

    public CharSequenceJavaFileObject(String className, CharSequence content) {
        super(URI.create("string:///" + className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
        this.content = content;
    }

    @Override
    public CharSequence getCharContent(boolean ignoreEncodingErrors) {
        return content;
    }

}

public class ClassFileManager extends ForwardingJavaFileManager {

    private JavaClassObject jClassObject;

    public ClassFileManager(StandardJavaFileManager standardManager) {
        super(standardManager);
    }

    @Override
    public ClassLoader getClassLoader(Location location) {
        return new SecureClassLoader() {
            @Override
            protected Class<?> findClass(String name) throws ClassNotFoundException {
                byte[] b = jClassObject.getBytes();
                return super.defineClass(name, jClassObject.getBytes(), 0, b.length);
            }
        };
    }

    @Override
    public JavaFileObject getJavaFileForOutput(Location location, String className, Kind kind, FileObject sibling) throws IOException {
        jClassObject = new JavaClassObject(className, kind);
        return jClassObject;
    }
}

public class JavaClassObject extends SimpleJavaFileObject {

    protected final ByteArrayOutputStream bos = new ByteArrayOutputStream();

    public JavaClassObject(String name, Kind kind) {
        super(URI.create("string:///" + name.replace('.', '/') + kind.extension), kind);
    }

    public byte[] getBytes() {
        return bos.toByteArray();
    }

    @Override
    public OutputStream openOutputStream() {
        return bos;
    }
}

我将DynaCompTest更改为库的DynamicCompiler:

public class DynamicCompiler {

    private JavaCompiler compiler;
    private JavaFileManager fileManager;
    private List<JavaFileObject> jFiles;
    private DiagnosticCollector<JavaFileObject> diagnostics;

    public DiagnosticCollector<JavaFileObject> getDiagnostics() {
        return diagnostics;
    }

    public DynamicCompiler(String className, StringBuilder sourceCode) {
        compiler = ToolProvider.getSystemJavaCompiler();
        fileManager = new ClassFileManager(compiler.getStandardFileManager(null, null, null));

        diagnostics = new DiagnosticCollector<>();
        jFiles = new ArrayList<>();
        jFiles.add(new CharSequenceJavaFileObject(className, sourceCode));
    }

    public boolean doCompilation() {
        return compiler.getTask(null, fileManager, diagnostics, null, null, jFiles).call();
    }
}

我创建了第二个原型来测试库:

public class Compiler {

    private static StringBuilder sourceCode = new StringBuilder();

    public static void main(String[] args) {
        boolean status;
        sourceCode.append("public class HelloWorld {\n")
            .append("\tpublic static void main(String[] args) {\n")
            .append("\t\tSystem.out.println(\"Hello World\");\n")
            .append("\t}\n")
            .append("}");

        DynamicCompiler compiler = new DynamicCompiler("HelloWorld", sourceCode);

        status = compiler.doCompilation();

        StringBuilder messages = new StringBuilder();
        if (!status) {
            for (Diagnostic diagnostic : compiler.getDiagnostics().getDiagnostics()) {
                messages.append("Error on line ")
                    .append(diagnostic.getLineNumber())
                    .append(" in ")
                    .append(diagnostic)
                    .append("\n");
            }
        } else {
            messages.append("BUILD SUCCESSFUL ");
        }

        System.out.println(messages.toString());
    }
}

当我使用上面的代码进行测试时,它运行良好并打印BUILD SUCCESSFUL但是当我尝试将其设置为错误时,例如我删除了分号;,就像第一个原型一样,它会在访问时抛出NoSuchMethodError异常循环中的compiler.getDiagnostics().getDiagnostics()

问题是,为什么在第一个原型中它在尝试发生错误时运行良好但是当我尝试使用自己的库时它会变成异常?

修改

这是stacktrace:

/HelloWorld.java:3: error: ';' expected
    System.out.println("Hello World")
                                     ^
1 error
Exception in thread "main" java.lang.NoSuchMethodError: org.ert.lib.DynamicCompiler.getDiagnostics()Ljavax/tools/DiagnosticCollector;
at org.ert.exp.Compiler.main(Compiler.java:28)
Java Result: 1

应该是这样的:

Error on line 3 in /HelloWorld.java:3: error: ';' expected
    System.out.println("Hello World")
                                     ^

尝试调试时,显示错误:

public DiagnosticCollector<JavaFileObject> getDiagnostics() {
    return diagnostics; // Set Breakpoint here
}

以下是错误消息:

Not able to submit breakpoint LineBreakpoint DynamicCompiler.java : 25, reason: No executable location available at line 25 in class org.ert.lib.DynamicCompiler.
Invalid LineBreakpoint DynamicCompiler.java : 25

更新

遇到问题,如果我们添加整个项目而不是构建库的jar,就会出现这个问题。因此,当我构建库jar时,它可以工作。 但任何人都可以解释为什么当我尝试添加整个项目而不是jar文件时会发生这种情况?

注意

我正在使用:

  • 来自Oracle的JDK 1.7
  • Netbeans 7.1.1

2 个答案:

答案 0 :(得分:0)

似乎您在两个不同的库(jar)中存在类似的类。 例如

com.test.Example.class in a.jar
com.test.Example.class in b.jar

现在类加载器将加载第一个Example.class,看起来你需要b.jar中的类。然后它不会抛出异常,如NoMethodFound,但抛出NoSuchMethodFound的异常,因为类仍然存在于内存中但无法找到所需的方法。

可以通过更改库顺序来解决此类问题。您需要更高的所需库的订单。你可以从日食中做到这一点

Project Setting -> Java Build Path -> Order and Export.

希望这有用。

答案 1 :(得分:0)

在我尝试使用Eclipse Indigo之后,我发现在添加Project或添加jar文件时它可以正常工作。在Netbeans 7.1.1中,如果添加Project会出错,但是如果添加jar文件则会有效。

也许这是Netbeans的错误之一...

感谢您的关注......