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

Unexpected crash on java.import() using more modules

Open salvatore373 opened this issue 8 years ago • 4 comments

I am requiring two modules (module x and module y) from the main module. Both the required modules call var java = require('java'). In the main module I first require module x and everything works well. When I require module y the app crashes without printing exeptions in the console. Anyway I figured out that the crash happens while calling java.import('class name'), and more precisely in the nodeJavaBridge.js file by the findClassSync(name) Line:227 method. I think that there is a JVM problem: in my opinion the JVM is created by module x and can't be created again by module y, so findClassSync(name) can't work. How can I use both modules without crashing?

salvatore373 avatar Oct 15 '17 10:10 salvatore373

I'm guessing this is two different versions of node-java being loaded. You could probably force the version using node shrinkwrap like in this post https://stackoverflow.com/a/37400568

I'm not sure of the best way to handle this in the long term. Maybe setting a global variable in node-java which holds a reference to the loaded native library?

joeferner avatar Oct 15 '17 14:10 joeferner

Thank you @joeferner but this issue happens also with the same versions. I am trying to solve it. I will update my code if I successed. In the meanwhile I will wait for other answers.

salvatore373 avatar Oct 16 '17 17:10 salvatore373

hi, have you solved this problem? I am facing the same problem when use node-java in Electron

HarveyLee1228 avatar Mar 04 '22 14:03 HarveyLee1228

Hi @HarveyLee1228 , yes, I remember I solved the problem, but it's been a long time ago, and honestly, I don't remember my solution. I only remember that, referring to that version of this package, the java files were loaded when the require() function was called, and so I added a function to explicitly load the java classes when they were ready. But my memories could be completely wrong. Unfortunately I can't help you on this topic. I hope you will find a better solution that can be included in this package.

salvatore373 avatar Mar 05 '22 08:03 salvatore373