Skip to content

OmixVisualization/qtjambi

QtJambi - Java Bindings for Qt

QtJambi - Java Bindings for Qt

Overview

QtJambi is Qt bindings for the Java programming language originally developed by Trolltech, the predecessor of The Qt Company. The successive open source project provides bindings of Qt5 and Qt6 releases for latest Java JDK versions.

The bindings are generated by inspection of the C++ header files of the Qt implementation then emitting code to provide the necessary JNI (Java Native Interface) and .java files to provide linkage to Java. An application programmer using QtJambi would simply add the corresponding Java libraries (.jar files) to their Java project to be able to develop and deploy their application.

Availability

Maven Central Release (latest by date) GitHub Sponsors

QtJambi is available for Java 8 or 11 and higher using Qt5.15 and Qt6 in Java on Windows, Android Linux and macOS. Most Qt modules are available as QtJambi module as listed here. All modules are published as Maven Artifact. The native components for Windows available on Maven require Qt binaries for MSVC (msvc2022_64). Mingw Qt is not compatible with QtJambi unless you build it from scratch with Mingw. By default, the native components of only essential modules are made available on Maven. Please make a request via /issues if you require native components not yet available.

Support

QtJambi is an open source project making the power of Qt available for your Java projects free of charge. However, this project is causing ongoing costs. Please consider supporting this project by donation. Your support is highly appreciated.

Sponsor this project

How To Build From Sources

If you just want to use QtJambi for your application development you can skip these instructions and simply use the provided binaries from the list of modules. Make yourself familiar with developing applications with QtJambi.

Requirements

  • Apache Ant (min. 1.10.x)
  • Java Development Kit (e.g. OpenJDK, tested with Java 1.8, 11 and 21)
  • Qt 5.15 or 6.x (using the Qt Online Installer)
  • Minimum required Qt modules: QtCore, QtQml, QtNetwork, QtConcurrent and QtXml
  • C++ compiler and make (Gcc, Clang, MSVC2022)
  • XCode command line tools (macOS only)

When building for Android all required SDK and NDK components are downloaded automatically. QtJambi build system has been tested on Linux (Ubuntu x64 and arm64, OpenSUSE x64, Fedora x64), Windows (with MSVC2022 for x64 and amd64), macOS (x64 and amd64) and FreeBSD (x64).

Building QtJambi

If you don't need the entire set of Qt modules available in Java edit property qtjambi.skipped.modules in build.properties and exclude Qt modules you don't need. This saves compilation time. Although QtJambi build process requires the availability of QtQml, QtNetwork, QtConcurrent and QtXml, you can skip them from being generated as QtJambi module.

On Windows open the Visual Studio command prompt for x64 architecture (to be found in: --> Start Menu --> Programs --> Visual Studio 2022 --> Visual Studio Tools --> VC). On Linux, macOS or if you intend to use Mingw instead of MSVC open a plain terminal.

Change to directory of your QtJambi clone.

Add Apache Ant to your PATH variable for easy access:

> set PATH=<path to..>\apache-ant\bin;%PATH%

If the command line does not find the program java by default or if you intend to use specific Java installation add this variable:

> set JAVA_HOME_TARGET=path to your java jdk

Start the build process by following command:

> ant all

(This step may take several hours.)

QtJambi's build process will automatically detect installed Qt:

  • On Windows it expects Qt to be installed in C:\Qt.

  • On Linux and macOS it searches for Qt installation in /opt/Qt.

  • On macOS it additionally searches in /Library/Qt and /System/Qt.

If Qt has not been found at these locations the build process searches in the user's home directory and in QtJambi's parent directory for Qt installation.

If a Qt installation has been found the build process starts building QtJambi for all Qt versions available.

If no Qt installation has been found the build process tries to detect qmake executable (on Linux additionally qmake-qt5/qmake-qt6).

The generated QtJambi version is based upon the Qt version linked against. Be aware that the patch version number (third) is not necessarily the same for Qt and QtJambi.

Finally, find all Java libraries in directory <qtjambiversion>/deployment and native bundles in <qtjambiversion>/deployment/native. Additionally, the platform-dependent libraries can be found in <qtjambiversion>/deployment/platforms.

Additional Options

You can call ant with additional properties as listed below. Therefore use the -D command line argument: ant -Dkey=value all.

  • qt - specify comma-separated Qt versions to be used, e.g. -Dqt="6.5,6.7,6.8".
  • qtbase - specify Qt installer's base directory, e.g. -Dqtbase=/var/Qt. Can be combined with qt.
  • qtdir - specify Qt version and platform directory, e.g. -Dqtdir=/var/Qt/6.8.1/macos. This option allows multiple directories separated by path separator. (If this option is specified qt and qtbase have no effect.)
  • qmake - specify a path to a qmake program to be used for building QtJambi. (If this option is specified qt, qtbase and qtdir have no effect.)
  • android - specify true to build QtJambi for Android. Therefore, Qt for Android has to be installed. By specify -Dandroid=only ant skips building QtJambi for the build platform. When compiling for Android all required NDK and SDK components are downloaded automatically. Alternatively, specify the Android NDK install path with -Dndk=....

All these properties could also be placed in build.properties file.

Alternatively, ant reads following environment variables:

  • QTVERSIONS - may soecify comma-separated Qt versions to be used
  • QTBASE - may specify Qt installer's base directory
  • QTDIR - may specify Qt version and platform directory
  • ANDROID_NDK - path to installed Android NDK

Unit Tests

Optionally, you could create and run unit tests:

> ant tests.generate tests.run

Find unit test results in directory TestResults.

Cross-Compilation

After building QtJambi for the running operation system you can additionally cross-compile QtJambi for other platforms. For instance if you want to build for Linux arm:

> ant -Dqmake=/opt/Qt/6.8.1/arm-gnueabi/bin/qmake library.native

Just specify the third-platform cross-compiled qmake with -Dqmake=path.

If you are using Qt from Qt installer located at default installation path (e.g. C:\Qt) and Android binaries are installed you can simply use the -Dandroid=true argument to build QtJambi for Android along with the platform-soecific binaries:

> ant -Dandroid=true all

How To Use QtJambi

Example

Add qtjambi-VERSION.jar to the classpath of your Java project containing the most essential Qt Core, Gui and Widgets modules. If you use Maven to build your application simply add following dependency to your project:

<dependency>
  <groupId>io.qtjambi</groupId>
  <artifactId>qtjambi</artifactId>
  <version>$VERSION</version>
</dependency>

(exchange $VERSION either by 6.5.10 or 6.8.1).

Otherwise, download QtJambi JAR file from Maven Central Repository.

Create a file Test.java containing the following code:

import io.qt.widgets.*;
public class Test {
    public static void main(String[] args) {
        QApplication.initialize(args);
        QMessageBox.information(null, "QtJambi", "Hello World!");
        QApplication.shutdown();
    }
}

Compile the file:

javac -cp qtjambi-6.8.1.jar Test.java

Execute Example

For execution you need the platform dependent binaries of QtJambi either as self-extracting bundle or as library file (e.b. dll). For instance, if you are working on Windows download qtjambi-native-windows-x64-VERSION.jar from Maven Central Repository and add it to java class path.

Additionally, you need Qt. Use the Qt installer to install Qt on your system. Make sure you are using the same Qt version and QtJambi version (the first and second version digit needs to be the same). When using Maven artifacts for Windows you need to select MSVC 2022 64-Bit (msvc2022_64), as they are not compatible with Mingw Qt.

When running a QtJambi application you have to make the locations of Qt libraries known to Java. Therefore, use the PATH environment (LD_LIBRARY_PATH on Linux, DYLD_FRAMEWORK_PATH on macOS) or the Java runtime property java.library.path. By default, on Windows Qt libraries are located in bin directory and on Linux and macOS in lib directory.

The example program can be executed this way on Windows:

java -cp qtjambi-6.8.1.jar;. -Djava.library.path=C:\Qt\6.8.1\msvc2022_64\bin Test

On Linux it looks this way:

java -cp qtjambi-6.8.1.jar:. -Djava.library.path=<path to>/Qt/6.8.1/gcc_64/lib Test

On macOS you additionally need to use the start parameter -XstartOnFirstThread:

java -cp qtjambi-6.8.1.jar:. -Djava.library.path=<path to>/Qt/6.8.1/macos/lib -XstartOnFirstThread Test

On Linux of your distribution provides Qt (of correct version) as system library you don't need to specify library path at all. However, since Maven-published binaries for Linux have been built with Qt from installer they might be incompatible to your system-specific Qt. In this case it is recommended to build QtJambi on your Linux system by using the installed qmake or install Qt by using Qt installer.

If the example fails with a UnsatisfiedLinkError QtJambi libraries and Qt libraries seem to be incompatible. Read here about library requirements and compatibility.

In general, you can start learning how to use Qt in Java as it is introduced for C++. There are a couple of specifics for QtJambi that are introduced here. Instead of starting your program with a java command as shown above you can deploy your application as executable as described here.

Licenses

            GNU LESSER GENERAL PUBLIC LICENSE

 QtJambi is Copyright (C) 1992-2009 Nokia. All rights reserved.
            Copyright (C) 2009-2024 Dr. Peter Droste, 
                                    Omix Visualization GmbH & Co. KG. 
                                    All rights reserved.

 You may use, distribute and copy QtJambi under the terms of
 [GNU Lesser General Public License version 2.1](LICENSE.LGPLv21).
            GNU GENERAL PUBLIC LICENSE

 QtJambi is Copyright (C) 1992-2009 Nokia. All rights reserved.
            Copyright (C) 2009-2024 Dr. Peter Droste, 
                                    Omix Visualization GmbH & Co. KG. 
                                    All rights reserved.

 You may use, distribute and copy QtJambi under the terms of
 GNU Lesser General Public License version 3. That license references
 the [General Public License version 3](LICENSE.GPLv3). Other
 portions of the QtJambi may be licensed directly under this license.

Further Reading