53

There has been some buzz about a tool called sjavac on the OpenJDK mailing lists. Also, there are two related JEPs: JEP 139: Enhance javac to Improve Build Speed and JEP 199: Smart Java Compilation, Phase Two.

My questions are:

  • What exactly is the sjavac tool?
  • Who is it intended for?
  • How do I use it?

Disclaimer: Self answered question. Just wanted to bring the knowledge of this tool to the StackOverflow community and to create a reference to future sjavac FAQ.

Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
aioobe
  • 413,195
  • 112
  • 811
  • 826

1 Answers1

57

What exactly is the sjavac tool?

The sjavac tool is an (allegedly smart) wrapper around javac, developed at Oracle and intended to provide the following features:

  • incremental compiles - recompile only what's necessary
  • parallel compilation - utilize more than one core during compilation
  • keep compiler in a hot VM - reuse a JIT'ed javac instance for consecutive invocations

When recompiling a set of source files, javac looks at the timestamps of the .java and .class files to determine what to keep and what to recompile. This is incredibly crude and can be devastating for large code bases. In addition to the timestamps sjavac inspects the public API of the dependencies to judge which files need to be recompiled.

Sjavac also attempts to split up the compilation into multiple invocations of javac. In other words, it brings a high level of parallelism to the build process.

Finally, the sjavac tool is split in a client part and a server part which allows you to leave sjavac running in the background, JIT'ed and ready for use in consecutive calls.

Who is it intended for?

People who are working on large projects and frequently recompiles the code base during development are encouraged to try out sjavac. (Be aware however that the tool is currently under development and there are still open issues.)

How do I use it?

The tool is not yet shipped with the OpenJDK, so you'll have to get it from the OpenJDK jdk9/dev repository. Also, there is no launcher in place yet, so you invoke it with java com.sun.tools.sjavac.Main.

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • 5
    +1 This seems to be poised to annul the only advantage of Eclipse's own compiler. Maybe in the future we won't have to ask each time "what compiler did you use?". – Marko Topolnik Oct 17 '14 at 12:21
  • It looks like openJDK8 also has that code, not sure if it's running, but: http://code.metager.de/source/xref/openjdk/jdk8/langtools/test/tools/sjavac/SJavac.java – Quartz Oct 17 '14 at 22:11
  • IMO `sjavac` should not be something the developer must set up to use directly; It should be what every IDE uses internally. In other words, "Who is it intended for?"---*everyone*. – Marko Topolnik Feb 11 '15 at 13:07
  • Why do you need a new tool? Why can't `javac` itself be modified to do incremental compiles, do parallel compiles, and accept directories as parameters instead of individual files? – user253751 Jul 13 '15 at 09:42
  • It looks like (not sure) this is going to be included in Java 9 release (see [JEP 199](http://openjdk.java.net/jeps/199)). – mkobit May 12 '16 at 03:00
  • Unfortunately no, it will not be released as a stand alone tool in JDK9. – aioobe May 12 '16 at 07:27
  • @immibis, that is (kinda indirectly?) answered here: https://www.slideshare.net/oehrstroem/building-large-java-projects-faster-multicore-javac-and-makefile-integration (assuming you care 4 years later :)) – hmijail May 27 '19 at 04:01
  • What is the state of `sjavac` now in 2022? – BuZZ-dEE Sep 22 '22 at 12:15