Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Compiling Machine Code with GCJ

The GNU Compiler for Java allows you to compile Java source to bytecode or machine code, and Java bytecode to machine code.


advertisement
pen Source Java development tools are finally on the rise and the GNU Compiler Collection (GCC) is helping to lead the charge. GCC was originally just a C compiler, but support for Objective-C and C++ were quickly added. Since then, it has provided an invaluable service to cross-platform developers, providing them with a consistent compiler on a wealth of different systems. Now it has added GCJ, the GNU Compiler for Java, to its collection, allowing you to compile Java source to bytecode or machine code, and Java bytecode to machine code.

Getting started with GCJ isn't an easy task right now because it is still in development, but the latest source codes are available from ftp://gcc.gnu.org/pub/gcc/releases/index.html.

As Linux increases its presence in the corporate enterprise, interest is growing regarding the options one has for Java development and deployment on that platform. GCJ provides a means to achieve high-powered Java performance on Linux through native compilation.



To compile GCJ on Linux, you will first have to get the latest sources to GCC and untar the archive. Then you will have to create a directory in which you will perform the build. This can be a subdirectory of the main source directory, such as obj/, for example. GCC ships with an autoconf configure script which makes it easy to configure the compiler for compilation and installation, but you will have to execute the configure script from the obj/ directory. A typical invocation of the configure script may look like:

../configure --prefix=/usr/local --enable-shared \ --enable-threads --enable-version-specific-runtime-libs \ --enable-languages=c++,java

The C compiler is automatically compiled regardless of which particular language you are interested in. Since the Java compiler shares some functions with C++ compiler, you must compile it as well. You can specify this with the --enable-languages switch, as in the example. All that's left to compile and install is:

make make install

The GCJ native code compiler is now accessible as the gcj command. You can compile either Java source code files or Java bytecode files to native code, but when you link the object files, you need to specify which class contains the program's main entry point. You can do this with the --main= option. For example:

gcj -o HelloWorld --main=HelloWorld HelloWorld.o

This tells gcj that the class containing the main() method should use HelloWorld as the program entry point.

One step remains. Before you can start compiling Java programs with GCJ, you need to get libgcj, the runtime library for GCJ. It is currently distributed separately from GCC, but will be incorporated into the GCC source tree at some point in the future. To compile libgcj, you also need to create an obj/ directory and configure it as you did GCC. GCJ will automatically link programs against libgcj, so you don't have to pass an extra -l linking option.

If you are developing Java applications under Linux, you can also save yourself the trouble of compiling the GCJ compiler and runtime library by fetching a pre-compiled RPMS.

There you go, now you can get started compiling native code with Java the GNU way. While Open Source Java is catching on, it still lags behind the commercial sector. GCJ only implements Java 1.1 language features and, at the time this Solution was written, does not support inner classes. It also lacks support for a significant subset of the core Java libraries. However, it is still suitable for writing a wide variety of Java programs.



   
Daniel F. Savarese holds a B.S. in astronomy and an M.S. in computer science, both from the University of Maryland, College Park. He is the author of the OROMatcher regular expression library for Java. Reach him here.
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date