Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

OS X JDK 1.7 failing to load #90

Closed
jahewson opened this issue Nov 14, 2013 · 54 comments
Closed

OS X JDK 1.7 failing to load #90

jahewson opened this issue Nov 14, 2013 · 54 comments

Comments

@jahewson
Copy link

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)
@joewhite86
Copy link
Contributor

At which point in your code the error comes up?

Tested on my system (OS X 10.9 with 1.7.0_45) right now and it works.

@jahewson
Copy link
Author

My code was simply:

var java = require("java");
var list = java.newInstanceSync("java.util.ArrayList");

The error occurs on line 2 (determined by inserting a console.log) there is no stack trace.

@joewhite86
Copy link
Contributor

Seems like the java environment isn't set up correctly on your system. The only place I found this file on mine is: /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Libraries/libserver.dylib, which is the pre-installed java version that came with previous versions of OS X.

What are your Java environment variables? (JAVA_HOME, ...)

@jahewson
Copy link
Author

OS X 10.9 (Mavericks) does not come with Java installed. The Apple version of Java 1.6 is available as an update but not Java 1.7, which must be downloaded from Oracle.

I do not have any environmental variables set regarding Java, there is no JAVA_HOME or Java directories in the PATH as Oracle's installer did not set them.

I think the problem may be that node-java assumes that Apple's version of Java is installed, which is not necessarily true.

@joewhite86
Copy link
Contributor

No node-java works with both, pre-installed and others. If you upgraded from previous versions of osx you can indeed have the pre-installed jdk under /System/Library.

Try setting JAVA_HOME

@jahewson
Copy link
Author

I have a fresh installation of Mavericks, not an upgrade. node-java needs to work out of the box with Oracle Java 7...

@jahewson
Copy link
Author

I've done some investigating - as you said the JDK under /System/Library/Java is present on Mavericks, but I think it's intended for system use (or is this just where Apple keeps its JDK?):

ls /System/Library/Java/JavaVirtualMachines
1.6.0.jdk

The user-accesible (or just Oracle?) JDKs are under /Library/Java and I only have Oracle's JDK 1.7:

~$ ls /Library/Java/JavaVirtualMachines/
jdk1.7.0_45.jdk

The Oracle JDK 7's Home directory contents is:

~$ ls /Library/Java/JavaVirtualMachines/jdk1.7.0_45.jdk/Contents/Home/
COPYRIGHT               include
LICENSE                 jre
README.html             lib
THIRDPARTYLICENSEREADME-JAVAFX.txt  man
THIRDPARTYLICENSEREADME.txt     release
bin                 src.zip
db

as you can see, there is no bundle directory, which relates to the original error message.

@jsdevel
Copy link
Collaborator

jsdevel commented Dec 19, 2013

I think find-java-home would help out here.

@jsdevel
Copy link
Collaborator

jsdevel commented Dec 21, 2013

@jahewson Can you clone my fork locally and try building that? Here's the URL: https://github.com/jsdevel/node-java.git

I currently have PR for issue #94 open and I believe it will solve issues like this.

@jahewson Now that 0.3.0 is published, can you try installing again with 0.3.0?

@TheSpyder
Copy link
Contributor

I'm still seeing this with 0.3.2, OS X 10.9.2.

shinji:corpus spyder$ echo $JAVA_HOME
/Library/Java/JavaVirtualMachines/jdk1.7.0_55.jdk/Contents/Home
shinji:corpus spyder$ node corpus.js 
<startup output>
No Java runtime present, requesting install.

I'm also using java.newInstanceSync.

@jsdevel
Copy link
Collaborator

jsdevel commented May 23, 2014

@joeferner should we add libserver.dylib to postInstall.js?

@joeferner
Copy link
Owner

That would probably make sense. Anyone have OSX to try it on?

@TheSpyder
Copy link
Contributor

I'm happy to test anything if you can give me a patch or tell me how to modify my local install :)

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

@TheSpyder can you try adding git+https://github.com/jsdevel/node-java.git#a4c47f938903957f578c7f228f4aad34b6cfdb32 in your package.json for java?

@TheSpyder
Copy link
Contributor

node(94131,0x7fff7d3e0310) malloc: *** error for object 0x103f7e7c0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6

well that was interesting :)

Do I need to run a particular command to get more detail?

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

hmmmm.... That is interesting. You're getting a different error now though which is promising!

Can you do this and paste the output of ~/contents-of-java-home.txt?

  1. cd node_modules/java
  2. cd "node findJavaHome.js"
  3. find . > ~/contents-of-java-home.txt

cc @joeferner. I'm guessing that error is related to node-java trying to free a pointer that hasn't been created yet?

@TheSpyder
Copy link
Contributor

I don't want to spam anyone's inbox, so I put it on pastebin :)

http://pastebin.com/mAxiwt05

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

@TheSpyder @joeferner looks like that error is happening for other Mavericks users without node-java https://www.bountysource.com/issues/1126096-tree-diffindex-pointer-being-freed-was-not-allocated.

@TheSpyder what version of java and node are you using?

@TheSpyder
Copy link
Contributor

ahh, well I have upgraded node recently. I try to keep it up to date.

Node v0.10.28
Java latest (tried both 1.7.0_55 and 1.8.0_05 by changing JAVA_HOME)

I have, however, installed node via homebrew if that makes any difference.

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

@TheSpyder you might try with other versions of node if you can.

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

@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?

@TheSpyder
Copy link
Contributor

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.

@jsdevel
Copy link
Collaborator

jsdevel commented May 25, 2014

@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.

@TheSpyder
Copy link
Contributor

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
Copy link
Contributor

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
Copy link
Contributor

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).

@jsdevel
Copy link
Collaborator

jsdevel commented May 26, 2014

@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
Copy link
Collaborator

jsdevel commented May 26, 2014

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
Copy link
Collaborator

jsdevel commented May 26, 2014

@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
Copy link
Collaborator

jsdevel commented Jun 12, 2014

@TheSpyder is it safe to close the issue then?

@TheSpyder
Copy link
Contributor

Yes. I don't believe you can fix this. It's worth adding a note (and possibly my suggested workarounds) to the readme, though.

@jsdevel
Copy link
Collaborator

jsdevel commented Jun 12, 2014

Thanks @TheSpyder! I think for now we can close it. Can you submit a PR with the README addition?

@seshness
Copy link

Thanks, @TheSpyder! That was super helpful.
It looks like the homebrew cask installation for Java 7 now applies your first suggested workaround: https://github.com/caskroom/homebrew-versions/blob/master/Casks/java7.rb#L17

Installation steps for the next person to stumble upon this issue:

$ brew install caskroom/cask/brew-cask
$ brew tap caskroom/homebrew-versions
$ brew cask install java7

@p3k p3k mentioned this issue Mar 21, 2016
jamestyrrell pushed a commit to jamestyrrell/homebrew-cask that referenced this issue Jan 5, 2017
This is copying the behaviour found in the [java](https://github.com/caskroom/homebrew-cask/blob/master/Casks/java.rb#L21) cask and resolves an issue with node-java (as described [here](joeferner/node-java#90 (comment))).
miccal pushed a commit to Homebrew/homebrew-cask that referenced this issue Jan 5, 2017
This is copying the behaviour found in the [java](https://github.com/caskroom/homebrew-cask/blob/master/Casks/java.rb#L21) cask and resolves an issue with node-java (as described [here](joeferner/node-java#90 (comment))).
@junqdu
Copy link

junqdu commented Mar 13, 2017

@TheSpyder any idea why JDK doesn't advertise itself as supporting JNI?

@TheSpyder
Copy link
Contributor

@junqdu no idea. I don't think Oracle cares about Java on OS X much anymore.

@scottellis64
Copy link

Adding JNI to the list of JVMCapabilities worked perfectly for me.

@caiyufei
Copy link

@TheSpyder Thanks for your solution, adding JNI also works for me! Thanks !!

@ctrueden
Copy link
Contributor

@TheSpyder @jsdevel There is another, better way to avoid this horrible issue: load libjli before loading libjvm. The ImageJ project uses this trick for its launcher successfully. And here is another example of a project using this solution. A big advantage of this approach is that users do not need to manually tweak their Java installations in order to get up and running.

@jsdevel
Copy link
Collaborator

jsdevel commented Apr 24, 2019

nice @ctrueden ! can you open a pr?

@TheSpyder
Copy link
Contributor

Interesting. That ImageJ dev seems to like to rant about Apple. I'm amused that there were three working methods to fix the same issue; perhaps the config patch only worked by accident.

Your second link (xamarin) points to this OpenJDK bug, which was fixed in Java 9:
https://bugs.openjdk.java.net/browse/JDK-7131356

So while this change is probably harmless on new releases, it seems only necessary on now-unsupported versions of Java (Java 8 fell out of Oracle support in Jan 2019).

A big advantage of this approach is that users do not need to manually tweak their Java installations in order to get up and running.

Note that manual patching wasn't required for homebrew users, the cask did that in a post install script. Although that was been removed after someone discovered it didn't makes sense in Java 11.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests