Commit a2c4ab67 authored by Martin Deinhofer's avatar Martin Deinhofer
Browse files

added solution for exercise 7_2_1

modified makefile
parent 39996bdc
......@@ -77,6 +77,14 @@
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
<target name="java_exercises7_2_1_HelloJNI.h" path="jni" targetID="org.eclipse.cdt.build.MakeTargetBuilder">
<buildCommand>make</buildCommand>
<buildArguments/>
<buildTarget>java_exercises7_2_1_HelloJNI.h</buildTarget>
<stopOnError>true</stopOnError>
<useDefaultCommand>true</useDefaultCommand>
<runAllBuilders>true</runAllBuilders>
</target>
</buildTargets>
</storageModule>
</cproject>
#include <jni.h>
#include <stdio.h>
#include "java_exercises7_2_1_HelloJNI.h"
// Implementation of native method sayHello() of HelloJNI class
JNIEXPORT void JNICALL Java_java_1exercises7_11_11_HelloJNI_sayHello(JNIEnv *env, jobject thisObj) {
printf("Hello World!\n");
return;
}
/*
* Class: java_exercises7_2_1_HelloJNI
* Method: intMethod
* Signature: (I)I
*/
JNIEXPORT jint JNICALL Java_java_1exercises7_12_11_HelloJNI_intMethod
(JNIEnv *env, jobject thisObj, jint n) {
return n*n;
}
/*
* Class: java_exercises7_2_1_HelloJNI
* Method: booleanMethod
* Signature: (Z)Z
*/
JNIEXPORT jboolean JNICALL Java_java_1exercises7_12_11_HelloJNI_booleanMethod
(JNIEnv *env, jobject thisObj, jboolean bool) {
return !bool;
}
/*
* Class: java_exercises7_2_1_HelloJNI
* Method: stringMethod
* Signature: (Ljava/lang/String;)Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_java_1exercises7_12_11_HelloJNI_stringMethod
(JNIEnv *env, jobject thisObj, jstring text) {
// Step 1: Convert the JNI String (jstring) into C-String (char*)
char *inCStr = (char*)(*env)->GetStringUTFChars(env, text, NULL);
if (NULL == inCStr) return NULL;
// Step 2: Perform its intended operations
printf("In C, the received string is: %s\n", inCStr);
inCStr[0]++;
jstring returnToken=(*env)->NewStringUTF(env, inCStr);
(*env)->ReleaseStringUTFChars(env, text, inCStr); // release resources
return returnToken;
}
......@@ -5,8 +5,9 @@ JAVA_HOME=C:\Program Files (x86)\Java\jdk1.8.0_131
# Define a virtual path for .class in the bin directory
vpath %.class $(CLASS_PATH)
all : hello.dll
all : hello.dll java_exercises7_2_1_HelloJNI.dll
# Targets for java_exercises_7_1_1
# $@ matches the target, $< matches the first dependancy
hello.dll : HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $<
......@@ -18,6 +19,18 @@ HelloJNI.o : HelloJNI.c HelloJNI.h
# $* matches the target filename without the extension
HelloJNI.h : java_exercises7_1_1/HelloJNI.class
javah -o HelloJNI.h -classpath $(CLASS_PATH) java_exercises7_1_1.$*
# Targets for java_exercises_7_2_1
# $@ matches the target, $< matches the first dependancy
java_exercises7_2_1_HelloJNI.dll : java_exercises7_2_1_HelloJNI.o
gcc -Wl,--add-stdcall-alias -shared -o $@ $<
# $@ matches the target, $< matches the first dependancy
java_exercises7_2_1_HelloJNI.o : java_exercises7_2_1_HelloJNI.c java_exercises7_2_1_HelloJNI.h
gcc -I"$(JAVA_HOME)\include" -I"$(JAVA_HOME)\include\win32" -c $< -o $@
java_exercises7_2_1_HelloJNI.h : java_exercises7_2_1/HelloJNI.class
javah -classpath $(CLASS_PATH) java_exercises7_2_1.HelloJNI
clean :
rm HelloJNI.h HelloJNI.o hello.dll
\ No newline at end of file
rm HelloJNI.h HelloJNI.o hello.dll java_exercises7_2_1_HelloJNI.dll java_exercises7_2_1_HelloJNI.h java_exercises7_2_1_HelloJNI.o
\ No newline at end of file
package java_exercises7_2_1;
public class HelloJNI {
static {
System.loadLibrary("java_exercises7_2_1_HelloJNI"); // Load native library at runtime
}
// Declare a native method sayHello() that receives nothing and returns void
private native void sayHello();
public native int intMethod(int n);
public native boolean booleanMethod(boolean bool);
public native String stringMethod(String text);
// Test Driver
public static void main(String[] args) {
HelloJNI helloJNI=new HelloJNI();
System.out.println("returned native nr: "+helloJNI.intMethod(2));
System.out.println("Returned boolean value: "+helloJNI.booleanMethod(true));
System.out.println("Returned string value: "+helloJNI.stringMethod("Test String"));
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment