Compiling Machine Code with GCJ

Compiling Machine Code with GCJ

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

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 withGCJ, 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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as