Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7777121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T18:03:28+00:00 2026-06-01T18:03:28+00:00

I have a problem regarding java.lang.NoSuchMethodError. This program is about Compiler API (JSR 199).

  • 0

I have a problem regarding java.lang.NoSuchMethodError. This program is about Compiler API (JSR 199). When I create a prototype for this it run work, but when I try to make it to become library it throw NoSuchMethodError Exception.

Here is the First Prototype:

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;
    }
}

I changed the DynaCompTest become DynamicCompiler for the library:

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();
    }
}

And I created Second Prototype to test the library:

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());
    }
}

When I test with code above it run well and print BUILD SUCCESSFUL but when I tried to make it error for example I deleted the semicolon ; like the first prototype, it throw the NoSuchMethodError Exception when access the compiler.getDiagnostics().getDiagnostics() inside the looping.

The question is, why in the First Prototype it run well when try to make an error but when I tried with my own library it become Exception?

Edit

Here is the 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

It should be like this:

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

When trying to debug it, it shown an error:

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

Here is the error message:

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

Update

Got the problem, this problem will occur if we add the whole project instead build the jar of the library. So when I build the library jar it works. But anyone can explain why this thing happen when I try add the whole project instead the jar file?

Note

I’m using:

  • JDK 1.7 from Oracle
  • Netbeans 7.1.1
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T18:03:30+00:00Added an answer on June 1, 2026 at 6:03 pm

    After I tried with Eclipse Indigo, I found that it works when add the Project or add the jar file. While in Netbeans 7.1.1 will get an error if add the Project, but works if add the jar file.

    Maybe it one of the bugs of Netbeans…

    Thank you for your attention…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a problem regarding running multiple threads executing a pipeline in Java. Say
I wanted some tips regarding the following problem, from the Java experts in this
I have a problem regarding StartUp Url in WPF. I have a LoginView.xaml and
I have a problem regarding the datediff MYSQL function, I can use it and
I have a problem regarding the ksoap2. My problem is that the project used
I have a problem regarding Android App. I have created an application that download
Description: I have a problem regarding DataGridView . I need to show a Client_Name
I have the following code, and i have a problem regarding changing the ringtone
Good day everyone! I have a problem regarding my date. It needs to be
I am new to jQueryMobile. I have one problem regarding fixed headers. I have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.