Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Code Block
$ java org.codehaus.janino.samples.ClassBodyDemo -help
Usage:
  ClassBodyDemo <class-body> { <argument> }
  ClassBodyDemo -help
If <class-body> starts with a '@', then the class body is read
from the named file.
The <class-body> must declare a method "public static void main(String[])"
to which the <argument>s are passed. If the return type of that method is
not VOID, then the returned value is printed to STDOUT.

...

The SimpleCompiler compiles a single "compilation unit" (a.k.a. ".java" file). Opposed to to normal JavaTM compilation, that compilation unit may define more than one public class. Example:

Code Block
// File "mycode.janino".

package my.pkg;

import java.util.*;

public class A {
    public static void main(String[] args) {
        B b = new B();
        b.meth1();
    }
}

public class B {
    void meth1() {
        System.out.println("Hello there.");
    }
}

It returns a ClassLoader from which you can retrieve the classes that were compiled.

To run this, type:

Code Block
$ java org.codehaus.janino.SimpleCompiler -help
Usage
    org.codehaus.janino.SimpleCompiler <source-file> <class-name> { <argument> }
Reads a compilation unit from the given <source-file> and invokes method
"public static void main(String[])" of class <class-name>, passing the
given <argument>s.
$
Code Block
$ java org.codehaus.janino.SimpleCompiler mycode.janino my.pkg.A
Hello there.
$

See Advanced for other ways of using JANINO.