-
Notifications
You must be signed in to change notification settings - Fork 590
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JavaCPP export Library clone as C++ class files #17
Comments
For example with the help of JavaCPP. |
It wasn't designed for that, no... Check the README.md file for some options. |
I know but its not that hard to do with ANTLR Library |
I'm not sure why ANTLR would help with that. We don't really need to parse anything... Am I missing something? |
so how can you convert Java code to C++ ?? I can help you about library. |
You can call JavaVM inside C++ with the help of JNI Library on C++ |
You can invoke with that code: JavaVM *InitJVM(JNIEnv **env2) {
JavaVM* vm;
JNIEnv* env;
JavaVMInitArgs vm_args;
JavaVMOption options[3];
CHAR lpDataTempClass[MAX_PATH];
CHAR lpchDataLib[MAX_PATH];
TCHAR lpchRuntimelib[MAX_PATH];
TCHAR lpchJavaPath[MAX_PATH];
jobjectArray joApplicationArgs;
*env2 = NULL;
/* disable JIT */
//options[0].optionString ="-Djava.compiler=NONE";
/* user classes */
//"-Djava.class.path=c:\\myclasses"
//Pointer to the function JNI_CreateJavaVM
HKEY key;
BOOL foundDLL;
TCHAR lpchRuntimePath[MAX_PATH];
TCHAR lpchJavaHome[MAX_PATH];
TCHAR lpchThisPath[MAX_PATH];
GetThisPath(lpchThisPath,MAX_PATH);
HRESULT result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, L"Software\\JavaSoft\\Java Runtime Environment", 0, KEY_QUERY_VALUE,&key);
char *lpVal = readStringRegValue(key, "CurrentVersion");
if ( lpVal != NULL ) {
TCHAR lpchJVMRuntime[MAX_PATH];
HKEY subkey;
result = RegOpenKeyExA(key, lpVal, 0, KEY_QUERY_VALUE, &subkey);
if(result == ERROR_SUCCESS) {
readStringRegValueW(subkey, _T("RuntimeLib"), lpchJVMRuntime );
readStringRegValueW(subkey, L"JavaHome", lpchJavaHome);
//lpchJavaHome = readStringRegValue( subkey , "JavaHome" );
if(hinst_JVM == NULL) {
//WCHAR achRuntimePath[MAX_PATH];
//MultiByteToWideChar( CP_ACP , 0 , lpchJVMRuntime , -1 , achRuntimePath , MAX_PATH );
//CHAR *achCustomLib = "C:\\Program Files\\Java\\jre6\\bin\\client\\jvm.dll";
hinst_JVM = LoadLibraryW( lpchJVMRuntime );
DWORD errCode = GetLastError( );
if (!hinst_JVM) {
WCHAR lpchJVMBinPath[MAX_PATH];
WCHAR lpchFullFilePath[MAX_PATH];
WCHAR lpchJavaHomeW[MAX_PATH];
WCHAR lpchClientPath[MAX_PATH];
//hinst_JVM = LoadLibraryA("jvm.dll");
EnumerateSubKeys(HKEY_LOCAL_MACHINE, "Software\\JavaSoft\\Java Runtime Environment", &foundDLL, lpchRuntimePath, lpchJavaHome);
if (foundDLL) {
wsprintfW(lpchJVMBinPath, L"%s\\bin", lpchJavaHome);
wsprintfW(lpchClientPath, L"%s\\bin\\client\\", lpchJavaHome);
CVector<CUIString> values;
listFiles(lpchJVMBinPath, values);
for (int j = 0; j < values.size(); j++) {
CUIString *str = values.getElementAt(j);
WCHAR *value = str->c_str();
if (wcsstr(value, L"msvcr") && wcsstr(value, L".dll")) {
//lpchFullFilePath
WCHAR wchCombinedPath[MAX_PATH];
wsprintfW(wchCombinedPath, L"%s\\%s", lpchJVMBinPath, value);
wcscat(lpchClientPath, value);
CopyFile(wchCombinedPath, lpchClientPath, FALSE);
wcscat(lpchThisPath, _T("\\"));
wcscat(lpchThisPath, value);
CopyFile(wchCombinedPath, lpchThisPath, FALSE);
break;
}
}
}
else {
//no jvm
RegCloseKey(key);
RegCloseKey(subkey);
return NULL;
}
}
//jvm.dll found
else {
WCHAR lpchJVMBinPath[MAX_PATH];
WCHAR lpchFullFilePath[MAX_PATH];
WCHAR lpchJavaHomeW[MAX_PATH];
WCHAR lpchClientPath[MAX_PATH];
wsprintfW(lpchJVMBinPath, L"%s\\bin", lpchJavaHome);
wsprintfW(lpchClientPath, L"%s\\bin\\client\\", lpchJavaHome);
CVector<CUIString> values;
listFiles(lpchJVMBinPath, values);
for (int j = 0; j < values.size(); j++) {
CUIString *str = values.getElementAt(j);
WCHAR *value = str->c_str();
if (wcsstr(value, L"msvcr") && wcsstr(value, L".dll")) {
//lpchFullFilePath
WCHAR wchCombinedPath[MAX_PATH];
wsprintfW(wchCombinedPath, L"%s\\%s", lpchJVMBinPath, value);
wcscat(lpchClientPath, value);
CopyFile(wchCombinedPath, lpchClientPath, FALSE);
wcscat(lpchThisPath, _T("\\"));
wcscat(lpchThisPath, value);
CopyFile(wchCombinedPath, lpchThisPath , FALSE);
break;
}
}
}
}
RegCloseKey(subkey);
}
} else {
//NO JVM Installed
}
RegCloseKey(key);
if(hinst_JVM == NULL) {
return NULL;
}
//java -Djava.library.path=C:\Users\Kadir\IdeaProjects\WebcamTransmitter\nativeLib\windows\x64 -cp logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar service.WebcamCapture
//java -Djava.library.path=C:\Users\Kadir\IdeaProjects\WebcamTransmitter\nativeLib\windows\x86 -cp logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar service.WebcamCapture
//Get the address of the function
pfnCreateJavaVM = (CreateJavaVM_t*)GetProcAddress(hinst_JVM, "JNI_CreateJavaVM");
pfnGetDefaultJavaVMOptions = (OptionsJavaVM_t*)GetProcAddress(hinst_JVM, "JNI_GetDefaultJavaVMInitArgs");
//-Djava.class.path
//"logback-classic-1.1.3.jar;logback-core-1.1.3.jar;WebcamTransmitter-1.0-SNAPSHOT.jar;bridj-0.6.2.jar;ffmpeg-2.6.1-0.11.jar;ffmpeg-2.6.1-0.11-windows-x86_64.jar;javacpp-0.11.jar;webcam-capture-0.3.10.jar;slf4j-api-1.7.2.jar"
CHAR achTemp[MAX_PATH];
{
TCHAR achTemp2[MAX_PATH];
GetTempPath(MAX_PATH, achTemp2);
int sizeNeed = WideCharToMultiByte(CP_UTF8, 0, achTemp2, MAX_PATH, achTemp, 0, NULL, NULL);
WideCharToMultiByte(CP_UTF8, 0, achTemp2, MAX_PATH, achTemp, sizeNeed, NULL, NULL);
int dataLen = wcslen(achTemp2) - 1;
if (achTemp[dataLen] == '\\') {
achTemp[dataLen] = L'\0';
}
}
//SHCreateDirectoryEx(T("C:\\Foo\\Bar\\Baz"));
sprintf( lpDataTempClass, "-Djava.class.path=%s;%ls\\lib;", achTemp , lpchJavaHome );
options[ 0 ].optionString = lpDataTempClass;
CHAR lpDataTempClass2[ 2048 ];
CHAR lpData[MAX_PATH];
TCHAR lpData2[MAX_PATH];
GetThisPath(lpData2, 2048);
int sizeNeed = WideCharToMultiByte(CP_UTF8, 0, lpData2, MAX_PATH, lpData , 0 , NULL , NULL );
WideCharToMultiByte(CP_UTF8, 0, lpData2, MAX_PATH, lpData , sizeNeed, NULL, NULL);
#ifdef _WIN64
sprintf(lpDataTempClass2, "%s\\libn\\x64", lpData);
CreateDirectoryRecA( lpDataTempClass2 );
sprintf(lpDataTempClass2, "-Djava.library.path=%s\\bin;%s\\libn\\x64", lpchJavaHome, lpData );
#else
sprintf(lpDataTempClass2, "%s\\libn\\x86", lpData);
CreateDirectoryRecA(lpDataTempClass2);
sprintf(lpDataTempClass2, "-Djava.library.path=%s\\bin;%s\\libn\\x86", lpchJavaHome, lpData );
#endif
options[1].optionString = lpDataTempClass2;
//options[0].optionString = "-Djava.complier=JIT";
/* native lib path */
//"-Djava.library.path=c:\\mylibs"
//sprintf(lpchDataLib,"-Djava.library.path=%s",lpchLibrary);
options[2].optionString = "-Djava.complier=JIT";
pfnGetDefaultJavaVMOptions(&vm_args);
vm_args.version = JNI_VERSION_1_6;//JNI_VERSION_1_4;
vm_args.options = options;
vm_args.nOptions = 3;
vm_args.ignoreUnrecognized = TRUE;
if(pfnCreateJavaVM == NULL) {
return NULL;
}
if(!pfnGetDefaultJavaVMOptions) {
FreeLibrary(hinst_JVM);
return NULL;
}
//Create JVM
jint iRetval = pfnCreateJavaVM(&vm, (void**)&env,&vm_args);
//Error handling.
if (iRetval < 0) {
vm_args.nOptions = 2;
options[2].optionString = NULL;
iRetval = pfnCreateJavaVM(&vm, (void**)&env,&vm_args);
if(iRetval < 0) {
return NULL;
}
}
*env2 = env;
return vm;
} |
That works for Win32. |
JavaCPP will make C++ code from Java class library. |
jar files will be used a dll inside C++ can be good idea! |
I will give you example C++ code for that if you want to add such capability to JavaCPP |
There's also jni.hpp these days: https://github.com/mapbox/jni.hpp |
Hello again.
Now i am doing C++ and Java Application.
I want C++ with Java Interop.
To do i use JavaCPP.
But the problem is , can javacpp export library clone as C++ also ?
so interop between java and C++ will be easier
The text was updated successfully, but these errors were encountered: