- 论坛徽章:
- 19
|
JavaNative的Java类- package com.fangzhaoguo.primefinder;
- public class NativePrimeFinder {
- static {
- System.loadLibrary("libJavaNative_PrimeFinder");
- }
- public NativePrimeFinder() {
- }
- public native boolean isPrime(final long number);
- public native int countPrime(final long number);
- public native int countPrime(final long lower, final long upper);
- }
复制代码 JavaNative的C头文件,这里只是将#include <jni.h>修改成#include "jni.h"- /* DO NOT EDIT THIS FILE - it is machine generated */
- #include "jni.h"
- /* Header for class com_fangzhaoguo_primefinder_NativePrimeFinder */
- #ifndef _Included_com_fangzhaoguo_primefinder_NativePrimeFinder
- #define _Included_com_fangzhaoguo_primefinder_NativePrimeFinder
- #ifdef __cplusplus
- extern "C" {
- #endif
- /*
- * Class: com_fangzhaoguo_primefinder_NativePrimeFinder
- * Method: isPrime
- * Signature: (J)Z
- */
- JNIEXPORT jboolean JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(
- JNIEnv *, jobject, jlong);
- /*
- * Class: com_fangzhaoguo_primefinder_NativePrimeFinder
- * Method: countPrime
- * Signature: (J)I
- */
- JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__J(
- JNIEnv *, jobject, jlong);
- /*
- * Class: com_fangzhaoguo_primefinder_NativePrimeFinder
- * Method: countPrime
- * Signature: (JJ)I
- */
- JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__JJ(
- JNIEnv *, jobject, jlong, jlong);
- #ifdef __cplusplus
- }
- #endif
- #endif
复制代码 JavaNative的C源文件- /*
- * com_fangzhaoguo_primefinder_NativePrimeFinder.c
- *
- * Created on: 2013年7月18日
- * Author: root
- */
- #include "com_fangzhaoguo_primefinder_NativePrimeFinder.h"
- #include <stdio.h>
- #include <math.h>
- JNIEXPORT jboolean JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(
- JNIEnv *env, jobject obj, jlong number) {
- jlong i = 0;
- if (1 >= number) {
- return 0;
- }
- for (i = 2; i <= sqrt(number); i++) {
- if (0 == number % i) {
- return 0;
- }
- }
- return 1;
- }
- JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__J(
- JNIEnv *env, jobject obj, jlong number) {
- jint total = 0;
- jlong i = 0;
- for (i = 2; i < number; i++) {
- if (Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(env, obj,
- i)) {
- total++;
- }
- }
- return total;
- }
- JNIEXPORT jint JNICALL Java_com_fangzhaoguo_primefinder_NativePrimeFinder_countPrime__JJ(
- JNIEnv *env, jobject obj, jlong lower, jlong upper) {
- jint total = 0;
- jlong i = 0;
- for (i = lower; i < upper; i++) {
- if (Java_com_fangzhaoguo_primefinder_NativePrimeFinder_isPrime(env, obj,
- i)) {
- total++;
- }
- }
- return total;
- }
复制代码 |
|