feat: print hello world with Java down calling C
This commit is contained in:
parent
04e2c6904b
commit
576f9c9eaf
46
java-panama/pom.xml
Normal file
46
java-panama/pom.xml
Normal file
@ -0,0 +1,46 @@
|
|||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung.java.panama</groupId>
|
||||||
|
<artifactId>java-panama</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<name>java-panama</name>
|
||||||
|
<url>https://maven.apache.org</url>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<maven.compiler.source>19</maven.compiler.source>
|
||||||
|
<maven.compiler.target>19</maven.compiler.target>
|
||||||
|
<maven.compiler.version>3.10.1</maven.compiler.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>5.9.0</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<version>${maven.compiler.version}</version>
|
||||||
|
<configuration>
|
||||||
|
<source>${maven.compiler.source}</source>
|
||||||
|
<target>${maven.compiler.target}</target>
|
||||||
|
<compilerArgs>
|
||||||
|
<arg>--add-opens=java.base/java.lang.foreign=ALL-UNNAMED</arg>
|
||||||
|
<arg>--enable-preview</arg>
|
||||||
|
</compilerArgs>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</project>
|
@ -0,0 +1,42 @@
|
|||||||
|
package com.baeldung.java.panama;
|
||||||
|
|
||||||
|
import java.lang.foreign.*;
|
||||||
|
import java.lang.invoke.MethodHandle;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
import static java.lang.foreign.ValueLayout.ADDRESS;
|
||||||
|
import static java.lang.foreign.ValueLayout.JAVA_INT;
|
||||||
|
|
||||||
|
|
||||||
|
public class HelloWorld {
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Throwable {
|
||||||
|
|
||||||
|
String symbolName = "printf";
|
||||||
|
String greetings = "Hello World from Project Panama Baeldung Article";
|
||||||
|
|
||||||
|
Linker nativeLinker = Linker.nativeLinker();
|
||||||
|
SymbolLookup stdlibLookup = nativeLinker.defaultLookup();
|
||||||
|
SymbolLookup loaderLookup = SymbolLookup.loaderLookup();
|
||||||
|
|
||||||
|
FunctionDescriptor functionDescriptor = FunctionDescriptor.of(
|
||||||
|
JAVA_INT.withBitAlignment(32),
|
||||||
|
ADDRESS.withBitAlignment(64)
|
||||||
|
);
|
||||||
|
|
||||||
|
MethodHandle methodHandle =
|
||||||
|
loaderLookup.lookup(symbolName)
|
||||||
|
.or(() -> stdlibLookup.lookup(symbolName))
|
||||||
|
.map(
|
||||||
|
symbolAddress -> nativeLinker.downcallHandle(symbolAddress, functionDescriptor)
|
||||||
|
).orElse(null);
|
||||||
|
|
||||||
|
|
||||||
|
Objects.requireNonNull(methodHandle);
|
||||||
|
|
||||||
|
try (MemorySession memorySession = MemorySession.openConfined()) {
|
||||||
|
MemorySegment nativeGreetings = memorySession.allocateUtf8String(greetings + "\n");
|
||||||
|
methodHandle.invoke(nativeGreetings);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user