GithubHelp home page GithubHelp logo

Comments (13)

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

saudet avatar saudet commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

saudet avatar saudet commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

eix128 avatar eix128 commented on June 10, 2024

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

from javacpp.

saudet avatar saudet commented on June 10, 2024

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

from javacpp.

saudet avatar saudet commented on June 10, 2024

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

from javacpp.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.