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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T15:52:58+00:00 2026-06-13T15:52:58+00:00

I currently have this code: private void compile(){ List<File> files = getListOfJavaFiles(); //JavaCompiler compiler

  • 0

I currently have this code:

private void compile(){ 

        List<File> files = getListOfJavaFiles();

        //JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        //compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");

        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
           StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);

           Iterable<? extends JavaFileObject> compilationUnits1 =
               fileManager.getJavaFileObjectsFromFiles(files);

           List<String> optionList = new ArrayList<String>();
           // set compiler's classpath to be same as the runtime's
           optionList.addAll(Arrays.asList("-classpath",System.getProperty("java.class.path")));

           //need to add options here.
           compiler.getTask(null, fileManager, null, optionList, null, compilationUnits1).call();

           //compiler.run(null, null, null, srcDirectory.getPath()+"/Main.java");
          // fileManager.close();

    }

But I am stuck now trying to make this actually run the files which have been compiled.
I see no output from this in the console, however in the Main.java file which I have compiled successfully (I can see the .class files), I have put “System.out.println(“Main class is running”);, so I would expect to see this when I run the application.

  • 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-13T15:52:59+00:00Added an answer on June 13, 2026 at 3:52 pm

    You can create an URLClassLoader to load your newly compiled classes, or you can have a look at a library I wrote which will compile in memory and load into the current class loader by default.

    http://vanillajava.blogspot.co.uk/2010/11/more-uses-for-dynamic-code-in-java.html

    If you have generated code, it will save the file to a source directory when debugging so you can step into the generated code (otherwise it does everything in memory)

    You can only load a class once this way so if you need to load many versions I suggest you implement an interface and change the name of the class each time.

    // this writes the file to disk only when debugging is enabled.
    CachedCompiler cc = CompilerUtils.DEBUGGING ?
            new CachedCompiler(new File(parent, "src/test/java"), new File(parent, "target/compiled")) :
            CompilerUtils.CACHED_COMPILER;
    
    String text = "generated test " + new Date();
    Class fooBarTeeClass = cc.loadFromJava("eg.FooBarTee", "package eg;\n" +
        '\n' +
        "import eg.components.BarImpl;\n" +
        "import eg.components.TeeImpl;\n" +
        "import eg.components.Foo;\n" +
        '\n' +
        "public class FooBarTee{\n" +
        "    public final String name;\n" +
        "    public final TeeImpl tee;\n" +
        "    public final BarImpl bar;\n" +
        "    public final BarImpl copy;\n" +
        "    public final Foo foo;\n" +
        '\n' +
        "    public FooBarTee(String name) {\n" +
        "        // when viewing this file, ensure it is synchronised with the copy on disk.\n" +
        "        System.out.println(\"" + text + "\");\n" +
        "        this.name = name;\n" +
        '\n' +
        "        tee = new TeeImpl(\"test\");\n" +
        '\n' +
        "        bar = new BarImpl(tee, 55);\n" +
        '\n' +
        "        copy = new BarImpl(tee, 555);\n" +
        '\n' +
        "        // you should see the current date here after synchronisation.\n" +
        "        foo = new Foo(bar, copy, \"" + text + "\", 5);\n" +
        "    }\n" +
        '\n' +
        "    public void start() {\n" +
        "    }\n" +
        '\n' +
        "    public void stop() {\n" +
        "    }\n" +
        '\n' +
        "    public void close() {\n" +
        "        stop();\n" +
        '\n' +
        "    }\n" +
        "}\n");
    
    // add a debug break point here and step into this method.
    FooBarTee fooBarTee = new FooBarTee("test foo bar tee");
    Foo foo = fooBarTee.foo;
    assertNotNull(foo);
    assertEquals(text, foo.s);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have this type of code: private void FillObject(Object MainObject, Foo Arg1, Bar
I currently have this code : Private Sub Worksheet_Change(ByVal Target As Range) Dim lastrow
I currently have this code Private Sub Worksheet_Change(ByVal Target As Range) WorksheetChanged(Target, Range(AB3).CurrentRegion, Range(B18:B19))
I have this code so far: private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
I currently have this code which stores XML into an XML-type column called data,
I currently have this code: PACKETS = {}; function AddPacket(data) local id = data.ID;
I currently have this code which reads the first field in a database record
Ok I currently have this code public int i = 0; //this is outside
Hello I currently have this code checking if a cookie exists, it works all
Currently I have this code: <?php echo '<meta name=robots content=noindex>'; $arr = json_decode(file_get_contents(http://media1.clubpenguin.com/play/en/web_service/game_configs/ paper_items.json),true);

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.