Skip to content
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

Open
eix128 opened this issue May 24, 2015 · 13 comments
Open

JavaCPP export Library clone as C++ class files #17

eix128 opened this issue May 24, 2015 · 13 comments

Comments

@eix128
Copy link

eix128 commented May 24, 2015

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

@eix128
Copy link
Author

eix128 commented May 24, 2015

For example with the help of JavaCPP.
https://github.com/hoary/JavaAV
I can use this library from C++ directly.

@saudet
Copy link
Member

saudet commented May 25, 2015

It wasn't designed for that, no... Check the README.md file for some options.

@eix128
Copy link
Author

eix128 commented May 28, 2015

I know but its not that hard to do with ANTLR Library

@saudet
Copy link
Member

saudet commented May 28, 2015

I'm not sure why ANTLR would help with that. We don't really need to parse anything... Am I missing something?

@eix128
Copy link
Author

eix128 commented May 29, 2015

so how can you convert Java code to C++ ?? I can help you about library.
I have some source codes for you

@eix128
Copy link
Author

eix128 commented May 29, 2015

You can call JavaVM inside C++ with the help of JNI Library on C++

@eix128
Copy link
Author

eix128 commented May 29, 2015

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;
}

@eix128
Copy link
Author

eix128 commented May 29, 2015

That works for Win32.
Also it can work on Linux and MAC too with some little work

@eix128
Copy link
Author

eix128 commented May 29, 2015

JavaCPP will make C++ code from Java class library.
Invoke Java from C++.
and wrap the class file with C++ methods

@eix128
Copy link
Author

eix128 commented May 29, 2015

jar files will be used a dll inside C++ can be good idea!

@eix128
Copy link
Author

eix128 commented May 29, 2015

I will give you example C++ code for that if you want to add such capability to JavaCPP

@saudet
Copy link
Member

saudet commented May 29, 2015

As mentioned in the README.md file, there's already solutions like Jace and JunC++ion for that. What is missing from those two?

@saudet
Copy link
Member

saudet commented Apr 25, 2020

There's also jni.hpp these days: https://github.com/mapbox/jni.hpp

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants