Using node-java in Electron


#1

Hi all, I’m trying to use node-java (https://github.com/joeferner/node-java) to execute some jar files. But when I try require(“java”) I get an error “Module did not self-register”.

I’m fairly new in this. Please help!


#2

I think that means the module was compiled against a different version of node than the one in Electron. electron-rebuild will rebuild your native modules using the same version of node as Electron.


#3

Hi, I did that too, but nothing happens.


#4

Are you sure the module is compatible with io.js? I couldn’t get it to install with either 1.6.3 or 2.2.1.


#5

I have no idea… Sorry. Can you check please? https://github.com/joeferner/node-java Thanks!


#6

It fails to install with io.js 2.2.1 and SIGSEGVs with 1.6.3. You might want to create an issue on the node-java repo for more help.


#7

node-java now depends on NAN v2. It is compatible with nodejs > 4 and therefore electron


#8

I can confirm that this now works. Verified with the electron quick start project, adding:

npm install --save-dev electron-rebuild
npm install java
./node_modules/.bin/electron-rebuild

Then, in main.js, some brutal code to test:

var java = require( 'java' );
var sys = java.import('java.lang.System');

sys.out.printlnSync('Hello from java :)'); // outputs to terminal

Worked out of box that way for me on Linux Mint.


#9

Doesn’t seem to work on Mac OSX El Capitan, I get this error. Any help would be appreciated.

Steps:

  1. brew install npm

  2. git clone https://github.com/electron/electron-quick-start

  3. cd electron-quick-start

  4. npm install

  5. npm install --save-dev electron-rebuild

  6. npm install java

  7. ./node_modules/.bin/electron-rebuild

    NOENT: no such file or directory, stat '/Users/gcstang/GitRepositories/3rdparty/electron-quick-start/node_modules/electron-rebuild/lib/headers/.node-gyp/1.0.1/common.gypi’
    Error: ENOENT: no such file or directory, stat '/Users/gcstang/GitRepositories/3rdparty/electron-quick-start/node_modules/electron-rebuild/lib/headers/.node-gyp/1.0.1/common.gypi’
    at Error (native)

******************************************** UPDATE ***************************************
Ok This seems like a bug in electron-rebuild
If I go here:
cd electron-quick-start/node_modules/electron-rebuild/lib/headers/.node-gyp

The directory below this is iojs-1.0.1 instead of 1.0.1 that the code is looking for so i created a symbolic link and it’s now working.