{"id":659719,"date":"2013-02-23T14:08:15","date_gmt":"2013-02-23T06:08:15","guid":{"rendered":"http:\/\/www.sunjw.us\/blog\/?p=659719"},"modified":"2013-02-28T21:44:45","modified_gmt":"2013-02-28T13:44:45","slug":"java-jni-and-notepad-plugin","status":"publish","type":"post","link":"https:\/\/www.sunjw.us\/blog\/java-jni-and-notepad-plugin\/","title":{"rendered":"Java, JNI and Notepad++ Plugin"},"content":{"rendered":"<p>\u6700\u8fd1\u628a\u4e4b\u524d\u5199\u7684 JsFormatter \u7528 Java \u79fb\u690d\u4e86\u4e00\u4e0b\uff0c\u4e8e\u662f\u60f3\u7740\u80fd\u4e0d\u80fd\u7528 JNI \u4f5c\u4e3a\u5305\u88c5\uff0c\u8ba9 Notepad++ \u7684\u63d2\u4ef6\u8c03\u7528 Java \u7248\u7684\u7b97\u6cd5\uff0c\u4e8e\u662f\u6709\u4e86\u4e0b\u9762\u7684\u4e24\u4e2a\u9879\u76ee\uff1a<br \/>\n<a href=\"https:\/\/github.com\/sunjw\/JsToolJava\" target=\"_blank\">https:\/\/github.com\/sunjw\/JsToolJava<\/a><br \/>\n<a href=\"https:\/\/github.com\/sunjw\/JsFormatterJavaNppPlugin\" target=\"_blank\">https:\/\/github.com\/sunjw\/JsFormatterJavaNppPlugin<\/a><\/p>\n<p>\u5148\u8bf4\u4e00\u4e0b\u600e\u4e48\u8dd1\u8fd9\u4e2a\u63d2\u4ef6\uff0c\u5c06 JsFormatterJavaNppPlugin \u7f16\u8bd1\u5f97\u5230\u7684 JSMinJava.dll \u653e\u5230 npp \u7684\u63d2\u4ef6\u76ee\u5f55\uff0c\u4e4b\u540e\u5c06 JsToolJava.jar \u548c commons-lang-2.6.jar \u4e5f\u653e\u5165\u63d2\u4ef6\u76ee\u5f55\uff0c\u6700\u540e\u5c06 %JAVA_HOME%\\bin\\client \u6dfb\u52a0\u5230 PATH \u73af\u5883\u53d8\u91cf\u3002\u8fd9\u6837\u5e94\u8be5\u5c31\u80fd\u8fd0\u884c JSMinJava \u63d2\u4ef6\uff0c\u5176\u4e2d JSFormat \u529f\u80fd\u5c31\u662f\u7528 JNI \u8c03\u7528 Java \u6267\u884c\u7684\u3002\u8fd9\u4e2a\u5728 npp 6.3, JRE 1.6 \u4e0a\u6d4b\u8bd5\u6210\u529f\uff0c\u5176\u4ed6\u914d\u7f6e\u4e0d\u4e00\u5b9a\u80fd\u6b63\u5e38\u5de5\u4f5c\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u5177\u4f53\u8fc7\u7a0b\u662f\u8fd9\u6837\u7684\uff0c\u9996\u5148\u901a\u8fc7 JNI \u542f\u52a8\u4e00\u4e2a JVM<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\nJavaVMOption options&#x5B;1];\r\nJavaVMInitArgs vm_args;\r\nlong status;\r\n\r\noptions&#x5B;0].optionString = &quot;-Djava.class.path=.\\\\plugins\\\\JsToolJava.jar;.\\\\plugins\\\\commons-lang-2.6.jar&quot;;\r\nmemset(&amp;vm_args, 0, sizeof(vm_args));\r\nvm_args.version = JNI_VERSION_1_6;\r\nvm_args.nOptions = 1;\r\nvm_args.options = options;\r\nstatus = JNI_CreateJavaVM(&amp;jvm, (void**)&amp;jenv, &amp;vm_args);\r\n<\/pre>\n<p>\u4e4b\u540e\u901a\u8fc7 JNI \u627e\u5230 org.sunjw.jni.JsfJniWrapper \u8fd9\u4e2a\u7c7b\uff0c\u5e76\u521b\u5efa\u5b9e\u4f8b\uff0c\u8c03\u7528\u6210\u5458\u51fd\u6570\uff0c\u5c06 c \u8bed\u8a00\u8868\u8fbe\u7684\u53c2\u6570\u4f20\u5165\uff0c\u4e4b\u540e\u5f97\u5230\u8fd4\u56de\u7684 Java String\uff0c\u5e76\u5728\u8f6c\u6362\u56de c \u8bed\u8a00\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\r\njclass jcls = jenv-&gt;FindClass(&quot;org\/sunjw\/jni\/JsfJniWrapper&quot;);\r\nif(jcls != 0)\r\n{\r\n  \/\/ We found class!\r\n  \/\/ Now, try to create an instance.\r\n  jobject jinstance = 0;\r\n  jmethodID jClsInit = jenv-&gt;GetMethodID(jcls, &quot;&quot;, &quot;()V&quot;);\r\n  if(jClsInit != 0)\r\n  {\r\n    jinstance = jenv-&gt;NewObject(jcls, jClsInit); \/\/ Create an instance\r\n  }\r\n\r\n  jmethodID jmid = jenv-&gt;GetMethodID(jcls,\r\n                                      &quot;formatJs&quot;,\r\n                                      &quot;(Ljava\/lang\/String;CIZZZ)Ljava\/lang\/String;&quot;);\r\n  if(jinstance != 0 &amp;&amp; jmid != 0)\r\n  {\r\n    \/\/ We have instance and method!\r\n    string strJs(pJS);\r\n    strJs = utf8conv(strJs);\r\n    jstring jJsStr = jenv-&gt;NewStringUTF(strJs.c_str());\r\n    jchar jChIndent = struOptions.chIndent;\r\n    jint jnChPerInd = _nChPerInd;\r\n    jboolean jbPutCR = struOptions.bPutCR ? JNI_TRUE : JNI_FALSE;\r\n    jboolean jbNLBracket = struOptions.bNLBracket ? JNI_TRUE : JNI_FALSE;\r\n    jboolean jbIndentInEmpty = struOptions.bIndentInEmpty ? JNI_TRUE : JNI_FALSE;\r\n\r\n    jstring jretstr = (jstring)jenv-&gt;CallObjectMethod(\r\n              jinstance, jmid,\r\n              jJsStr, jChIndent, jnChPerInd, jbPutCR, jbNLBracket, jbIndentInEmpty);\r\n\r\n    const char *nativeString = jenv-&gt;GetStringUTFChars(jretstr, 0);\r\n    strJSFormat = (const char *)nativeString;\r\n    strJSFormat = asciiconv(strJSFormat);\r\n\r\n    jenv-&gt;ReleaseStringUTFChars(jretstr, nativeString);\r\n    jenv-&gt;DeleteLocalRef(jJsStr);\r\n  }\r\n}\r\n<\/pre>\n<p>\u6d4b\u8bd5\u7684\u65f6\u5019\u53d1\u73b0\u8c8c\u4f3c Java \u7248\u7684\u7b97\u6cd5\u8fd8\u6bd4 Release \u7f16\u8bd1\u7684 c++ \u7248\u7b97\u6cd5\u66f4\u5feb\u4e00\u70b9\uff0cJRE \u7684 JIT \u6027\u80fd\u8fd8\u662f\u76f8\u5f53\u53ef\u4ee5\u7684\u3002<\/p>\n<p>\u7eed &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;<\/p>\n<p>\u53d1\u73b0 Java \u7248\u7684\u7b97\u6cd5\u5728\u7279\u5b9a\u7684\u51e0\u4e2a\u6d4b\u8bd5\u6587\u4ef6\u4e0a\u7a33\u5b9a\u7684\u5feb\u4e8e c++ \u7248\u540e\uff0c\u5f00\u59cb\u7740\u624b\u7814\u7a76\u8fd9\u4e2a\u95ee\u9898\u3002\u901a\u8fc7 <a href=\"http:\/\/www.codersnotes.com\/sleepy\" target=\"_blank\">Very Sleepy<\/a> \u8fd9\u4e2a c++ profiler \u53d1\u73b0\u95ee\u9898\u5728\u4e8e\uff0c\u4e00\u4e2a\u65b9\u4fbf\u64cd\u4f5c stack \u7684\u51fd\u6570\u4e2d\uff0c\u58f0\u660e\u7684\u53c2\u6570\u6ca1\u6709\u4f7f\u7528\u5e38\u91cf\u5f15\u7528\uff08const reference\uff09\uff0c\u5bfc\u81f4 stack \u88ab\u591a\u6b21\u62f7\u8d1d\uff0c\u5c06\u90a3\u4e2a\u53c2\u6570\u6539\u6210\u5e38\u91cf\u5f15\u7528\u540e\uff0cc++ \u7248\u7684\u6027\u80fd\u7ec8\u4e8e\u7a33\u8d85 Java \u7248\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u628a\u4e4b\u524d\u5199\u7684 JsFormatter \u7528 Java \u79fb\u690d\u4e86\u4e00\u4e0b\uff0c\u4e8e\u662f\u60f3\u7740\u80fd\u4e0d\u80fd\u7528 JNI \u4f5c\u4e3a\u5305\u88c5\uff0c\u8ba9 No [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[119,120,352,127,372,152],"class_list":["post-659719","post","type-post","status-publish","format-standard","hentry","category-jsminnpp","tag-java","tag-javascript","tag-jni","tag-jsformat","tag-jsminnpp","tag-notepad"],"_links":{"self":[{"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/posts\/659719","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/comments?post=659719"}],"version-history":[{"count":12,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/posts\/659719\/revisions"}],"predecessor-version":[{"id":659721,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/posts\/659719\/revisions\/659721"}],"wp:attachment":[{"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/media?parent=659719"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/categories?post=659719"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sunjw.us\/blog\/wp-json\/wp\/v2\/tags?post=659719"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}