I'm trying to launch a Java app from a C++ app 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 build 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 a runnable JAR file exported from an Eclipse project with a single class in it:
public class Test {
public static void main(String[] args) {
System.out.println("test");
}
}
The EXE and JAR file are in the exact same folder, and I'm running the EXE from the command line. If I run the JAR directly by putting c:\java\jre\bin\java.exe -jar testapp.jar
into the command-prompt everything works as expected.
Does anyone have any idea what's going on here?
EDIT: Thank you all for your help, but it looks like I've got it working now.