簡體   English   中英

如何讓任何用戶運行從其C ++代碼讀取Java方法的.exe程序?

[英]How to let any users run .exe program that reads java methods from its c++ code?

這里是源代碼的zip文件: http : //cfile218.uf.daum.net/attach/254F2D4F5211EF93107910

或底部可見的源代碼。

我只是在PC上編譯並運行它:在調試和發布模式下都可以正常工作。

但是,如果我嘗試在另一台PC(已安裝Windows XP)上運行它,則會顯示一條錯誤消息,提示我沒有“ jvm.dll”,因此它無法運行。

因此,我從地址“ http://java.com/en/download/index.jsp ”安裝了Java並運行了它。 但是,它仍然說同樣的錯誤。

這是我的問題。

如何使它在沒有JDK或MSVC的其他普通PC上運行? 因為普通用戶絕對不擁有JDK。

## ================================ ##
// it is the c++ code that starts JVM and run java source code...

               #include <iostream>
              #include <windows.h>
             #include <jni.h>

       #pragma comment(lib, "jvm.lib")

       using namespace std;


       void main(){

    cout<<"JVM Create Start!!"<<endl;

    // JavaVM create & JVM environment setting start
    JavaVMOption options[1];
    JavaVMInitArgs vm_args;
    JNIEnv *env;
    JavaVM *jvm;
    long status;

    options[0].optionString = "-Djava.class.path=.";
    memset(&vm_args, 0, sizeof(vm_args));
    vm_args.version = JNI_VERSION_1_6;
    vm_args.nOptions = 1;
    vm_args.options = options;
    status = JNI_CreateJavaVM(&jvm, (void**)&env, &vm_args);
    // JavaVM create & JVM environment setting end

    cout<<"JVM Operating......."<<endl;

    cout<<"\n10+20?\n"<<endl;

    jclass cls;
    jmethodID mid;
    jobject obj;
    int staticresult=0;
    int result=0;

    if(status != JNI_ERR)
    {
        cls = env->FindClass("FirstJavaEx");

        if(cls != 0)
        {
            //
            // Use the static method start
            //
            cout<<"\t'static'start calling method" << endl;
            mid = env->GetStaticMethodID(cls, "staticAdd", "(II)I");
            if (mid != 0)
            {
                staticresult = env->CallStaticIntMethod(cls, mid, 10, 20);
                cout<<"\t'static'result: " << staticresult << endl;
            }
            else
            {
                printf("mid error\n");
                return;
            }
            //
            // Use the static method end
            //

            //
            //Create the Instance of InvokeFirstEX Class start
            //
            cout<<"\n\tInvokeFirstEx create instance" << endl;
            jmethodID cls_constructor = env->GetMethodID(cls, "<init>", "()V");

            if (cls_constructor != 0) {             
                obj = env->NewObject(cls, cls_constructor, "()V");
                cout<<"\t\tstart calling method" << endl;
                mid = env->GetMethodID(cls, "AddFunc", "(II)I"); 
                if (mid != 0)
                {
                    result = env->CallIntMethod(obj, mid, 10, 20);
                    cout<<"\t\tresult: " << result << endl;
                }
                else
                {
                    printf("mid error\n");
                    return;
                }
            }       
        }
        else 
        {
            printf("Can't find class\n");
            return;
        }

        jvm->DestroyJavaVM();
        cout<<"\nJVM Destroyed!!"<<endl;
    }

}


## ================================ ##
#pragma comment(lib, "jvm.lib")

因此,您的程序與jvm.lib靜態鏈接。 這意味着在啟動應用程序時, jvm.dll必須位於DLL搜索路徑上(當前目錄,C:\\ Windows \\ System32,%PATH%,...)。 jvm.dll可能位於%PATH%上的某個位置,因此該應用程序可以運行。 但是,正如您還注意到的那樣,通常情況並非如此。 因此,您必須使用另一種方法:手動加載庫。

在另一個答案中對此做了詳細的解釋。 那應該正是您所需要的,因為您沒有使用C ++ / CLI,您只需要用自己的錯誤處理替換所有throw gcnew System::ComponentModel::Win32Exception

編輯:代替RegGetValue ,您可以使用RegQueryValueEx

RegQueryValueEx(jKey, TEXT("CurrentVersion"), NULL, NULL, versionString, &bufsize);

但是,如果您看一下該函數的文檔 ,就會發現應該添加一個空終止符:

if (bufsize <= (16 * sizeof TCHAR)) // or whatever the buffer size is
    versionString[bufsize] = TEXT('\0');
else // error: buffer overflow

如何使它在沒有JDK或MSVC的其他普通PC上運行? 因為普通用戶絕對不擁有JDK。

他們不需要JDK,在客戶端上安裝JRE就足夠了。 但他們絕對需要的手段來訪問的jvm.dll(通過增加其路徑PATH環境varialble解釋這里 )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM