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.