This demo illustrates how GraalPy can be used to embed the qrcode
Python package in a script that runs on JBang.
Install GraalVM for JDK 23 and set the value of JAVA_HOME
accordingly.
We recommend using SDKMAN!. (For other download options, see GraalVM Downloads.)
sdk install java 23-graal
Afterward, install jbang
, for with:
sdk install jbang
To start the demo, run:
jbang run qrcode.java "Hello from GraalPy!"
qrcode.java defines required dependencies on GraalPy, which also allows to depend on Python packages.
//PIP qrcode==7.4.2
defines the dependency on the qrcode
Python package.
The main()
method checks the arguments and then creates a new Context
.
It then fetches the qrcode
Python module and maps it to a QRCode
Java interface.
Afterward, it uses the qrcode
module as illustrated in this example through Java interfaces.
Finally, it prints the result to System.out
.