|  | Home · Examples

# Hello Generator

We start of with the classic "Hello World" program - except we call it "Hello Generator". We have one class with one function: `hello()`. All the `hello()` function does is creating and showing a QPushButton.

    Missing snippet: hellogenerator.h.

What we need to do is tell the generator to map the class `HelloGenerator` and the `hello()` function to Java. We do this by writing a type system specification, which is a handwritten XML document describing the types that will be made available in the generated Java API.

We start the specification by giving the package name and default super class of our Java objects.

    <typesystem package="com.trolltech.examples.hellogenerator" default-superclass="com.trolltech.qt.QtJambiObject">

The typesystem node is the root node and contains all type system information.

Any objects without a superclass will extend the default superclass. Note that currently, all typesystems must have QtJambiObject as their default superclass. A typesystem specification can only describe one package. You need one typesystem specification for each package you wish to create.

We then tell the generator that we want to map the HelloGenerator class.

    <object-type name="HelloGenerator" />

The generator will now find and map the `hello()` function automatically. C++ classes are mapped to Java either as `object-type`s or `value-types`s, depending on whether they are object or value types in C++. We will take a closer look at this later in the tutorial.

We are now ready to run the generator. The generator takes the type system specification and a header file as input. The header file should include all classes we wish to map. Since we only have one class, we simply use its header file.

Assuming that the generator executable is in your path, you can then run the generator:

    generator hellogenerator.h typesystem_hellogenerator.txt

The generator will then create both the Java and C++ files. The Java files will be located in the package specified, and the C++ files in the folder cpp/com_trolltech_examples_generatortutorial.

Now we need to compile the C++ sources. We need to write a qmake `.pro` file for this. We print the file in its entirety here.

    TARGET = com_trolltech_examples_generatortutorial

    include($JAMBI_HOME/qtjambi/qtjambi_include.pri)
    include(./cpp/com_trolltech_examples_generatortutorial/com_trolltech_examples_generatortutorial.pri)

    HEADERS += hellogenerator.h

    SOURCES += hellogenerator.cpp

The qtjambi_include.pri needs to be included because it contains functionality used for the mappings. We also need to include the `.pri` file for our own library; this file is generated automatically by the generator. Then, you can simply run `qmake` and `make`:

    qmake hellogenerator.pro
    make

Note that you might need to run `qmake` with the `-config release` argument if Qt was built with debug support.

After compiling the C++ sources, you also need to compile the Java sources with `javac`.

    javac com/trolltech/examples/generatortutorial/HelloGenerator.java

Congratulations! Your first Java to C++ bindings using the Qt Jambi Generator are ready for action. You only need to make sure that the C++ library is in your library path. The finished library is placed in the `../lib` folder from the examples directory. To make it easy, you can simply put the library in the `lib` folder under the Jambi home directory, or add it to the Jambi platform JAR archive (see the installation guide for details).