Java binary on Linux

Recently noticed support for Java binary on Linux kernel which means you can execute your Java applications simply as:
$ ./HelloWorld.class
And this can be achieved in following few steps(Assuming JDK is already installed and CLASSPATH properly configured):

  • Recompile your kernel with CONFIG_BINFMT_MISC option. This can be achieved as follows:
    #cd /usr/src/linux; make menuconfig
    Select "Executable file formats / Emulations" -> Kernel Support for MISC binaries
    Save and exit. Follow /usr/src/linux/README for further information on compiling the kernel.
    BINFMT_MISC can also be compiled as a independent module and inserted manually. This feature allows you to invoke almost any binary by simply typing it's name in shell. Refer to /usr/src/linux/Documentation/binfmt_misc.txt for more information on this.
  • Mount binfmt_misc and setup for Java executable:
    # mount binfmt_misc -t binfmt_misc /proc/sys/fs/binfmt_misc
    # echo ':Java:M::\xca\xfe\xba\xbe::/usr/local/bin/javawrapper:' > /proc/sys/fs/binfmt_misc/register
  • Now execute the following:
    # cat /usr/src/linux/Documentation/java.txt |grep -m3 -A 195 "Cut here"
    and copy the first script as /usr/local/bin/javawrapper. This script will add the class file to classpath.
    Compile the second C program as follow:
    # gcc -O2 -o javaclassname javaclassname.c
    # cp javaclassname /usr/local/bin
    This executable is required to find the fully qualified class name i.e. for class Test.class in package foo.bar, it will return foo.bar.Test
  • 5. And now the fun part. Just chmod any Java class to execute it.
    # javac HelloWorld.java
    # chmod +x HelloWorld.class
    # ./HelloWorld.class
  • I gathered this from /usr/src/linux/Documentation/java.txt which can be referred for more information.

    Post new comment


    *

    • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
    • Lines and paragraphs break automatically.