Saturday, May 29, 2010

ECJ - Eclipse Compiler for Java

Eclipse provides and uses its own compiler that is not javac
  • The Eclipse compiler is used inside the IDE (Eclipse)
  • The Eclipse compiler can also be used as a pure batch compiler outside of Eclipse
Finding the Batch compiler

The batch compiler class is located in the JDT Core plug-in. The name of the class is org.eclipse.jdt.compiler.batch.BatchCompiler. It is packaged into plugins/org.eclipse.jdt.core_3.5.2.v_981_R35x.jar. Since 3.2, it is also available as a separate download. The name of the file is ecj.jar. Its corresponding source is also available. To get them, go to the download page, select a build and search for the section JDT Core Batch Compiler. This jar contains the batch compiler and the javac ant adapter.

Running the batch compiler from command line

Check compiler version
$ java -jar ecj-3.5.2.jar -version
or
$ java -jar org.eclipse.jdt.core_3.5.2.v_981_R35x.jar -version

Eclipse Compiler for Java (TM) 0.981_R35x, 3.5.2 release, Copyright IBM Corp 2000, 2009. All rights reserved.


Compile a source file
$ java -jar ecj-3.5.2.jar HelloWorld.java
or
$ java -jar org.eclipse.jdt.core_3.5.2.v_981_R35x.jar HelloWorld.java

Running the batch compiler programmatically
Use the static compile(String commandLine, PrintWriter outWriter, PrintWriter errWriter, CompilationProgress progress) method of the class BatchCompiler.

org.eclipse.jdt.compiler.CompilationProgress progress = null; // instantiate your subclass
org.eclipse.jdt.internal.compiler.batch.BatchCompiler.compile(
"-classpath rt.jar A.java",
new PrintWriter(System.out),
new PrintWriter(System.err),
progress);

You can control how progress is reported, or how the batch compiler is canceled, by subclassing the class org.eclipse.jdt.compiler.CompilationProgress.

Using the ant javac adapter

The Eclipse compiler can be used inside an Ant buildfile using the javac adapter. In order to use the Eclipse compiler, you simply need to define the build.compiler property in your buildfile.

property name="build.compiler" value="org.eclipse.jdt.core.JDTCompilerAdapter"

More Information
Look in Eclipse SDK - Help : JDT Plug-in Developer Guide > Programmer's Guide > JDT Core > Compiling Java code

So who all are using the Eclipse Java Compiler - besides developers using Eclipse for writing java code :) ? Apache Tomcat uses ECJ to compile JSPs, IntelliJ IDEA has support ECJ, as of GCJ 4.3, GCJ integrates with ECJ, Liferay Builds with ECJ, and there are many others.

Sunday, May 9, 2010

Why Open Source?

Now that I work on Eclipse which is Open Source, I decided to give some serious thought to the question - why allow your software to be used for free?

http://www.geograph.org.uk/photo/176270

Before getting to software lets see if some other things have been allowed to be used for free in the past. The first example I can think of is Roads - governments construct roads and let people use them for free. The goal of the government is to make places - markets, malls, industries, schools etc - on the road to be more accessible. These places in turn generate money and the government earns its taxes from them which in turn are used to construct more roads.

http://upload.wikimedia.org/wikipedia/commons/8/88/BomstasjonSvinesund.jpg

Now there are some 'Toll roads' as well where you pay for each usage of the road. People will pay for each usage of a road if firstly they have the money, and secondly they have no alternative free road to use.

In both cases the roads do not serve any purpose other than make places accessible where the actual work gets done and people make money. True you can go for a nice relaxing drive but that is not the primary use of a road.

In software world, an example of 'Toll Road' would be Microsoft Windows - people do not pay for Windows just to click around with a mouse; they probably need a word processor, a browser, a presentation maker etc to get some work done. Since historically there were not many choices in operating systems and Microsoft managed to market Windows well - people ended up paying for it.

Now Eclipse began as an IBM project which was later made open source. The Eclipse foundation has a number of strategic members and I think all of them sell products built of top of Eclipse platform and make money. Eclipse being a free and open road means that the road is just awesome and a lot of people use it, making the final destinations (commercial products) that much more profitable.

MySql also has a free version and an enterprise commercial version. Mozilla Foundation makes money from Search royalties and some donations. However if Netscape had defeated Microsoft in the browser wars over a decade ago then I do not think we would have been using Firefox for free. (Maybe all of us should thank Microsoft for this :p )

I assume something similar happens in Apache, Linux worlds and other open source projects. If you are not able to make people pay for the road, you give it away for free, create a huge user base or community, invite contributions from this community and then reap benefits from making money at the malls, industries, schools down the road.

Sunday, May 2, 2010

3.6 M7 - New & Cool in JDT

Till now Eclipse had a package name compression preference with which 'org.eclipse.ui.contentassist' could be compressed to 'o.e.u.contentassist'. From M7 onwards package names in Java views can be abbreviated with some custom rules as well.
















Rules for this example:
org.eclipse.ui={UI}
org.eclipse.ui.texteditor={T}
org.eclipse.ui.internal.texteditor=[iT]

These rules can be configured in Preferences > Java > Appearance > Abbreviate package names.

I really like this feature. Thanks Guven Demir for contributing this(Bug 299514) !!

Also, we have a new decorator (a big red '!') on Java projects and working sets that contain build path errors. We went through a number of options (Bug 215980)before settling on this one. Helps to spot those build path problems really easily!