Recent developments have used the ability to invoke the native dynamic connection library using Java, and the file path is invoked through Java to invoke C + + code to manipulate the file.
A failure is assumed in the call to include Chinese characters in the path. The execution of the program is aborted.
The following is a sample to illustrate the documented solution.
A string is passed in Java and the C + + code is called to output the string
public class Commonutil{static{system.loadlibrary ("Nativetest");} Public native static void Print (String str);p ublic static void Main (string args[]) {commonutil.print ("Chinese garbled");}}
Run the Javac Commonutil.java and Javah commonutil two commands. A CommonUtil.h C + + header file is generated. CommonUtil.h source code such as the following
/* Don't EDIT this file-it are machine generated */#include <jni.h>/* Header for class Commonutil */#ifndef _INCLU Ded_commonutil#define _included_commonutil#ifdef __cplusplusextern "C" {#endif/* * Class: commonutil * Method: Print * Signature: (ljava/lang/string;) V */jniexport void Jnicall java_commonutil_print (jnienv *, Jclass, jstring); #ifdef __cplusplus} #endif #endif
Use VS2005 to create a new C + + DLL for project. Add CommonUtil.h to the project. Create a new. cpp file to implement the Java_commonutil_print function. Implementation code such as the following:
#include "CommonUtil.h" #include <iostream>using namespace std; Jniexport void Jnicall java_commonutil_print (jnienv *env, Jclass obj, jstring jstr) {const char *LOCALSTR = Env->getstri Ngutfchars (jstr,null); Cout<<localstr<<endl;}
In the compilation need to increase the Java comes with the C + + header file, otherwise like jnienv this kind will not find, I use is jdk1.6, so the "C:\Program files\java\jdk1.6.0_10\include; C:\Program Files\java\jdk1.6.0_10\include\win32 "into the project properties.
Compiled into a DLL. Copy the DLL into the same directory as the. class you just compiled (as a simple test, no package is used.) Assume a slightly different scenario for using the package).
Run the command Java commonutil output such as the following
Now it is completely clear that garbled characters, but the web has to convert Java UTF code to gb2312.
Here is the code for the conversion, code Source: http://blog.csdn.net/yiyaaixuexi/article/details/6173592
char* jstringtowindows (jnienv *env, jstring jstr) {//utf8/16 converted to gb2312 int length = (env)->getstringlength (JSTR) ; Const jchar* JCSTR = (env)->getstringchars (jstr, 0); char* RTN = (char*) malloc (length*2+1); int size = 0; Size = WideCharToMultiByte (CP_ACP, 0, (LPCWSTR) jcstr, Length, RTN, (length*2+1), NULL, NULL); if (size <= 0) return NULL; (env)->releasestringchars (JSTR, jcstr); Rtn[size] = 0; return RTN;}
Change the java_commonutil_print to such as the following:
Jniexport void Jnicall java_commonutil_print (jnienv *env, Jclass obj, jstring jstr) {char *localstr = jstringtowindows (env , jstr); Cout<<localstr<<endl;free (LOCALSTR);}
Once again, the generated DLL is then copied to the directory where the. class resides.
Running Java commonutil
Perform normal
Java calls C + + DLL with garbled language