2010-02-26 33 views
4
一個Win32 C++應用程序啓動Java應用程序

我嘗試使用下面的代碼啓動從C++應用程序的Java應用程式:錯誤使用CreateProcess的

#include <windows.h> 
#include <memory.h> 
#include <tchar.h> 

int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { 
    STARTUPINFOW  siStartupInfo; 
    PROCESS_INFORMATION piProcessInfo; 

    memset(&siStartupInfo, 0, sizeof(siStartupInfo)); 
    memset(&piProcessInfo, 0, sizeof(piProcessInfo)); 

    if (CreateProcess(TEXT("c:\\java\\jre\\bin\\java.exe"), TEXT("-jar testapp.jar"), NULL, NULL, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcessInfo) == false) { 
     MessageBox(NULL, L"Could not load app", L"Error", 0); 
    } 

    CloseHandle(piProcessInfo.hProcess); 
    CloseHandle(piProcessInfo.hThread); 

    return 0; 
} 

當我建立並運行程序,我得到了以下錯誤:

Exception in thread "main" java.lang.NoClassDefFoundError: testapp/jar 
Caused by: java.lang.ClassNotFoundException: testapp.jar 
     at: java.net.URLClassLoader$1.run(Uknown Source) 
     at: java.security.AccessController.doPrivileged(Native Method) 
     at: java.net.URLClassLoader.findClass(Uknown Source) 
     at: java.lang.ClassLoader.loadClass(Uknown Source) 
     at: sun.misc.Launcher$AppClassLoader.loadClass(Uknown Source) 
     at: java.lang.ClassLoader.loadClass(Uknown Source) 
Could not find the main class: testapp.jar. Program will exit. 

testapp.jar文件是從Eclipse項目導出的一個類運行的JAR文件:

public class Test { 
    public static void main(String[] args) { 
     System.out.println("test"); 
    } 
} 

EXE和JAR文件位於完全相同的文件夾中,並且正在從命令行運行EXE。如果我通過將c:\java\jre\bin\java.exe -jar testapp.jar放入命令提示符中直接運行JAR,則所有操作都按預期工作。

有沒有人有任何想法這裏發生了什麼?

編輯:謝謝大家的幫助,但它看起來像我現在正在工作。

回答

8

解決了它。我用:

if (CreateProcess(TEXT("C:\\Program Files\\Java\\jre6\\bin\\java.exe"), TEXT(" -jar test.jar"), NULL, NULL, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcessInfo) == false) { 
     MessageBox(NULL, L"Could not load app", L"Error", 0); 
    } 

而你使用:

if (CreateProcess(TEXT("C:\\Program Files\\Java\\jre6\\bin\\java.exe"), TEXT("-jar test.jar"), NULL, NULL, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcessInfo) == false) { 
     MessageBox(NULL, L"Could not load app", L"Error", 0); 
    } 

,當我用它,複製您的錯誤。區別在於-jar開關之前的空格,以及爲什麼這應該是,我不知道,我偶然發現了它!

+1

啊,那確實有效!給你一個大綠色的勾號! – Zoltan 2010-02-26 05:04:46

+0

我會老實說,爲什麼當前沒有了我。如果我找到了, – 2010-02-26 05:09:29

+1

這個鬼鬼祟祟的空白...... – 2010-02-26 05:24:07

1

CreateProcess()的文檔指定的參數lpCurrentDirectory

The full path to the current directory for the process. The string can also specify a UNC path.
If this parameter is NULL, the new process will have the same current drive and directory as the calling process.

您摘錄缺少定義path,但最有可能設置不正確。

+0

對不起,這是我在粘貼代碼中的錯誤。該行應該是: CreateProcess(TEXT(「c:\\ java \\ jre \\ bin \\ java.exe」),TEXT(「 - jar testapp.jar」),NULL,NULL,false,CREATE_DEFAULT_ERROR_MODE,NULL ,NULL,&siStartupInfo,&piProcessInfo) – Zoltan 2010-02-26 04:22:52

0

嘗試在-jar之後指定JAR的目錄。它可能與你目前的工作目錄...

+0

試過了,仍然沒有運氣:( – Zoltan 2010-02-26 04:24:34

3

我不得不改變我打電話CreateProcess的方式:

wchar_t *command = (wchar_t*)calloc(512, sizeof(wchar_t)); 

wsprintf(command, TEXT("c:\\java\\jre\\bin\\java.exe -jar testapp.jar")); 

if (CreateProcess(NULL, command, NULL, NULL, false, CREATE_DEFAULT_ERROR_MODE, NULL, NULL, &siStartupInfo, &piProcessInfo) == false) { 
+0

哈哈,現在你可以接受你自己的答案! – 2010-02-26 05:01:04

+0

這個命令允許你在命令vs「c:\\ java \\ jre \\ bin \\ java.exe -jar testapp.jar」中使用「java.exe -jar testapp.jar」。 https://support.microsoft.com/zh-cn/kb/175986 – 2015-07-07 15:21:38

相關問題