Error starting Java application from Win32 C ++ application using CreateProcess

I am trying to run a Java application from a C ++ application using the following code:

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

When I create and run the program, I get the following error:

 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. 

The testapp.jar file is an executable JAR file exported from an Eclipse project with one class in it:

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

The exe and jar files are in the same folder, and I run the exe from the command line. If I run the JAR directly by inserting c:\java\jre\bin\java.exe -jar testapp.jar into the command line, everything works as expected.

Does anyone know what is going on here?

EDIT: Thanks to everyone for your help, but it looks like I'm working now.

+4
source share
4 answers

I decided. I used:

 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 you used:

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

which, when I used it, replicates your error. The difference is the space before the -jar switch, and why it should be, I don’t know, I came across this by mistake!

+8
source

I just had to change the way CreateProcess was created:

 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) { 
+3
source

The documentation for CreateProcess() indicates the lpCurrentDirectory parameter:

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

The statement does not contain a definition for path , but most likely it is configured incorrectly.

+1
source

Try specifying the JAR directory after -jar . This may be relevant to your current working directory ...

0
source

Source: https://habr.com/ru/post/1302446/


All Articles