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 3393224
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T04:01:31+00:00 2026-05-18T04:01:31+00:00

In the following example, I’m trying to use sun.tools.javac.Main to dynamically compile a class

  • 0

In the following example, I’m trying to use sun.tools.javac.Main to dynamically compile a class I generate, then instantiate an object of that class and invoke a method. So far, I can’t even get passed loading the generated class. I get the following exception in Eclipse:

java.lang.ClassNotFoundException: TestHello_1289950330167
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at MyClassGenerator.runIt(MyClassGenerator.java:47)
    at MyClassGenerator.main(MyClassGenerator.java:13)
Note: sun.tools.javac.Main has been deprecated.
1 warning
Running TestHello_1289950330167:

Here’s the code:

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileWriter;
import java.net.URL;
import java.net.URLClassLoader;

public class MyClassGenerator {
    String generatedClassName = "TestHello_" + System.currentTimeMillis();
    String javaFileName = this.generatedClassName + ".java";

    public static void main(final String args[]) {
        final MyClassGenerator mtc = new MyClassGenerator();
        mtc.createIt();
        if (mtc.compileIt()) {
            System.out.println("Running " + mtc.generatedClassName + ":\n\n");
            mtc.runIt();
        }
        else {
            System.out.println(mtc.javaFileName + " is bad.");
        }
    }

    public void loadIt() {

        final ClassLoader classLoader = MyClassGenerator.class.getClassLoader();

        try {
            final Class aClass = classLoader.loadClass(this.generatedClassName);
            System.out.println("Loaded " + aClass.getName());
        }
        catch (final ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    public void createIt() {
        try {
            final FileWriter aWriter = new FileWriter(this.javaFileName, true);
            aWriter.write("public class " + this.generatedClassName + " { }");
            aWriter.flush();
            aWriter.close();
        }
        catch (final Exception e) {
            e.printStackTrace();
        }
    }

    public boolean compileIt() {
        final String[] source = { new String(this.javaFileName) };
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();

        new sun.tools.javac.Main(baos, source[0]).compile(source);

        System.out.print(baos.toString());

        return (baos.toString().indexOf("error") == -1);
    }

    public void runIt() {
        try {
            final File file = new File(this.javaFileName);
            final URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { file.toURI().toURL() });
            final Class<?> cls = Class.forName(this.generatedClassName, true, classLoader);
        }
        catch (final Exception e) {
            e.printStackTrace();
        }
    }
}
  • 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-05-18T04:01:32+00:00Added an answer on May 18, 2026 at 4:01 am

    Because it’s not in the classpath. Either write it to the classpath (or add its root path to the classpath) or use URLClassLoader.

    File root = new File(".");
    URLClassLoader classLoader = URLClassLoader.newInstance(new URL[] { root.toURI().toURL() });
    Class<?> cls = Class.forName(generatedClassName, true, classLoader);
    

    Using relative paths in java.io is by the way a bad idea. You’re dependent on the current working directory which is not controllable from in the code.

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

Sidebar

Related Questions

No related questions found

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.