- src/tfw/rsch/jni/MainTest.java
,启动程序,启动用于测试 JNI 加载与调用的函数:
/**
* This file is a component of FREE SOFTWARE.<br />
* <br />
* Any entity with self-awareness, or organization are allowed to use, modify or
* redistribute this according to the second version of "GNU Public License".<br />
* <br />
* The goal of this distribution is that this software being useful and helpful,
* FOR A PARTICULAR PURPOSE.<br />
*/
package tfw.rsch.jni;
import tfw.base.util.array.ArrayToolE;
import tfw.base.util.text.TextToolE;
/**
* Main class, to run a test method of JNI loading and calling.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-33
*/
public class MainTest
{
/**
* Main method, to run a test of JNI loading and calling.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-04-09_15-54
* @param str1dCmdArgs
* - Command line arguments.<br />
*/
public static void main(String[] str1dCmdArgs)
{
new MainTest().test(str1dCmdArgs);
}
/**
* General test method.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-29
* @param str1dArgs
* - Arguments.<br />
*/
private void test(String[] str1dArgs)
{
try
{
// [S] Pre-processing incoming arguments into a single string.
String strArgText = ArrayToolE.arrayForConsole(str1dArgs);
if (null == strArgText)
{
strArgText = "";
}
else if ("null".equals(strArgText))
{
strArgText = null;
}
// [E] Pre-processing incoming arguments into a single string.
test_01();
test_02(strArgText);
test_03(strArgText);
}
catch (Throwable t)
{
t.printStackTrace();
}
}
/**
* Test 01: Basic loading and calling of the native library.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-05-26_16-26
*/
private void test_01()
{
// Loading native library.
new JniTest_01_Loader();
// Calling native method of native library.
JniTest_01_Caller jt_01 = new JniTest_01_Caller();
jt_01.println();
jt_01.nativePrintln();
}
/**
* Test 02: <strong>Argument</strong> passing in and <strong>return
* value</strong> retrieving.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-29
* @param strArgText
* - Text argument to the native library.<br />
*/
private void test_02(String strArgText)
{
// Loading native library during class initializing.
JniTest_02_LoadAndCall jt_02 = new JniTest_02_LoadAndCall();
{
String strMethodHead =
TextToolE.concat("\tpublic String jt_02.javaManipulate(",
((null == strArgText) ? "null"
: ("\"" + strArgText + "\"")) + ")\n\t{");
System.out.println(strMethodHead);
// Calling the java method.
String strRst = jt_02.javaManipulate(strArgText);
String strMethodTail =
TextToolE
.concat("\t}\n\tGot Return Value: ",
((null == strRst) ? "null"
: ("\"" + strRst + "\"")));
System.out.println(strMethodTail);
}
System.out.println();
{
String strMethodHead =
TextToolE.concat(
"\tpublic native String jt_02.nativeManipulate(",
((null == strArgText) ? "null"
: ("\"" + strArgText + "\"")) + ")\n\t{");
System.out.println(strMethodHead);
// Calling the native method, here!
String strOut = jt_02.nativeManipulate(strArgText);
String strMethodTail =
TextToolE
.concat("\t}\n\tGot Return Value: ",
((null == strOut) ? "null"
: ("\"" + strOut + "\"")));
System.out.println(strMethodTail);
}
}
/**
* Test 03: Calling to the <u><strong>pure local native library</strong>
* that without any relationship to JNI or java</u> through the
* <u><strong>native JNI-bridge library</strong></u>.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-29
* @param strArgText
* - Text argument to the native library.<br />
*/
private void test_03(String strArgText)
{
// Loading native library during class initializing.
JniTest_03_ToPureLocal jt_03 = new JniTest_03_ToPureLocal();
{
String strMethodHead =
TextToolE.concat("\tpublic String jt_03.javaManipulate(",
((null == strArgText) ? "null"
: ("\"" + strArgText + "\"")) + ")\n\t{");
System.out.println(strMethodHead);
// Calling the java method.
String strRst = jt_03.javaManipulate(strArgText);
String strMethodTail =
TextToolE
.concat("\t}\n\tGot Return Value: ",
((null == strRst) ? "null"
: ("\"" + strRst + "\"")));
System.out.println(strMethodTail);
}
System.out.println();
{
String strMethodHead =
TextToolE.concat(
"\tpublic native String jt_03.nativeManipulate(",
((null == strArgText) ? "null"
: ("\"" + strArgText + "\"")) + ")\n\t{");
System.out.println(strMethodHead);
// Calling the native method, here!
String strOut = jt_03.nativeManipulate(strArgText);
String strMethodTail =
TextToolE
.concat("\t}\n\tGot Return Value: ",
((null == strOut) ? "null"
: ("\"" + strOut + "\"")));
System.out.println(strMethodTail);
}
}
}
- src/tfw/rsch/jni/JniTest_01_Loader.java
,示例 01 - JNI 本地库加载:
/**
* This file is a component of FREE SOFTWARE.<br />
* <br />
* Any entity with self-awareness, or organization are allowed to use, modify or
* redistribute this according to the second version of "GNU Public License".
* <br />
* <br />
* The goal of this distribution is that this software being useful and helpful,
* FOR A PARTICULAR PURPOSE.<br />
*/
package tfw.rsch.jni;
/**
* Test 01:<br />
* This class loads the native library (shared object, or dynamic link library,
* and so on) during class initializing.<br />
*
* @author Typhoon.Free.Wolf
* @version 2017-03-25_16-00
*/
public class JniTest_01_Loader
{
/**
* Make sure to run during class initializing.<br />
*/
static
{
loadNativeLibrary();
}
/**
* Loads the native library (shared object, or dynamic link library, and so
* on).<br />
*
* @author Typhoon.Free.Wolf
* @version 2017-03-25_16-00
*/
private static void loadNativeLibrary()
{
// Loading native library file "libJniTest_01_ThisNameIsOK.so" (or
// "JniTest_01_ThisNameIsOK.dll", and so on) according to the library
// name "JniTest_01_ThisNameIsOK".
String strNativeLibraryName = "JniTest_01_ThisNameIsOK";
System.out.println("Loading \"" + strNativeLibraryName + "\"...");
System.loadLibrary(strNativeLibraryName); // <== Loading, here!
System.out.println("Loaded.");
}
}
- src/tfw/rsch/jni/JniTest_01_Caller.java
,示例 01 - JNI 本地库调用:
/**
* This file is a component of FREE SOFTWARE.<br />
* <br />
* Any entity with self-awareness, or organization are allowed to use, modify or
* redistribute this according to the second version of "GNU Public License".<br />
* <br />
* The goal of this distribution is that this software being useful and helpful,
* FOR A PARTICULAR PURPOSE.<br />
*/
package tfw.rsch.jni;
/**
* Test 01:<br />
* This class provides an entrance to the native method in the native library
* (shared object, or dynamic link library, and so on) for java calling.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-05-26_16-30
*/
public class JniTest_01_Caller
{
/**
* A java method, prints a message to the standard-out.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-04-11_14-09
*/
public void println()
{
System.out.println("\tJava:\tWorks!");
}
/**
* Entrance to the native method in the native library (shared object, or
* dynamic link library, and so on).<br />
* The corresponding native method is supposed to print a message to the
* standard-out, just like what the java one above does.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-04-11_14-09
*/
public native void nativePrintln();
}
- src/tfw/rsch/jni/JniTest_02_LoadAndCall.java
,示例 02 - 加载和调用 JNI 本地库,进行控制台 IO 操作:
/**
* This file is a component of FREE SOFTWARE.<br />
* <br />
* Any entity with self-awareness, or organization are allowed to use, modify or
* redistribute this according to the second version of "GNU Public License".<br />
* <br />
* The goal of this distribution is that this software being useful and helpful,
* FOR A PARTICULAR PURPOSE.<br />
*/
package tfw.rsch.jni;
import tfw.base.util.misc.MiscToolE;
import tfw.base.util.text.TextToolE;
/**
* Test 02: This class<br />
* <ul>
* <li>Loads the native library (shared object, or dynamic link library, and so
* on) during class initializing;<br />
* </li>
* <li>Provides an entrance to the native method in the native library.<br />
* </li>
* </ul>
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-38
*/
public class JniTest_02_LoadAndCall
{
/**
* Make sure to run during class initializing.<br />
*/
static
{
loadNativeLibrary();
}
/**
* Loads the native library (shared object, or dynamic link library, and so
* on).<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-05-26_15-58
*/
private static void loadNativeLibrary()
{
// Loading native library file "libJniTest_02.so" (or "JniTest_02.dll",
// and so on) according to the library name "JniTest_02".
String strNativeLibraryName = "JniTest_02";
System.out.println("Loading \"" + strNativeLibraryName + "\"...");
System.loadLibrary(strNativeLibraryName); // <== Loading, here!
System.out.println("Loaded.");
}
/**
* A java method.<br />
* Prints the incoming text string to the standard-out, then receives the
* text string from the standard-input and finally returns it.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-36
* @param strText
* - Incoming text string.<br />
* @return A text string from the standard-input, <strong>could be
* null.</strong><br />
*/
public String javaManipulate(String strText)
{
// Printing the received argument.
String strReceiveText =
TextToolE.concat("\t\tJava:\n\n\t\tArgument:\n\t\t\t",
(null == strText) ? "null" : ("\"" + strText + "\""));
System.out.println(strReceiveText);
// [S] Receiving a text string for standard input.
System.out
.print("\t\tUser Input (\"null\" would be considered as null):\n\t\t\t");
String strUserInputText = MiscToolE.getline(System.in);
System.out.println("\t\tUser Input Received:\n\t\t\t"
+ ((null == strUserInputText) ? "null" : ("\""
+ strUserInputText + "\"")));
// [E] Receiving a text string for standard input.
// [S] Printing the value which to be returned.
String strReturnValue =
"null".equals(strUserInputText) ? null : strUserInputText;
System.out.println("\t\tReturns:\n\t\t\t"
+ ((null == strReturnValue) ? "null"
: ("\"" + strReturnValue + "\"")));
// [E] Printing the value which to be returned.
// Returning.
return strReturnValue;
}
/**
* Entrance to the native method in the native library (shared object, or
* dynamic link library, and so on).<br />
* The corresponding native method is supposed to print the incoming text
* string to the standard-out, then receive the text string from the
* standard-input and finally return it, just like what the java one above
* does.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-04-11_14-10
* @param strText
* - Incoming text string.<br />
* @return A text string actually returned from the native method,
* <strong>could be null.</strong><br />
*/
public native String nativeManipulate(String strText);
}
- src/tfw/rsch/jni/JniTest_03_ToPureLocal.java
,示例 03 - 通过加载和调用 JNI 本地库进一步调用一个与 Java 无关的纯本地库,进行控制台 IO 操作:
/**
* This file is a component of FREE SOFTWARE.<br />
* <br />
* Any entity with self-awareness, or organization are allowed to use, modify or
* redistribute this according to the second version of "GNU Public License".<br />
* <br />
* The goal of this distribution is that this software being useful and helpful,
* FOR A PARTICULAR PURPOSE.<br />
*/
package tfw.rsch.jni;
import tfw.base.util.misc.MiscToolE;
import tfw.base.util.text.TextToolE;
/**
* Test 03: This class<br />
* <ul>
* <li>Loads the native library (shared object, or dynamic link library, and so
* on) during class initializing;<br />
* </li>
* <li>Provides an entrance to the native method in the <strong><u>native
* JNI-bridge library</u></strong>.<br />
* </li>
* <li>The <u><strong>native JNI-bridge library</strong></u> is to call another
* <u><strong>pure, local native library</strong> that without any relationship
* to JNI or java</u>.<br />
* </li>
* </ul>
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-40
*/
public class JniTest_03_ToPureLocal
{
/**
* Make sure to run during class initializing.<br />
*/
static
{
loadNativeLibrary();
}
/**
* Loads the native library (shared object, or dynamic link library, and so
* on).<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-05-26_15-58
*/
private static void loadNativeLibrary()
{
// Loading native library file "libJniTest_03.so" (or "JniTest_03.dll",
// and so on) according to the library name "JniTest_03".
String strNativeLibraryName = "JniTest_03";
System.out.println("Loading \"" + strNativeLibraryName + "\"...");
System.loadLibrary(strNativeLibraryName); // <== Loading, here!
System.out.println("Loaded.");
}
/**
* A java method.<br />
* Prints the incoming text string to the standard-out, then receives the
* text string from the standard-input and finally returns it.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-06-25_18-36
* @param strText
* - Incoming text string.<br />
* @return A text string from the standard-input, <strong>could be
* null.</strong><br />
*/
public String javaManipulate(String strText)
{
// Printing the received argument.
String strReceiveText =
TextToolE.concat("\t\tJava:\n\n\t\tArgument:\n\t\t\t",
(null == strText) ? "null" : ("\"" + strText + "\""));
System.out.println(strReceiveText);
// [S] Receiving a text string for standard input.
System.out
.print("\t\tUser Input (\"null\" would be considered as null):\n\t\t\t");
String strUserInputText = MiscToolE.getline(System.in);
System.out.println("\t\tUser Input Received:\n\t\t\t"
+ ((null == strUserInputText) ? "null" : ("\""
+ strUserInputText + "\"")));
// [E] Receiving a text string for standard input.
// [S] Printing the value which to be returned.
String strReturnValue =
"null".equals(strUserInputText) ? null : strUserInputText;
System.out.println("\t\tReturns:\n\t\t\t"
+ ((null == strReturnValue) ? "null"
: ("\"" + strReturnValue + "\"")));
// [E] Printing the value which to be returned.
// Returning.
return strReturnValue;
}
/**
* Entrance to the native method in the native library (shared object, or
* dynamic link library, and so on).<br />
* The corresponding native method is supposed to print the incoming text
* string to the standard-out, then receive the text string from the
* standard-input and finally return it, just like what the java one above
* does.<br />
*
* @author Typhoon.Free.Wolf
* @version 2015-04-11_14-10
* @param strText
* - Incoming text string.<br />
* @return A text string actually returned from the native method,
* <strong>could be null.</strong><br />
*/
public native String nativeManipulate(String strText);
}