How to use GCC with Java?

GCC is a front-end to compile several languages (GNU Pascal, Mercury, Cobol, GNU Modula-2, Modula-3, GHDL, PL/1, GCC Unified Parallel C...).

Currently the main GCC distribution contains front ends for C (gcc), C++ (g++), Objective C, Fortran, Java (GCJ), Ada (GNAT), and Go.

GCJ is the equivalent of javac, but as you can see the latest news is dated 2009.


GCC Java compiler

Once upon a time, there existed a GCC Java frontend, but it has been deprecated.

Notably, the package is not present anymore in Ubuntu 18.04, but it was in Ubuntu 16.04.

Why it was depreated: Java JRE vs GCJ

Just for fun though, we can try it out this historical software in an old Docker container. But TODO: I couldn't get this to work on Ubuntu 16.04 or 14.04 guests today, and I'm lazy to debug it. But I swear that I got this working at least once when I first answered this question.

Main.java

public class Main {
    public static void main(String args[]) {
        System.out.println("hello world");
    }
}

Compile and run:

sudo apt-get install gcj-4.8 libgcj16-dev
gcj-4.8 -c Main.java
gcj --main=Main -o Main Main.o
./Main

Compilation fails with:

gcj-4.8: error: libgcj.spec: No such file or directory

Related: https://bugs.launchpad.net/ubuntu/+source/gcj-4.0/+bug/24906

Android ART

Since Android 5, Android has been using the "ART runtime", which compiles the Java in apps ahead of time: https://en.wikipedia.org/wiki/Android_Runtime

Therefore, if you were to look into Java to binary compilation today 2019, that's where you should look.

Like everything else in Android, running ART without the rest of the bloat of Android might be impossible or insane unfortunately.

Here I briefly unpacked some precompiled Android apps: What is the native keyword in Java for? showing that they are actually compiled to shared objects.


Did you read the manual? gnu java

They have a compiler called gcj dedicated to Java language.


If anyone is reading this question past September 30, 2016:

The GNU Compiler for Java (GCJ) has been discontinued and is no longer part of the GNU Compiler Collection (GCC), as it can be seen in the release notes for GCC 7.

Tags:

Java

Cygwin

Gcc