zoukankan      html  css  js  c++  java
  • Java 之 native:JNI 本地方法测试实现

    native 本地方法的实现

    1.先编写java文件

    package com;
    
    public class funcdll {
        static {
    //        System.load(System.getProperty("user.dir") + File.separator + "funmy.dll");
            System.load("E:\ku_code\VS2019\funmy\funmy\x64\Debug\funmy.dll");
        }
        public static void main(String[] args) {
    
            String c_data = "as1231231111112";
            System.out.println("ok start dll function");
            funcdll f =  new funcdll();
            c_data = f.getdll(c_data);
            System.out.println(c_data);
            System.out.println(c_data.length());
    
        }
        public native String getdll(String name);
    }
    
    

    2.编译java文件

    //编译
    java HelloWorld.java
    //生成 .h 头文件,这句注意目录的位置,需要在 com所在的 上一层目录。
    javah -jni com.day01231.HelloWorld
    
    //会生成一个   com_day01231_HelloWorld.h   文件
    

    com_day01231_HelloWorld.h 内容,不用做任何修改

    /* DO NOT EDIT THIS FILE - it is machine generated */
    #include <jni.h>
    /* Header for class com_day01231_HelloWorld */
    
    #ifndef _Included_com_day01231_HelloWorld
    #define _Included_com_day01231_HelloWorld
    #ifdef __cplusplus
    extern "C" {
    #endif
    /*
     * Class:     com_day01231_HelloWorld
     * Method:    sayHello
     * Signature: (Ljava/lang/String;)V
     */
    JNIEXPORT void JNICALL Java_com_day01231_HelloWorld_sayHello
      (JNIEnv *, jobject, jstring);
    
    #ifdef __cplusplus
    }
    #endif
    #endif
    
    

    3.新建一个DLL 工程,工程命名,就叫 dll的名字。 hello

    4.引入jdk中的 jni.h jni_md.h 2个文件,在工程中的 【头文件】目录中

    5.新建一个cpp 文件,在【源文件】中

    #include <iostream>
    #include "pch.h"
    #include "com_day01231_HelloWorld.h"   // 需要引入 com_day01231_HelloWorld.h
    
    using namespace std;
    
    JNIEXPORT void JNICALL Java_com_day01231_HelloWorld_sayHello
    (JNIEnv* env, jobject jobj, jstring jstr)
    {
    	const char* str;
    	str = env->GetStringUTFChars(jstr, JNI_FALSE);  //JNI 中的 false 是 JNI_FALSE
    	cout << str << endl;
    	env->ReleaseStringUTFChars(jstr, str);
    }
    

    6.编译生成。使用的机器如果是 64位,需要选择 x64 ,否则只能在 32 位机器上使用

    7.把生成的 dll 拷贝到 java 引入的路径,运行java 测试。

    8.其他笔记

    C代码:
       JNIEXPORT jstring JNICALL Java_Sample1_stringMethod(JNIEnv *env, jobject obj, jstring string)
          {
             const char *str = (*env)->GetStringUTFChars(env, string, 0);
             char cap[128];
             strcpy(cap, str);
             (*env)->ReleaseStringUTFChars(env, string, str);
           int i=0;
           for(i=0;i
             *(cap+i)=(char)toupper(*(cap+i));
           return (*env)->NewStringUTF(env, cap);
        }
    C++代码:
    
       JNIEXPORT jstring JNICALL Java_Sample1_stringMethod(JNIEnv *env, jobject obj, jstring string)
          {
             const char *str = (env)->GetStringUTFChars(string, 0);
             char cap[128];
             strcpy(cap, str);
             (env)->ReleaseStringUTFChars(string, str);
           int i=0;
           for(i=0;i
             *(cap+i)=(char)toupper(*(cap+i));
           return (env)->NewStringUTF(cap);
        }
    
    
    
    
    
    
    
    
    

    https://blog.csdn.net/xlxxcc/article/details/51106721

    加密
    https://blog.csdn.net/zwc2xm/article/details/52253733

    
    
    #include "pch.h"
    #include "com_funcdll.h"
    
    #include <iostream>
    #include <string>
    
    using namespace std;
    void encryption(string& c, int a[]);
    void decode(string& c, int a[]);
    JNIEXPORT jstring JNICALL Java_com_funcdll_getdll
    (JNIEnv* env, jobject job, jstring jstr) {
    
        const char* c_data = env->GetStringUTFChars(jstr, 0);
    
        char cc_data[sizeof(c_data)];
    
        int a[] = { 4, 9, 6, 2, 8, 7, 3 };
    
        char result[1024];
        //数据处理
        strcpy_s(result, c_data);
    
        string str;
    
        str = result;
        memset(result, '', sizeof(result));
        encryption(str, a);
    
        for (int i = 0; i < str.length(); i++) {
            result[i] = str[i];
        }
    
        cout << "sizeof(result)=" << sizeof(result) << endl;
    
    
        env->ReleaseStringUTFChars(jstr, c_data);
        return env->NewStringUTF(result);
    }
    void encryption(string& c, int a[]) {
        for (int i = 0, j = 0; c[j]; j++, i = (i + 1) % 7) {
            c[j] += a[i];
            if (c[j] > 122) c[j] -= 90;
        }
    }
    void decode(string& c, int a[]) {
        for (int i = 0, j = 0; c[j]; j++, i = (i + 1) % 7) {
            c[j] -= a[i];
            if (c[j] < 32) c[j] += 90;
        }
    }
     
     
    
    
    

    java 打jar 包,也能实现 类似功能

    package com;
    
    public class encodefunc {
        public static String myencode(String s){
            int a[] = { 4, 9, 6, 2, 8, 7, 3 };
            char[] res = new char[s.length()];
    
            for (int i = 0, j = 0; j < s.length(); j++, i = (i + 1) % 7) {
                res[j] =  (char)((byte)s.charAt(j) + a[i]);
                if ((byte)res[j] > 122) res[j] =(char)((byte)res[j] - 90);
            }
            return String.valueOf(res);
        }
    }
    
    public class decodefunc {
        public static String mydecode(String s){
            int a[] = { 4, 9, 6, 2, 8, 7, 3 };
            char[] res = new char[s.length()];
    
            for (int i = 0, j = 0; j < s.length(); j++, i = (i + 1) % 7) {
                res[j] =  (char)((byte)s.charAt(j) - a[i]);
                if ((byte)res[j] < 32) res[j] =(char)((byte)res[j] + 90);
            }
            return String.valueOf(res);
        }
    }
    
    
    
    
    
  • 相关阅读:
    mac的端口被占用
    php中的运算符、控制结构
    文档模式影响浏览器的渲染
    ubuntu 命令 tips 来自于 ubuntu中文论坛
    用好 Emacs 中的 register
    [转载] Rsync命令参数详解
    使用 python 遍历目录下的文件
    灵活的左移位( << )操作
    使用 iperf 测试两台机器间的最大带宽
    Emacs server 新启动方式 (仅在emacs daemon未启动时才启动daemon)
  • 原文地址:https://www.cnblogs.com/kutsu/p/14319990.html
Copyright © 2011-2022 走看看