Skip to content
This repository has been archived by the owner on Aug 11, 2023. It is now read-only.

Commit

Permalink
fixed jvm loading on mac/linux
Browse files Browse the repository at this point in the history
  • Loading branch information
badlogic committed May 17, 2014
1 parent 4ae46dc commit b3a62da
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 29 deletions.
2 changes: 1 addition & 1 deletion example-config-mac.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"platform": "mac",
"jdk": "/Users/badlogic/Downloads/openjdk-1.7.0-u45-unofficial-icedtea-2.4.3-macosx-x86_64-image.zip",
"executable": "myapp",
"appjar": "target/packr-1.0-SNAPSHOT.jar",
"appjar": "target/packr-1.1-SNAPSHOT.jar",
"mainclass": "com/badlogicgames/packr/TestApp",
"vmargs": [
"-Xmx1G"
Expand Down
30 changes: 8 additions & 22 deletions native/premake4.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,9 @@
JAVA_HOME = os.getenv("JAVA_HOME")
if not JAVA_HOME then
printf("ERROR: set JAVA_HOME to your JDK directory, e.g. /Library/Java/JavaVirtualMachines/jdk1.7.0_51.jdk/Contents/Home");
printf("No trailing slash in the path!");
os.exit()
end

solution "packr"
configurations { "debug", "release" }

project "packr"
kind "ConsoleApp"
language "C++"
language "C++"
buildoptions { "-Wall" }
files { "**.h", "src/launcher.cpp" }
includedirs { "include", "include/jni-headers" }
Expand All @@ -26,29 +19,22 @@ solution "packr"
--- windows ---
configuration { "windows" }
kind "WindowedApp"
defines { "WINDOWS" }
defines { "WINDOWS" }
includedirs { "include/jni-headers/win32" }
files { "src/main-windows.cpp" }
flags { "WinMain" }
flags { "WinMain" }

--- linux ---
configuration { "linux" }
defines { "LINUX" }
LIBJVM_DIR = JAVA_HOME .. "/jre/lib/amd64/server/"
printf(LIBJVM_DIR);
includedirs { "include/jni-headers/linux" }
links { "dl" }
files { "src/main-linux.cpp" }
libdirs { LIBJVM_DIR }
links { "jvm" }
linkoptions { "-Wl,-rpath,'$$ORIGIN/jre/lib/amd64/server'" }

--- mac os x ---
configuration { "macosx" }
configuration { "macosx" }
defines { "MACOSX" }
LIBJVM_DIR = JAVA_HOME .. "/jre/lib/server/"
printf(LIBJVM_DIR);
includedirs { "include/jni-headers/mac" }
files { "src/main-mac.cpp" }
libdirs { LIBJVM_DIR }
links { "jvm" }
linkoptions { "-framework CoreFoundation", "-rpath @executable_path/jre/lib/server" }
links { "dl" }
linkoptions { "-framework CoreFoundation -ldl" }
18 changes: 12 additions & 6 deletions native/src/launcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <iostream>
#include <fstream>
#include <picojson.h>
#include <dlfcn.h>

extern std::string getExecutableDir();
extern int g_argc;
Expand Down Expand Up @@ -68,17 +69,22 @@ void* launchVM(void* params) {
JavaVM* jvm = 0;
JNIEnv* env = 0;


#ifndef WINDOWS
JNI_CreateJavaVM(&jvm, (void**)&env, &args);
if(!env || !jvm) {
printf("couldn't create JVM");
exit(-1);
}
#ifdef MACOSX
std::string jre = execDir + std::string("/jre/lib/server/libjvm.dylib");
#else
std::string jre = execDir + std::string("/jre/lib/amd64/server/libjvm.so");
#endif

void* handle = dlopen(jre.c_str(), RTLD_LAZY);
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)dlsym(handle, "JNI_CreateJavaVM");
#else
HINSTANCE hinstLib = LoadLibrary(TEXT("jre\\bin\\server\\jvm.dll"));
PtrCreateJavaVM ptrCreateJavaVM = (PtrCreateJavaVM)GetProcAddress(hinstLib,"JNI_CreateJavaVM");
jint res = ptrCreateJavaVM(&jvm, (void**)&env, &args);
#endif

jint res = ptrCreateJavaVM(&jvm, (void**)&env, &args);

jobjectArray appArgs = env->NewObjectArray(g_argc, env->FindClass("java/lang/String"), NULL);
for(int i = 0; i < g_argc; i++) {
Expand Down
Binary file modified src/main/resources/packr-mac
Binary file not shown.

0 comments on commit b3a62da

Please sign in to comment.