<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Dynamic in-memory compilation</title>
	<atom:link href="http://www.javablogging.com/dynamic-in-memory-compilation/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.javablogging.com/dynamic-in-memory-compilation/</link>
	<description>Tracking surprises, features and bugs</description>
	<lastBuildDate>Sun, 05 Feb 2012 15:43:49 -0500</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8</generator>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
		<item>
		<title>By: George</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-20496</link>
		<dc:creator>George</dc:creator>
		<pubDate>Mon, 21 Nov 2011 23:15:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-20496</guid>
		<description>When I try this with dynamic Class A that extends non-dynamic class B and I properly set the class path during compile time, it compiles but I can&#039;t load it.

If I load it with the custom file manager, I get an issue finding class B. If I try it with Class.forName(), I get an issue finding class C.

Thoughts?</description>
		<content:encoded><![CDATA[<p>When I try this with dynamic Class A that extends non-dynamic class B and I properly set the class path during compile time, it compiles but I can&#8217;t load it.</p>
<p>If I load it with the custom file manager, I get an issue finding class B. If I try it with Class.forName(), I get an issue finding class C.</p>
<p>Thoughts?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: RSS Digest: Week Ending 18-Nov-2011 &#124; Zahid Qureshi</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-20416</link>
		<dc:creator>RSS Digest: Week Ending 18-Nov-2011 &#124; Zahid Qureshi</dc:creator>
		<pubDate>Fri, 18 Nov 2011 12:29:49 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-20416</guid>
		<description>[...] Dynamic in-memory compilation http://www.javablogging.com/dynamic-in-memory-compilation/ [...]</description>
		<content:encoded><![CDATA[<p>[...] Dynamic in-memory compilation <a href="http://www.javablogging.com/dynamic-in-memory-compilation/" rel="nofollow">http://www.javablogging.com/dynamic-in-memory-compilation/</a> [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Hannes de Jager</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-20121</link>
		<dc:creator>Hannes de Jager</dc:creator>
		<pubDate>Sun, 13 Nov 2011 18:38:43 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-20121</guid>
		<description>Great article. You should have a twitter feed to announce blog posts. Would like to follow you like that...</description>
		<content:encoded><![CDATA[<p>Great article. You should have a twitter feed to announce blog posts. Would like to follow you like that&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sandeep</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-20115</link>
		<dc:creator>Sandeep</dc:creator>
		<pubDate>Sun, 13 Nov 2011 17:12:45 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-20115</guid>
		<description>Hi,
What if I want to specify the version of compiler to use. I mean I want to use some compiler which is not the same as the one which through which I am running the demo program written by you above.

&lt;a href=&quot;http://extreme-java.com/naming-java-files/&quot; rel=&quot;nofollow&quot;&gt;Naming Java Files&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>Hi,<br />
What if I want to specify the version of compiler to use. I mean I want to use some compiler which is not the same as the one which through which I am running the demo program written by you above.</p>
<p><a href="http://extreme-java.com/naming-java-files/" rel="nofollow">Naming Java Files</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: bhomass</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-20060</link>
		<dc:creator>bhomass</dc:creator>
		<pubDate>Sat, 12 Nov 2011 06:40:05 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-20060</guid>
		<description>This is a great article, I am using it for my application. However, one issue is not clear. If the class I am compiling depends on other in memory classes, how is that fed to the compiler? Normally, the filemanager would have a StandardLocation.CLASS_OUTPUT where the dependent classes can be found. of course, not true in this approach. w/o these dependent classes, the compilation would fail.</description>
		<content:encoded><![CDATA[<p>This is a great article, I am using it for my application. However, one issue is not clear. If the class I am compiling depends on other in memory classes, how is that fed to the compiler? Normally, the filemanager would have a StandardLocation.CLASS_OUTPUT where the dependent classes can be found. of course, not true in this approach. w/o these dependent classes, the compilation would fail.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Sergio</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-19649</link>
		<dc:creator>Sergio</dc:creator>
		<pubDate>Thu, 03 Nov 2011 09:59:41 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-19649</guid>
		<description>Really cool. But could you do the same without knowing in advance the class name ?. I mean, generating in memory byte codes only having the source code of the class?</description>
		<content:encoded><![CDATA[<p>Really cool. But could you do the same without knowing in advance the class name ?. I mean, generating in memory byte codes only having the source code of the class?</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: jhoni</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-17247</link>
		<dc:creator>jhoni</dc:creator>
		<pubDate>Thu, 13 Oct 2011 09:22:55 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-17247</guid>
		<description>&quot;Intriguing post - thank you. I thought your post was really intriguing.  Thanks over again - I will come back.
&lt;a href=&quot;http://www.transitioncomputing.com/&quot; rel=&quot;nofollow&quot;&gt;Software Development&lt;/a&gt;</description>
		<content:encoded><![CDATA[<p>&#8220;Intriguing post &#8211; thank you. I thought your post was really intriguing.  Thanks over again &#8211; I will come back.<br />
<a href="http://www.transitioncomputing.com/" rel="nofollow">Software Development</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Karsten</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-16444</link>
		<dc:creator>Karsten</dc:creator>
		<pubDate>Mon, 03 Oct 2011 07:35:27 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-16444</guid>
		<description>Nice work! Did anybody know an Eclipse-plugin that uses this way to compile the java sources. At work I&#039;m really annoyed by an enthusastic virus scanner that I can&#039;t disable and every compiling-task takes a lot of time (even on a SSD)</description>
		<content:encoded><![CDATA[<p>Nice work! Did anybody know an Eclipse-plugin that uses this way to compile the java sources. At work I&#8217;m really annoyed by an enthusastic virus scanner that I can&#8217;t disable and every compiling-task takes a lot of time (even on a SSD)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Britt</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-11491</link>
		<dc:creator>Britt</dc:creator>
		<pubDate>Mon, 20 Jun 2011 19:53:56 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-11491</guid>
		<description>Great example!  After doing something similar, I thought it would be useful to mention that the public code in com.sum.script.java.MemoryJavaFileManager does a lot of this, allowing for a simplification of your example:

&lt;code&gt;
public class DynaCompTest {
    public static void main(String[] args) throws Exception {
        // Full name of the class that will be compiled.
        // If class should be in some package,
        // fullName should contain it too
        // (ex. &quot;testpackage.DynaClass&quot;)
        String fullName = &quot;DynaClass&quot;;

        // Here we specify the source code of the class to be compiled
        StringBuilder src = new StringBuilder();
        src.append(&quot;public class DynaClass {\n&quot;);
        src.append(&quot;    public String toString() {\n&quot;);
        src.append(&quot;        return \&quot;Hello, I am \&quot; + &quot;);
        src.append(&quot;this.getClass().getSimpleName();\n&quot;);
        src.append(&quot;    }\n&quot;);
        src.append(&quot;}\n&quot;);

        System.out.println(src);

        // We get an instance of JavaCompiler. Then
        // we create a file manager
        // (our custom implementation of it)
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
        JavaFileManager fileManager = new MemoryJavaFileManager(compiler.getStandardFileManager(null, null, null));

        // Dynamic compiling requires specifying
        // a list of &quot;files&quot; to compile. In our case
        // this is a list containing one &quot;file&quot; which is in our case
        // our own implementation (see details below)
        List jfiles = new ArrayList();
        jfiles.add(MemoryJavaFileManager.makeStringSource(fullName + &quot;.java&quot;, src.toString()));

        // We specify a task to the compiler. Compiler should use our file
        // manager and our list of &quot;files&quot;.
        // Then we run the compilation with call()
        compiler.getTask(null, fileManager, null, null,
            null, jfiles).call();

        // Creating an instance of our compiled class and
        // running its toString() method
        Object instance = fileManager.getClassLoader(null)
            .loadClass(fullName).newInstance();
        System.out.println(instance);
    }
}
&lt;/code&gt;

The only thing needed is to add a getClassLoader() method to MemoryJavaFileManager, such as this one:

&lt;code&gt;
    @Override
    public ClassLoader getClassLoader(Location location) {
        return new java.security.SecureClassLoader() {
            @Override
            protected Class findClass(String name) throws ClassNotFoundException {
                byte[] b = classBytes.get(name);
                if (b != null)
                    return defineClass(name, b, 0, b.length);
                return super.findClass(name);
            }
        };
    }
&lt;/code&gt;

Source for MemoryJavaFileManager is just a google search away.

Thanks again for the great blog post!</description>
		<content:encoded><![CDATA[<p>Great example!  After doing something similar, I thought it would be useful to mention that the public code in com.sum.script.java.MemoryJavaFileManager does a lot of this, allowing for a simplification of your example:</p>
<p><code><br />
public class DynaCompTest {<br />
    public static void main(String[] args) throws Exception {<br />
        // Full name of the class that will be compiled.<br />
        // If class should be in some package,<br />
        // fullName should contain it too<br />
        // (ex. "testpackage.DynaClass")<br />
        String fullName = "DynaClass";</p>
<p>        // Here we specify the source code of the class to be compiled<br />
        StringBuilder src = new StringBuilder();<br />
        src.append("public class DynaClass {\n");<br />
        src.append("    public String toString() {\n");<br />
        src.append("        return \"Hello, I am \" + ");<br />
        src.append("this.getClass().getSimpleName();\n");<br />
        src.append("    }\n");<br />
        src.append("}\n");</p>
<p>        System.out.println(src);</p>
<p>        // We get an instance of JavaCompiler. Then<br />
        // we create a file manager<br />
        // (our custom implementation of it)<br />
        JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();<br />
        JavaFileManager fileManager = new MemoryJavaFileManager(compiler.getStandardFileManager(null, null, null));</p>
<p>        // Dynamic compiling requires specifying<br />
        // a list of "files" to compile. In our case<br />
        // this is a list containing one "file" which is in our case<br />
        // our own implementation (see details below)<br />
        List jfiles = new ArrayList();<br />
        jfiles.add(MemoryJavaFileManager.makeStringSource(fullName + ".java", src.toString()));</p>
<p>        // We specify a task to the compiler. Compiler should use our file<br />
        // manager and our list of "files".<br />
        // Then we run the compilation with call()<br />
        compiler.getTask(null, fileManager, null, null,<br />
            null, jfiles).call();</p>
<p>        // Creating an instance of our compiled class and<br />
        // running its toString() method<br />
        Object instance = fileManager.getClassLoader(null)<br />
            .loadClass(fullName).newInstance();<br />
        System.out.println(instance);<br />
    }<br />
}<br />
</code></p>
<p>The only thing needed is to add a getClassLoader() method to MemoryJavaFileManager, such as this one:</p>
<p><code><br />
    @Override<br />
    public ClassLoader getClassLoader(Location location) {<br />
        return new java.security.SecureClassLoader() {<br />
            @Override<br />
            protected Class findClass(String name) throws ClassNotFoundException {<br />
                byte[] b = classBytes.get(name);<br />
                if (b != null)<br />
                    return defineClass(name, b, 0, b.length);<br />
                return super.findClass(name);<br />
            }<br />
        };<br />
    }<br />
</code></p>
<p>Source for MemoryJavaFileManager is just a google search away.</p>
<p>Thanks again for the great blog post!</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Teresa</title>
		<link>http://www.javablogging.com/dynamic-in-memory-compilation/comment-page-1/#comment-9808</link>
		<dc:creator>Teresa</dc:creator>
		<pubDate>Tue, 12 Apr 2011 08:38:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.javablogging.com/?p=438#comment-9808</guid>
		<description>Hey interesting post! Thanks for sharing!!</description>
		<content:encoded><![CDATA[<p>Hey interesting post! Thanks for sharing!!</p>
]]></content:encoded>
	</item>
</channel>
</rss>

