Archivos por Etiqueta: compiler

Java: Dynamic Compiling

Un ejemplo de uso del API de Compilacion de Java, en este caso generaremos una clase “HelloWorld” dinamicamente desde un String:

import java.util.Arrays;


public class StringCompiler {
	public static final void compile(String className, String sourceCode, 
			String outputDir) {
		long ts = System.currentTimeMillis();
		SimpleJavaFileObject fileObject = new JavaSourceFromString(className, 
		JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
		Iterable<? extends JavaFileObject> compilationUnits = Arrays
		Iterable<String> compilationOptionss = Arrays.asList( //
				"-source", "1.6", // Source version
				"-target", "1.6", // Target version
				"-d", outputDir // Output Directory
		DiagnosticCollector<JavaFileObject> diagnostics = new 
		CompilationTask compilerTask = compiler.getTask(null, null, 
			diagnostics, compilationOptionss, null, compilationUnits);
		System.out.format("Compiling %s ", className);
		if (! { // Error compiling
			for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
				System.out.format("Error on line %d in %s ", 
					diagnostic.getLineNumber(), diagnostic);
		System.out.format("time=%d (ms)\n", (System.currentTimeMillis() - ts));

	 * Simple HelloWorld Compilation
	public static void main(final String[] args) {
		final StringBuilder sb = new StringBuilder();
		final String tmpDir = System.getProperty("");
		sb.append("package com.acme;").append('\n');
		sb.append("public class Test {").append('\n');
		sb.append("  public static void main(String[] args) {").append('\n');
		sb.append("    System.out.println(\"hello world\");").append('\n');
		sb.append("  }").append('\n');
		compile("com.acme.Test", sb.toString(), tmpDir);

	public static class JavaSourceFromString extends SimpleJavaFileObject {
		final String code;

		JavaSourceFromString(final String name, final String code) {
			super(URI.create("string:///" + name.replace('.', '/') + 
				Kind.SOURCE.extension), Kind.SOURCE);
			this.code = code;

		public CharSequence getCharContent(boolean ignoreEncodingErrors) {
			return code;


Apache Commons JCI || Sample
Janino || Sample
Eclipse Compiler for Java || Sample


JVM performance optimization – Java

Java applications run on the JVM, but what do you know about JVM technology?

If you are a programmer then you have undoubtedly experienced that special feeling when a light goes on in your thought process, when those neurons finally make a connection, and you open your previous thought pattern to a new perspective.

All JVM implementations have one thing in common, which is their attempt to get application bytecode translated into machine instructions. Some JVMs interpret application code on load and use performance counters to focus on “hot” code. Some JVMs skip interpretation and rely on compilation alone. The resource intensiveness of compilation can be a bigger hit (especially for client-side applications) but it also enables more advanced optimizations.

Read complete articles:
JVM performance optimization, Part 1: A JVM technology primer.
JVM performance optimization, Part 2: Compilers.

A %d blogueros les gusta esto: