Create JNI C method
/** @version 1.10 1997-07-01 @author Cay Horstmann */ #include "Printf1.h" #include <stdio.h> JNIEXPORT jint JNICALL Java_Printf1_print(JNIEnv* env, jclass cl, jint width, jint precision, jdouble x) { char fmt[30]; jint ret; sprintf(fmt, "%%%d.%df", width, precision); ret = printf(fmt, x); fflush(stdout); return ret; } /////////////////////////////////////// /* This program is a part of the companion code for Core Java 8th ed. (http://horstmann.com/corejava) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * @version 1.10 1997-07-01 * @author Cay Horstmann */ class Printf1 { public static native int print(int width, int precision, double x); static { System.loadLibrary("Printf1"); } } /////////////////////////////////////// /* This program is a part of the companion code for Core Java 8th ed. (http://horstmann.com/corejava) This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>. */ /** * @version 1.10 1997-07-01 * @author Cay Horstmann */ class Printf1Test { public static void main(String[] args) { int count = Printf1.print(8, 4, 3.14); count += Printf1.print(8, 4, count); System.out.println(); for (int i = 0; i < count; i++) System.out.print("-"); System.out.println(); } }