{"id":202676,"date":"2025-05-22T09:49:40","date_gmt":"2025-05-22T01:49:40","guid":{"rendered":"https:\/\/server.hk\/cnblog\/202676\/"},"modified":"2025-05-22T09:49:40","modified_gmt":"2025-05-22T01:49:40","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8java%e4%b8%ad%e5%ae%9a%e4%b9%89-native-%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/202676\/","title":{"rendered":"\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49 native \u51fd\u6570"},"content":{"rendered":"<p><b><\/b>     <\/p>\n<h1>\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49 native \u51fd\u6570<\/h1>\n<p>\u79ef\u7d2f\u77e5\u8bc6\uff0c\u80dc\u8fc7\u79ef\u84c4\u91d1\u94f6\uff01\u6bd5\u7adf\u5728\u6587\u7ae0\u5f00\u53d1\u7684\u8fc7\u7a0b\u4e2d\uff0c\u4f1a\u9047\u5230\u5404\u79cd\u5404\u6837\u7684\u95ee\u9898\uff0c\u5f80\u5f80\u90fd\u662f\u4e00\u4e9b\u7ec6\u8282\u77e5\u8bc6\u70b9\u8fd8\u6ca1\u6709\u638c\u63e1\u597d\u800c\u5bfc\u81f4\u7684\uff0c\u56e0\u6b64\u57fa\u7840\u77e5\u8bc6\u70b9\u7684\u79ef\u7d2f\u662f\u5f88\u91cd\u8981\u7684\u3002\u4e0b\u9762\u672c\u6587\u300a\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49 native \u51fd\u6570\u300b\uff0c\u5c31\u5e26\u5927\u5bb6\u8bb2\u89e3\u4e00\u4e0b\u77e5\u8bc6\u70b9\uff0c\u82e5\u662f\u4f60\u5bf9\u672c\u6587\u611f\u5174\u8da3\uff0c\u6216\u8005\u662f\u60f3\u641e\u61c2\u5176\u4e2d\u67d0\u4e2a\u77e5\u8bc6\u70b9\uff0c\u5c31\u8bf7\u4f60\u7ee7\u7eed\u5f80\u4e0b\u770b\u5427~<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729931003671ca6fb38f80.jpg\" class=\"aligncenter\"><\/p>\n<h2>\u5982\u4f55\u5728 Java \u4e2d\u5b9a\u4e49 native \u51fd\u6570<\/h2>\n<p><strong>\u7b80\u4ecb<\/strong><\/p>\n<p>native \u51fd\u6570\u662f Java \u7f16\u7a0b\u8bed\u8a00\u4e2d\u7684\u4e00\u79cd\u7279\u6b8a\u51fd\u6570\u7c7b\u578b\uff0c\u5b83\u5141\u8bb8 Java \u4ee3\u7801\u8c03\u7528\u5176\u4ed6\u7f16\u7a0b\u8bed\u8a00\uff08\u5982 C \u6216 C++\uff09\u4e2d\u5b9e\u73b0\u7684\u4ee3\u7801\u3002\u8fd9\u5bf9\u4e8e\u4f7f\u7528 Java \u7f16\u7a0b\u8bed\u8a00\u65f6\u9700\u8981\u8bbf\u95ee\u5e95\u5c42\u5e73\u53f0\u529f\u80fd\u65f6\u975e\u5e38\u6709\u7528\u3002<\/p>\n<p><strong>\u5b9a\u4e49\u8bed\u6cd5<\/strong><\/p>\n<p>\u8981\u5b9a\u4e49\u4e00\u4e2a native \u51fd\u6570\uff0c\u60a8\u9700\u8981\u4f7f\u7528 <code>native<\/code> \u5173\u952e\u5b57\uff0c\u540e\u8ddf\u51fd\u6570\u7b7e\u540d\uff1a<\/p>\n<pre>public native &lt;return type&gt; &lt;function name&gt;(&lt;parameter list&gt;);<\/pre>\n<p>\u4f8b\u5982\uff1a<\/p>\n<pre>public native int sum(int x, int y);<\/pre>\n<p><strong>\u5b9e\u73b0\u7ec6\u8282<\/strong><\/p>\n<p>native \u51fd\u6570\u7684\u5b9e\u73b0\u7ec6\u8282\u7531 Java \u865a\u62df\u673a\uff08JVM\uff09\u51b3\u5b9a\u3002JVM \u901a\u5e38\u4f1a\u5c06 native \u51fd\u6570\u6620\u5c04\u5230\u672c\u5730\u64cd\u4f5c\u7cfb\u7edf\u4e2d\u7684\u5e93\u6216 DLL\u3002\u60a8\u4e0d\u5fc5\u63d0\u4f9b native \u51fd\u6570\u7684\u5b9e\u73b0\uff0c\u56e0\u4e3a\u8be5\u5b9e\u73b0\u662f\u7531 JVM \u5904\u7406\u7684\u3002<\/p>\n<p><strong>\u5b9e\u6218\u6848\u4f8b<\/strong><\/p>\n<p>\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528 native \u51fd\u6570\u5728 Java \u4e2d\u6253\u5370\u7cfb\u7edf\u4fe1\u606f\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>public class NativeExample {\n\n    \/\/ Declare a native method\n    public native void printSystemInfo();\n\n    \/\/ Load the native library\n    static {\n        System.loadLibrary(\"NativeExample\");\n    }\n\n    public static void main(String[] args) {\n        \/\/ Create an instance of the class\n        NativeExample example = new NativeExample();\n\n        \/\/ Call the native method\n        example.printSystemInfo();\n    }\n}<\/pre>\n<p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>printSystemInfo()<\/code> \u65b9\u6cd5\u662f\u4e00\u4e2a native \u51fd\u6570\uff0c\u5b83\u88ab\u58f0\u660e\u4e3a Java \u65b9\u6cd5\uff0c\u4f46\u5b83\u7684\u5b9e\u73b0\u662f\u5728\u5916\u90e8 C\/C++ \u4ee3\u7801\u5e93\u4e2d\u3002\u5f53 <code>example.printSystemInfo()<\/code> \u88ab\u8c03\u7528\u65f6\uff0cJVM \u4f1a\u5c06\u8be5\u8c03\u7528\u91cd\u5b9a\u5411\u5230\u672c\u5730\u4ee3\u7801\uff0c\u8be5\u672c\u5730\u4ee3\u7801\u5c06\u6253\u5370\u7cfb\u7edf\u4fe1\u606f\u3002<\/p>\n<p><strong>\u6ce8\u610f\u4e8b\u9879<\/strong><\/p>\n<ul>\n<li>native \u51fd\u6570\u53ea\u80fd\u88ab\u58f0\u660e\u4e3a <code>public<\/code> \u6216 <code>default<\/code>\u3002<\/li>\n<li>native \u51fd\u6570\u4e0d\u80fd\u88ab\u91cd\u8f7d\u3002<\/li>\n<li>native \u51fd\u6570\u4e0d\u80fd\u88ab\u58f0\u660e\u4e3a <code>static<\/code>\u3002<\/li>\n<li>\u60a8\u9700\u8981\u63d0\u4f9b\u4e00\u4e2a\u4e0e native \u51fd\u6570\u5bf9\u5e94\u7684\u672c\u5730\u4ee3\u7801\u5b9e\u73b0\u3002<\/li>\n<\/ul>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Java\u4e2d\u5b9a\u4e49 nativ&#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-202676","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202676","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=202676"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/202676\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=202676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=202676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=202676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}