node-java icon indicating copy to clipboard operation
node-java copied to clipboard

OS X JDK 1.7 failing to load

Open jahewson opened this issue 13 years ago • 54 comments

I'm using OS X 10.9 with Sun's JDK 1.7 and always get the following error:

JavaVM: Failed to load JVM: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bundle/Libraries/libserver.dylib
JavaVM FATAL: Failed to load the jvm library.
JavaVM: Failed to load JVM: /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bundle/Libraries/libserver.dylib
JavaVM FATAL: Failed to load the jvm library.
Segmentation fault: 11

Indeed, the folder /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/bundle does not exist on my system.

I'm using Sun's JDK 1.7, and do not have Apple's installed. The output of java -version is:

java version "1.7.0_45"
Java(TM) SE Runtime Environment (build 1.7.0_45-b18)
Java HotSpot(TM) 64-Bit Server VM (build 24.45-b08, mixed mode)

jahewson avatar Nov 14 '13 23:11 jahewson

@TheSpyder you may attempt the proposed debugging instructions in that bountysource link I posted. It may be related to a function within node-java. What line do you get that error on BTW?

jsdevel avatar May 25 '14 09:05 jsdevel

I'm not familiar with gdb, so I'd need help to get a backtrace. The error is definitely coming from newInstanceSync, even this is enough to do it:

var java = require('java');
java.newInstanceSync("java.lang.String");

And just to be sure, I tried passing an empty and incorrect strings to newInstanceSync - still failed. It doesn't even manage to check if the class exists.

TheSpyder avatar May 25 '14 09:05 TheSpyder

@TheSpyder I'm no c++ expert. You may attempt adding print statements in src/Java.cpp#newInstanceSync and re-build. That could let you know where the failure is at.

jsdevel avatar May 25 '14 10:05 jsdevel

I was trying older versions of node, no dice.

Adding printf to help debug the method calls revealed this nugget in Java::createJVM:

  // setup classpath
  std::ostringstream classPath;
  printf("10\n");
  classPath << "-Djava.class.path=";
  printf("11\n");

10 prints, then crash without 11. I'm no C guru either, but doesn't that read as classPath has been declared but not defined before use? :)

TheSpyder avatar May 25 '14 10:05 TheSpyder

hmm. git history shows that's been there since 2012, so clearly I'm off base somehow.

I just installed 0.3.3 plain and I'm getting the same error, so it wasn't anything you did :)

TheSpyder avatar May 25 '14 10:05 TheSpyder

ooh, and I'm getting it with 0.3.2 as well.

When I remove JDK 1.7 and 1.8 and type java at the command line, it no longer offers to install it - instead I get a message saying I need to install a JDK and linking to the oracle site.

It looks like Java 1.6 is officially dead on OS X.

They must've snuck that in with 10.9.3 (I only just realised I was still on 10.9.2 when I first posted here).

TheSpyder avatar May 25 '14 10:05 TheSpyder

@TheSpyder 10 prints, then crash without 11. I'm no C guru either, but doesn't that read as classPath has been declared but not defined before use? :) In c++ there's something called RAII which basically means merely declaring the type acquired a new instance of the object, so in that case classPath would be a new instance of std::ostringstream.

@TheSpyder @jahewson are you using macports? Can you verify that you're compiling with g++?

@joeferner this (http://stackoverflow.com/questions/21413852/simple-protocol-buffers-program-works-when-compiled-with-g-but-not-clang) seems to be very relevant.

jsdevel avatar May 26 '14 04:05 jsdevel

And here we have this: https://trac.macports.org/wiki/UsingTheRightCompiler.

With Xcode 4.0 and 4.1 on OS X 10.6 or 10.7, llvm-gcc-4.2 is the default. With Xcode 4.2 or later, clang is the default and gcc is no longer included. The Xcode 4.6 release notes state that it is the last version of Xcode that will include llvm-gcc-4.2, which will leave only clang.

@TheSpyder @jahewson I'm guessing node-java is getting compiled with clang and not gcc.

jsdevel avatar May 26 '14 04:05 jsdevel

@TheSpyder looks like I may have been wrong about RAII in this context.

@joeferner From http://clang.llvm.org/compatibility.html, "Clang does not zero initialize local block variables, and programs which rely on such behavior will most likely break when built with Clang."

jsdevel avatar May 26 '14 04:05 jsdevel

Sorry for not responding, I was on holiday.

I'm using Homebrew, not macports, but I have Xcode 5 (the latest) so yes clang is the cause. An update around mid may removed GCC completely, which broke a lot of open source projects.

Homebrew has a replacement GCC package that was already installed, so doing this fixed it:

export CXX=gcc
npm rebuild

I'm not sure what you can do about that, other than searching for GCC before CLANG and hoping it's installed.

But now that we've cleared that up, back on topic - your commit above (git+https://github.com/jsdevel/node-java.git#a4c47f938903957f578c7f228f4aad34b6cfdb32) doesn't work and still requests Java 6. Same with the new 0.3.4 release (Which I can see contains that commit).

My node_modules/java/build/jvm_dll_path.json file contains ":/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home/jre/lib/server" and that path is correct, libjvm.dylib is in that folder. But it doesn't appear to be using it.

Want me to insert print statements in the C code again? :)

TheSpyder avatar Jun 03 '14 12:06 TheSpyder

Hi all, just upgraded to node 0.10.28, OS X 10.9.3, and JDK 1.7.0_60. Experiencing this same issue now. Any debug info I can provide to help troubleshoot?

node(70832,0x7fff748a2310) malloc: *** error for object 0x103b117c0: pointer being freed was not allocated *** set a breakpoint in malloc_error_break to debug make[1]: *** [test-debug] Abort trap: 6

stefankutko avatar Jun 05 '14 14:06 stefankutko

That's the clang issue. Assuming you have gcc installed, this will fix it:

export CXX=gcc
npm rebuild

If you don't have gcc, I recommend homebrew :)

TheSpyder avatar Jun 05 '14 22:06 TheSpyder

Thanks for the reply. Yes, GCC installed, exported CXX ENVAR, npm rebuild.. but, new error this time:

$ make run /Applications/Xcode.app/Contents/Developer/usr/bin/make DEBUG= 'run-debug' dyld: lazy symbol binding failed: Symbol not found: _ZNSt8__detail15_List_node_base7_M_hookEPS0 Referenced from: /Users/xxxx/xxxx/xxxx/node_modules/node-fix/node_modules/java/build/Release/nodejavabridge_bindings.node Expected in: dynamic lookup

dyld: Symbol not found: _ZNSt8__detail15_List_node_base7_M_hookEPS0 Referenced from: /Users/xxxx/xxxx/xxxx/node_modules/node-fix/node_modules/java/build/Release/nodejavabridge_bindings.node Expected in: dynamic lookup

make[1]: *** [run-debug] Trace/BPT trap: 5 make: *** [run] Error 2

Thoughts?

stefankutko avatar Jun 05 '14 23:06 stefankutko

@stefankutko you might try cleaning up the build directory first. It's possible the code that was compiled with clang is still present.

@TheSpyder I think the solution here would be to compile the code against clang and see how hard it would be to make it compatible.

jsdevel avatar Jun 08 '14 01:06 jsdevel

FYI, I was able to fix this issue this morning with a brew update gcc... latest version is 4.8.3_1

stefankutko avatar Jun 08 '14 14:06 stefankutko

Very weird. All of a sudden I am getting those dyld errors as well - but updating gcc doesn't fix it.

It looks like the cause is that the code now compiles against JAVA_HOME but is still running against the system JDK6 (hence the prompt to install it still appears). This causes dynamic link errors.

When I recompile without JAVA_HOME set, using gcc, and allow JDK6 to be installed, it still works.

TheSpyder avatar Jun 10 '14 11:06 TheSpyder

I think it was a weird combination of using my actual project (which still links to v0.3.2) instead of my test project, and needing a brew update.

0.3.4 now compiles without needing CXX=gcc and doesn't have dylib errors even when JAVA_HOME is set. I hate moving targets :)

TheSpyder avatar Jun 10 '14 12:06 TheSpyder

OK. The answer is crap, but at least it makes sense now.

Debugging finally lead me to the JNI_CreateJavaVM() line in java.cpp. A quick google had this as the top result: https://bugs.eclipse.org/bugs/show_bug.cgi?id=411361

The problem, once you get towards the end of the bug report, is that Oracle's JDK doesn't advertise itself as supporting JNI. So even though node-java links to JAVA_HOME correctly, when the JNI_CreateJavaVM call is made the system sees that the linked JDK doesn't "support" JNI and switches to Java 6 instead.

/headdesk

There are two solutions I've found from various sources:

  • manually enable JNI - edit /Library/Java/JavaVirtualMachines/<version>.jdk/Contents/Info.plist (a simple XML file) and add JNI as an option in JVMCapabilities
  • (does not work in 10.10) trick the system into thinking you have JDK6, by symlinking the new java to where JDK6 normally lives (sudo ln -s /Library/Java/JavaVirtualMachines/jdk1.8.0_05.jdk /System/Library/Java/JavaVirtualMachines/1.6.0.jdk)

Both work, I've tested them, but neither is a good solution and both require root access to update the fix upon upgrading the JDK.

The eclipse bug report talks about a way to solve it but I don't think their fix really applies to the c++ code here. I suspect there is very little you can do to work around Oracle's issue here.

.

On the plus side, I know more about the whole process now and I don't think your jvm_dll_path.json fix is necessary. binding.gyp already uses findJavaHome.js and when I run npm install --verbose the build links to my Oracle JDK even without JAVA_HOME set.

TheSpyder avatar Jun 10 '14 13:06 TheSpyder

Awesome job researching this @TheSpyder !

jsdevel avatar Jun 10 '14 13:06 jsdevel

So I was able to compile successfully with CXX=clang++ :/ OSX 10.8.5 Xcode 5.0.2

jsdevel avatar Jun 12 '14 04:06 jsdevel