{"id":202565,"date":"2025-05-22T09:04:53","date_gmt":"2025-05-22T01:04:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202565\/"},"modified":"2025-05-22T09:04:53","modified_gmt":"2025-05-22T01:04:53","slug":"%e5%a6%82%e4%bd%95%e8%b0%83%e7%94%a8-java-%e6%96%b9%e6%b3%95%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202565\/","title":{"rendered":"\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f<\/h1>\n<p>\u600e\u4e48\u5165\u95e8\u6587\u7ae0\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300a\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f\u300b\uff0c\u6d89\u53ca\u5230\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1a\u5b89\u88c5 JDK\u3001SWIG\u3002\u7f16\u8bd1 Java \u7c7b\u5e76\u751f\u6210 JNI \u5934\u6587\u4ef6\u3002\u4f7f\u7528 SWIG \u751f\u6210 JNI \u5305\u88c5\u5668\u4ee3\u7801\u3002\u5728 Python \u4e2d\u52a0\u8f7d JNI \u5e93\u3002\u4f7f\u7528 autoclass \u52a0\u8f7d Java \u7c7b\u5e76\u8c03\u7528\u65b9\u6cd5\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729918337671c75818364d.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f<\/h2>\n<p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u6211\u4eec\u9700\u8981\u4ece Python \u4e2d\u8c03\u7528 Java \u65b9\u6cd5\u3002\u8fd9\u53ef\u4ee5\u901a\u8fc7 Java Native Interface (JNI) \u6765\u5b9e\u73b0\u3002JNI \u662f\u4e00\u79cd\u5141\u8bb8 Python \u4e0e Java \u5e93\u4ea4\u4e92\u7684 Java \u672c\u673a\u5e93\u3002<\/p>\n<h3>\u5148\u51b3\u6761\u4ef6<\/h3>\n<ul>\n<li>\u5b89\u88c5 Java Development Kit (JDK)<\/li>\n<li>\u5b89\u88c5 SWIG (Simplified Wrapper and Interface Generator)<\/li>\n<\/ul>\n<h3>\u5b89\u88c5 SWIG<\/h3>\n<p>\u5728\u7ec8\u7aef\u4e2d\u8fd0\u884c\u4ee5\u4e0b\u547d\u4ee4\uff1a<\/p>\n<pre>sudo apt-get install swig<\/pre>\n<h3>\u7f16\u8bd1 Java \u7c7b<\/h3>\n<p>\u521b\u5efa Java \u7c7b <code>NativeCode.java<\/code>\uff1a<\/p>\n<pre>public class NativeCode {\n    public static int add(int a, int b) {\n        return a + b;\n    }\n}<\/pre>\n<p>\u7136\u540e\u7f16\u8bd1\u5b83\uff1a<\/p>\n<pre>javac NativeCode.java<\/pre>\n<h3>\u751f\u6210 JNI \u5934\u6587\u4ef6<\/h3>\n<p>\u4f7f\u7528 SWIG \u751f\u6210 JNI \u5934\u6587\u4ef6\uff1a<\/p>\n<pre>swig -java -o NativeCode.i NativeCode.java<\/pre>\n<h3>\u751f\u6210\u5305\u88c5\u5668\u4ee3\u7801<\/h3>\n<p>\u751f\u6210 JNI \u5305\u88c5\u5668\u4ee3\u7801\uff1a<\/p>\n<pre>swig -java NativeCode.i<\/pre>\n<h3>\u52a0\u8f7d\u5e93<\/h3>\n<p>\u5728 Python \u811a\u672c\u4e2d\uff0c\u52a0\u8f7d JNI \u5e93\uff1a<\/p>\n<pre>from jnius import autoclass<\/pre>\n<p>\u5176\u4e2d\uff0c<code>autoclass<\/code> \u5141\u8bb8 Python \u81ea\u52a8\u52a0\u8f7d Java \u7c7b\u3002<\/p>\n<h3>\u8c03\u7528 Java \u65b9\u6cd5<\/h3>\n<p>\u4f7f\u7528 <code>autoclass<\/code> \u52a0\u8f7d Java \u7c7b\u548c\u8c03\u7528\u65b9\u6cd5\uff1a<\/p>\n<pre>NativeCode = autoclass('NativeCode')\nresult = NativeCode.add(10, 20)\nprint(result)<\/pre>\n<h3>\u5b9e\u6218\u6848\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 JNI \u4ece Python \u4e2d\u8c03\u7528 Java \u65b9\u6cd5\u7684\u5b9e\u6218\u6848\u4f8b\uff1a<\/p>\n<pre># NativeCode.java\npublic class NativeCode {\n    public static String greet(String name) {\n        return \"Hello, \" + name + \"!\";\n    }\n}\n\n# NativeCode.i (JNI \u5934\u6587\u4ef6)\n%module NativeCode\n{\n    static {\n        System.loadLibrary(\"NativeCode\");\n    }\n\n    public class NativeCodeJNI {\n        public static native String greet(String name);\n    }\n}\n\n# greeting.py (Python \u811a\u672c)\nfrom jnius import autoclass\nimport os\n\n# \u52a0\u8f7d JNI \u5e93\nnative_lib_path = os.path.join(os.path.dirname(__file__), \"libNativeCode.so\")\nSystem = autoclass('java.lang.System')\nSystem.load(native_lib_path)\n\n# \u52a0\u8f7d Java \u7c7b\nNativeCode = autoclass('NativeCode')\n\n# \u8c03\u7528 Java \u65b9\u6cd5\ngreeting = NativeCode.greet(\"John\")\nprint(greeting)<\/pre>\n<p>\u8fd9\u4e2a Python \u811a\u672c\u4f7f\u7528 SWIG \u751f\u6210\u7684 JNI \u5934\u6587\u4ef6\u548c\u5305\u88c5\u5668\u4ee3\u7801\u4ece Python \u4e2d\u8c03\u7528 Java \u4e2d\u7684 <code>greet<\/code> \u65b9\u6cd5\u3002<\/p>\n<p>\u4eca\u5929\u5173\u4e8e\u300a\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ejava\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u8c03\u7528 Java \u65b9\u6cd5\uff1f \u600e\u4e48&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-202565","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202565","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=202565"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202565\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202565"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202565"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202565"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}