@@ -0,0 +1,19 @@
|
||||
package com.baeldung.jni;
|
||||
|
||||
public class ExampleObjectsJNI {
|
||||
|
||||
static {
|
||||
System.loadLibrary("native");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ExampleObjectsJNI instance = new ExampleObjectsJNI();
|
||||
UserData newUser = instance.createUser("John Doe", 450.67);
|
||||
instance.printUserData(newUser);
|
||||
}
|
||||
|
||||
public native UserData createUser(String name, double balance);
|
||||
|
||||
public native String printUserData(UserData user);
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.jni;
|
||||
|
||||
public class ExampleParametersJNI {
|
||||
|
||||
static {
|
||||
System.loadLibrary("native");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Java: My full name: " + new ExampleParametersJNI().sayHelloToMe("Martin", false));
|
||||
long sumFromNative = new ExampleParametersJNI().sumIntegers(456, 44);
|
||||
System.out.println("Java: The sum coming from native code is: " + sumFromNative);
|
||||
}
|
||||
|
||||
// Declare another method sumIntegers that receives two integers and return a long with the sum
|
||||
public native long sumIntegers(int first, int second);
|
||||
|
||||
// Declare another method sayHelloToMe that receives the name and gender and returns the proper salutation
|
||||
public native String sayHelloToMe(String name, boolean isFemale);
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung.jni;
|
||||
|
||||
public class HelloWorldJNI {
|
||||
|
||||
static {
|
||||
System.loadLibrary("native");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
new HelloWorldJNI().sayHello();
|
||||
}
|
||||
|
||||
// Declare a native method sayHello() that receives no arguments and returns void
|
||||
public native String sayHello();
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
package com.baeldung.jni;
|
||||
|
||||
public class UserData {
|
||||
|
||||
public String name;
|
||||
public double balance;
|
||||
|
||||
public String getUserInfo() {
|
||||
return "[name]=" + name + ", [balance]=" + balance;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user