2010-02-26 68 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

相关问题