Running TeX on a java virtual machine

by Martin Monperrus
This document presents solutions to run Knuth's typesetting program TeX into a Java Virtual Machine. Why running TeX in a JVM? One reason is to be able to use TeX in a JVM only environment (e.g. Google App Engine).

Using NestedVM

NestedVM is a compiler of native MIPS code to Java bytecode (the authors compiled another port of tex for writing their papers).

To run tex in a a JVM, you compile tex-gpc to MIPS code (say tex.mips) and you then compile tex.mips to tex.class, i.e. to Java bytecode.

It seems easy? It is conceptually simple, but getting all tools to run and to work together is a difficult task. That's why I provide in this archive the executable proof-of-concept. At the end, you get two real-world documents, one for TeX and one for LaTeX (the sample document of the Springer LNCS style), to be compiled.


1) Download the archive:
$ curl -O

2) Unzip the archive
$ unzip

3) Build everything
$ cd nestedVM
$ sh

4) Admire the results
$ xdvi build/harvsamp.dvi
$ xdvi build/llncs.dvi

Main limitations of this approach: it's really slow :-) Howver, I hope to be able to compile TeX-GPC to Java source code first (see below), so as to get a significant speedup.

Using Extex


Using NTS

Dave Jarvis had modernized the New Typesetting System (NTS), with good success (as of Jan 2023), see

Using javaTeX

The second possibility is to use javaTeX. Timothy Murphy's javaTeX is a set of Java source files. It consists of some helpers classes and a class tex, resulting from the automated translation of the Pascal code of tex using a dedicated tool (web2java).

This solution is really nice for two reasons:
1) the resulting code is much faster (50x) than the MIPS one.
2) it enables us to easily modify and extend the Java code.

This archive demonstrates the compilation of tex.fmt and of a simple latex document.


1) Download the archive:
$ curl -O

2) Unzip the archive
$ unzip

3) Build everything
$ cd javatex
$ sh

4) Admire the results (limited :-)
$ ls -l build/plain.fmt
$ xdvi build/tex-wikipedia.dvi

Main limitations of this approach:
Since javaTeX is built on top of an old version of TeX (v3.14159 of March 1995), it is not able to deal with complex tex or latex document (like the LNCS sample document above). I guess it's due to the modern versions of TeX fonts (the tfm files) and/or the modern version of LaTeX (always the same error: Math formula deleted: Insufficient extension fonts)

Future work - do you want to contribute :-) ?:
* solving this font error to be able to compile advanced TeX and LaTeX documents with the current version
* modifying web2java to be able to translate TeX-GPC version of TeX to Java source code. Then we have neither to maintain two different change files ( and tex.jch) nor to run a slow version of TeX (tex.mips). It might only require to slightly modify the grammar and to adapt the helper classes.
See also compiling Knuth's tex with GNU Pascal - gpc.

Tagged as: