Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is javac 1.5 running so slowly compared with the Eclipse compiler?

I have a Java Maven project with about 800 source files (some generated by javacc/JTB) which is taking a good 25 minutes to compile with javac.

When I changed my pom.xml over to use the Eclipse compiler, it takes about 30 seconds to compile.

Any suggestions as to why javac (1.5) is running so slowly? (I don't want to switch over to the Eclipse compiler permanently, as the plugin for Maven seems more than a little buggy.)

I have a test case which easily reproduces the problem. The following code generates a number of source files in the default package. If you try to compile ImplementingClass.java with javac, it will seem to pause for an inordinately long time.

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;

public class CodeGenerator
{
    private final static String PATH = System.getProperty("java.io.tmpdir");
    private final static int NUM_TYPES = 1000;

    public static void main(String[] args) throws FileNotFoundException
    {
        PrintStream interfacePs = new PrintStream(PATH + File.separator + "Interface.java");
        PrintStream abstractClassPs = new PrintStream(PATH + File.separator + "AbstractClass.java");
        PrintStream implementingClassPs = new PrintStream(PATH + File.separator + "ImplementingClass.java");
        interfacePs.println("public interface Interface<T> {");
        abstractClassPs.println("public abstract class AbstractClass<T> implements Interface<T> {");
        implementingClassPs.println("public class ImplementingClass extends AbstractClass<Object> {");

        for (int i=0; i<NUM_TYPES; i++)
        {
            String nodeName = "Node" + i;
            PrintStream nodePs = new PrintStream(PATH + File.separator + nodeName + ".java");
            nodePs.printf("public class %s { }\n", nodeName);
            nodePs.close();
            interfacePs.printf("void visit(%s node, T obj);%n", nodeName);
            abstractClassPs.printf("public void visit(%s node, T obj) { System.out.println(obj.toString()); }%n", nodeName);
        }
        interfacePs.println("}");
        abstractClassPs.println("}");
        implementingClassPs.println("}");
        interfacePs.close();
        abstractClassPs.close();
        implementingClassPs.close();
    }
}
like image 908
Simon Nickerson Avatar asked Mar 26 '09 16:03

Simon Nickerson


People also ask

Does Eclipse use javac?

Eclipse has its own built-in incremental compiler so it does not need (nor use) javac from a JDK. So yes, Eclipse's Java Development Tools (JDT) will function with only a JRE.

What compiler does Eclipse use?

Eclipse Java compiler is an incremental Java builder The Java compiler built in Eclipse is a part of JDT Core component (JDT: Java Development Tool). An incremental compiler automatically compiles code when changes are detected.

Does Eclipse use its own compiler?

Eclipse does have it's own compiler, it does not use the JDK compiler (javac). However, Eclipse's compiler produces standard bytecode that complies with the Java Language Specification (JLS) and JVM Specification, so the compiled code it produces will work on any compliant JVM.


1 Answers

Sun has confirmed to me by email that this is a new bug (6827648 in their bug database).

like image 119
Simon Nickerson Avatar answered Sep 20 '22 20:09

Simon Nickerson