The objective of this lab is use jextract to generate Panama code and successfully execute an existing Java Hello World example. The following are the exercises for Lab 1:
- Exercise 1 - Generate classes for stdio.h
- Exercise 2 - Generate source code for stdio.h
- Exercise 3 - Execute the existing HelloWorld.java file
- Exercise 4 - (Optionally) Setup your IDE
The following is C Source code of a Hello World application that is shown as a reference. Your task is to generate binding code so that the Java application can invoke C functions natively similar to the C version of Hello World.
#include <stdio.h>
int main() {
printf(“Hello, World! \n”);
return 0;
}Before you begin change directory to lab1 and run the following scripts to clean up: Linux and MacOS
bash clean.shWindows
clean_windows.cmdThe following are the parameters when using the jextract tool.
classes- Destination of class filessrc- Destination of source filesorg.unix- Package namespace$C_INCLUDE_PATH- C include directory path$C_INCLUDE_PATH/stdio.h- The location of the header file
If you are unsure what the jextract options are type:
jextract -hIf you get an error your JAVA_HOME is probably not setup properly.
Refer to Setup Develpment Environment Instructions
After classes are generated directories should look like the following:
|-- classes
| |-- org
| | unixAfter source files are generated directories should look like the following:
|-- src
| |-- org
| |-- unixjava -cp classes --enable-native-access=ALL-UNNAMED --add-modules jdk.incubator.foreign src/HelloWorld.java
classes- ClasspathALL-UNNAMED- enable native accessjdk.incubator.foreign- Module to addsrc/HelloWorld.javaorsrc\HelloWorld.java- Java application
After executing the Java application the following should output should be:
Hello World! Panama style
(Java) Hello World! Panama style
Set Java compiler to point to use Panama’s JDK
Compiler JVM options (--add-modules jdk.incubator.foreign)
Do the following:
gcc helloworld.coutputs:
a.out
#Windows
a.out.exeTo run
./a.out