title: Java JavaCompiler.run() compiling anonymous classes as well
body: <p>I am trying to load in text files on the fly and compile them.</p>
  
  <pre><code>File file = new File("Files/"+fileName+".java");
  JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
  compiler.run(null, null, errStream, file.getAbsolutePath());
  </code></pre>
  
  <p>I then will load the compiled .class files later:</p>
  
  <pre><code> public Class loadStrategyClass(File strategyClassFile) throws IOException
      {
          FileChannel roChannel = new RandomAccessFile(strategyClassFile, "r").getChannel();
          ByteBuffer buffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)roChannel.size());
  
          return defineClass(strategyClassFile.getName(), buffer, (ProtectionDomain)null);
      }
  </code></pre>
  
  <p>I am currently running into two issues:
  The first is if the .java files I load in contain anonymous classes. It doesn't appear that the JavaCompiler class will compile these.
  Exception in thread "main" java.lang.IllegalAccessException: Class Loader.ClassLoader can not access a member of class Files.myname.myclass$1 with modifiers ""</p>
  
  <p>The second:
  Is that sometimes I will get errors for NoClassDefFoundError:
  Exception in thread "main" java.lang.NoClassDefFoundError: Files/myname/myclass
  Despite the fact that other classes will load correctly and the .class file is in that path.</p>