OCaml-Java: 2.0 alpha


Introduction

The objective of the OCaml-Java project is to allow seamless integration of OCaml and Java. Its ocamljava compiler can generate Java archives to be run on any Java virtual machine (version 1.7 or above). This possibility will allow to leverage Java libraries from purely OCaml code. Moreover, the OCaml-Java runtime does not need to rely on a global runtime lock, meaning that concurrent programming is now possible in a single OCaml process.

OCaml-Java exhibits good compatibility with respect to the original OCaml implementation since its very first version. However, versions 1.x should be regarded as mere prototypes, due to terrible performance and huge memory consumption. The upcoming 2.0 version fixes a number of issues, and greatly improves both CPU usage and memory footprint. The current alpha version is globally representative of what will be available in version 2.0; however, non-compatible changes may occur before 2.0 is actually published.

OCaml-Java is relased under the QPL/LGPL licences.

First steps

The new ocamljava compiler is akin to the ocamlc/ocamlopt compilers from the original OCaml distribution. However, rather than producing OCaml bytecode, or native code, it will compile source files to Java classes. The following table presents the correspondence between the files manipulated by the various OCaml compilers.

ocamlc ocamlopt ocamljava
interface source .mli .mli .mli
implementation source .ml .ml .ml
compiled interface .cmi .cmi .cmi
compiled implementation .cmo .cmx .cmj
object binary - .o .jo
compiled library .cma .cmxa .cmja
library binary - .a, .so, ... .ja
plugin - .cmxs .cmjs

Compiling sources through the ocamljava compiler is not different from compiling through one of the original compilers. Compiling then linking a source.ml file can be done in either two steps:

ocamljava -c source.ml
ocamljava -o myprog.jar source.cmj

or just one step:

ocamljava -o myprog.jar source.ml

Then, the created archive can be executed by any Java virtual machine:

java -jar myprog.jar

The binary distribution of OCaml-Java ships with the command-line tools listed in the following table.

command name description
camlp4xyz the various camlp4 preprocessors
ocaml the toplevel, as a terminal application
ocamlbuild the compilation manager
ocamlc the OCaml bytecode compiler
ocamldebug the debugger (for ocamlc-compiled programs)
ocamldep the dependency analyzer
ocamldoc the documentation generator
ocamlj the toplevel, with typer extensions (using Java bytecode)
ocamljar the post-compilation optimizer
ocamljava the Java bytecode compiler
ocamllex the lexer generator
ocamlrun the interpreter for ocamlc-compiled programs
ocamltop the toplevel, as a windowed application
ocamlwrap the Java wrapper

Compatibility

In its current version, OCaml-Java is based on OCaml version 4.01.0, and requires a Java 1.7 virtual machine to run compiled programs. The whole OCaml language is supported, with some limitations:

  • tail calls are optimized only for direct self-recursion;
  • evaluation order is not guaranteed to be the same as in ocamlc/ocamlopt (however, it should not be a major problem as evaluation order is not specified in the OCaml language);
  • stack overflow as well as memory shortage are not diagnosed by the OCaml-Java runtime (resulting in Java exceptions);
  • backtrace support is rudimentary;
  • ocamljava may fail to compile some (very) large functions (Java set a hard limit on method length).

Regarding primitives/libraries, compatibility is high but not perfect. Indeed, some POSIX routines are not available from Java. The libraries shipped with the original OCaml implementation are available in OCaml-Java: str, num, dynlink, bigarray, systhreads, threads, and graph should behave roughly as their equivalents in the original distribution. The unix library is only partially supported, and support for labltk is experimental.

Performance

Performance of ocamljava-generated code is regurlarly improved, but a reasonable rule of thumb for the current version is to consider that such code is on average 3 times slower than ocamlopt-generated code. On some numerical codes, ocamljava-compiled programs are on par with ocamlopt-compiled ones.

Warning! OCaml-Java is much slower than the original OCaml in the handling of exceptions.

Optimizer

Since version 2.0-early-access5, the distribution features a post-compilation optimizer (namely ocamljar) that can be applied to a linked OCaml-Java program (that is, a jar file) in order to produce an optimized version of the program. An overview of the tool is available here.

Java access

Since version 2.0-early-access8, the distribution includes a new extension to the OCaml typer that allows to create and manipulate Java instances using OCaml code. An overview of these extensions is available here.

Concurrency

The current version of OCaml-Java ships with a draft version of the concurrent library which provides several modules that are bare wrappers around Java classes. The doc directory contains the ocamldoc-generated documentation for these modules. An overview of the library is available here.

Warning! by default, OCaml-Java favors compatibility with the original OCaml implementation, meaning that it is based on a global runtime lock. In order to leverage the power of the concurrent library, it is necessary to disable the runtime lock by linking the program with the -runtime-parameter runtime-lock=off command-line option.

Applets

Since version 2.0-early-access9, the distribution allows to write applets. Applets can be based on the Java AWT toolkit, the Java Swing toolkit, or the OCaml Graphics module. More information on how to develop and deploy applet is available here.

Servlets

Since version 2.0-alpha1, the distribution allows to write servlets. Servlets can then be deployed to a container such as Apache Tomcat or Jetty. More information on how to develop and deploy servlets is available here.

Scripting

Since version 2.0-early-access5, scripting from a Java application is supported, by providing an OCaml script engine for the javax.script framework. An overview of the support for scripting is available here.

Wrapping

Since version 2.0-early-access6, the distribution features a tool named ocamlwrap that can be used to generate Java class definitions for easy access to ocamljava-compiled code from the Java language. An overview of the tool is available here.

Contact

The developer of OCaml-Java can be reached either by mail (xclerc AT ocamljava.org) or through the project bugtracker (available on github).