Building a Qt Project in Java using QtJambi.

I’ve been doing some Qt programming in C++ and I have been enjoying myself, but for my sins, I prefer working in Java. So I found myself wondering whether I could combine my two favourite software development technologies. A surprisingly long Internet search led me to the correct combination of words to discover QtJambi.

It seemed simple enough, just add the libraries to a gradle or maven project and you’re good to go. Alas, it wasn’t to be that simple. I run Fedora 44 as my operating system QtJambi is built on Ubuntu 20.04. This led to a version mismatch between the compilers used to build Qt and QtLambi. It didn’t like that at all. The solution is to build from source and this blog sets out the steps for doing just that on Fedora 44.

First, lets make sure our build environment is set up.

Install Fedora’s development tools by running sudo dnf groupinstall "Development Tools" and then make sure git is installed by running sudo dnf install git. That should give us what we need to build the C++ side of things. Next install java by running sudo dnf install java-25-openjdk-devel.QtJambi needs to build java and the endgame is to program in java anyway. Depending on how you program, you may have some of these installed already. QtJambi uses ant as it’s build tool so install that by running sudo dnf install ant. That’s everything installed.

Download and install Qt.

It is best to get this from qt.io which is the official site. Download and run the installer. Choose qt6.12.0 and what ever tools you want to have. Qt will be downloaded and installed in to your chosen directory. If you choose more than one version of the Qt framework, you will need to build QtJambi for every version you selected. This can take quite a while.

Remember the directory you installed Qt to as we will need it in the next step.

Depending on your Internet connection the download could take a little while.

Now, let’s set up our environment.

We need to create a couple of environment variables, so edit your confing file choice, for example, .bashrc or .zshrc. Add the following exports: export JAVA_HOME=/usr/lib/jvm/java-openjdk and export LD_LIBRARY_PATH=~/Qt/6.12.0/gcc_64/lib assuming you took the default path. If not, you’ll have to adjust the path accordingly.

At last, we’ll build the QtLambi project.

Clone the github repository by running git clone https://github.com/OmixVisualization/qtjambi.git which will download the source code into a sub-directory of your current location. Then cd in to that directory. To kick the build off run ant build. On my 8th generation Intel Core I7 mobile with 32 gb of ram, the build took just over an hour.

Next we deploy our libraries.

Copy the packaged libraries to where you want your libraries to live. Run cd qtjambi/6.12.0 and you should see a deployment directory. Run cp -r deployment /path/to/your/library. This step is optional if you are happy to use the libraries from the build directory.

Now we write our code.

Create a gradle project by running gradle init. If you prefer maven, you can use that. Open the project in your IDE and edit you build.gradle to look like the following:

/*
* This file was generated by the Gradle 'init' task.
*
* This generated file contains a sample Java application project to get you started.
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.13/userguide/building_java_projects.html in the Gradle documentation.
*/


plugins {
// Apply the application plugin to add support for building a CLI application in Java.
id 'application'
}


repositories {
// Use Maven Central for resolving dependencies.
mavenCentral()
}


dependencies {
// This dependency is used by the application.
implementation libs.guava
implementation files('/home/richard/Projects/java/__lib/qtjambi-6.12.0/native/qtjambi-native-linux-x64-6.12.0.jar')
implementation files('/home/richard/Projects/java/__lib/qtjambi-6.12.0/qtjambi-6.12.0.jar')
}


// Apply a specific Java toolchain to ease working on different environments.
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}


application {
mainClass = 'com.thefifthcontinent.App'
}
And add the following to the App.java:
/*
* This source file was generated by the Gradle 'init' task
*/
package com.thefifthcontinent;
import io.qt.widgets.*;

public class App {
    public static void main(String[] args) {
        QApplication.initialize(args);
        QMessageBox.information(null, "Java Qt", "Hello World");
        QApplication.shutdown();
    }
}

Finally run gradle run in the root of your project. If all’s gone well, you should see a Qt message box.

Full Repository

 

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.