joeferner / node-java

Bridge API to connect with existing Java APIs.
MIT License
1.87k stars 282 forks source link

using node-java with node-webkit #131

Open GayatriN opened 10 years ago

GayatriN commented 10 years ago

I want to use node-java in node-webkit application (with html+js+java in which the javascript should call a java method). I followed the steps mentioned below.

  1. Installed node

2.Created nodewebkit application at ..\test_nwk_app folder with required html, javascript and java class files.

  1. installed node-java (using npm install -g java) in the test_nwk_app Could not install node-java directly in the application folder - getting msbuild errors. So, installed using Microsoft Visual Studio command prompt(at default location - C:\Users...\node_modules ) using npm install -g java. Rebuilt it for node-webkit using the command : nw-gyp --rebuild --target = 0.9.2. Then copied the node_modules folder to the application folder test_nwk_app Also, though -g option is used node-java is not installed globally.
  2. When the javascript file using the node-java module is executed in the node command prompt, rebuilding it for the node-webkit (nw-gyp --rebuild --target = 0.9.2) it worked fine. But, after rebuilding it - it raised jvm_dll_package.json missing error
  3. Also, when the packed node-webkit application is executed using nw.exe, java is recognized as [object object] and any methods inside it are noted as undefined.

These steps are tried using i. node-0.10.28+node-webkit-0.9.2 ii. node-0.10.28+node-webkit-0.8.5 iii. node-0.10.22+node-webkit-0.8.5

None of the above combinations, gave a successful result.

Could anyone please let me know if i have missed anything or the sequence of steps on how to use node-java in node-webkit application?

BTW, OS is windows7 and visual studio version used is 2012

Thanks in Advance, Gayatri

jsdevel commented 10 years ago

@GayatriN a detailed error output would be helpful.

GayatriN commented 10 years ago

After installing node and node-java, when I executed the js file (which uses node-java module) with node version 0.10.28 everything worked fine. Ex: executing the command "node myscript.js" at the node command prompt -- gave the expected output

In order to use the node-java module in node-webkit as the ABI for node and node-webkit, I'm supposed to rebuid node-java using nw-gyp with the command : nw-gyp rebuild --target=x.x.x, where x.x.x is the version of node-webkit that is being used.

After rebuilding node-java for 0.9.2 / 0.8.5:

  1. executed the js file using node, it gave the following error: ../build/jvm_dll_path.json not found. The build folder is missing.(which worked well before rebuilding the module)
  2. executed the node-webkit application, the alerts in js file, are displaying 'java' as object[object] and any method/attribute in java are displayed as 'undefined'. Hence, I couldn't use node-java in a node-webkit application.

The reason for choosing node-java and node-webkt is, I need to create a HTML5 desktop application, in which the javascript should communicate with java libraries and update the html page. Also, the javascript should be made as a binary file (when packing the application), for which there is an option in nodw-webkit.

Appreciate any help/suggestions in resolving the issues mentioned above.

Regards, Gayatri

jsdevel commented 10 years ago

@GayatriN I would suspect this to be a cwd issue. Can you see if this is the case?

GayatriN commented 10 years ago

It might not be the issue with cwd. After rebuilding node-java for node-webkit with the command nw-gyp rebuild --target=x.x.x - the jvm_dll_path.json under node_modules\java\build folder which was available before rebuild is missing. So, to solve the issue I had a copy the jvm_dll_path.json file and after rebuilding replaced the file in the build folder. This solved the problem and I could access java and its methods in the javascript file. But, I don't think this is the right approach. Any suggestions on this?

Also, as per my requirement I need to convert the .js file which uses node-java, to a binary file and the pack it as an node-webkit application But, I cannot create the snapshot of any .js file which uses the node_modules i.e., if the js file has the lines var f = require("fs"); or var f = require("java"); the nwsnapshot.exe of node-webkit is not able to create binary (.bin) for the .js file. It prints the error message - Failure running '....\myscript.js' on console and display windows error message dialog - stating that - nwsnapshot.exe stopped working.. I did post this issue in node-webkit/issues on github but no reply yet. Could you please let me know if you have any idea on how to create snapshot of a .js file which uses the node_modules?

Regards, Gayatri

sushruth commented 9 years ago

EDIT 2: No mouse functions work when using nodewebkit 0.8.6. I am going back to require('child_process').exec

EDIT: Using node-webkit 0.8.6 using npm install nodewebkit@0.8.6 with node 0.10.35 makes it work. everything ran without issues.

I just started using node.js yesterday! so here goes my attempt at debugging this:

After a lot of searching around on the internet for 18+ hours (p.s. I coult not understand what the above comment was talking about back when i started though) and debugging nodewebkit and node-java twice from scratch, My temporary workaround solution is to use the code below as suggested by Hubert OG, instead of node-java for my node-webkit project.

var exec = require('child_process').exec;

exec("java myProgram.jar param param", function (error, stdout, stderr) {
    ...
});

Synopsis on my search is that the file nodejavabridge_bindings.node when examined using Dependancy walker as suggested by TooTallNate, the dependancywalker reported not having access to two files : <mypath>\node_modules\java\build\Release\JVM.DLL and <mypath>\node_modules\java\build\Release\NW.EXE.

Which probably means the PATH is not set up somewhere. Just guessing.

This is my environment :

Windows 8.1 x64
Visual Studio 2013
JDK 7 U 71 x86
Python 2.7 x86
Node.js 0.10.35
node-java 0.2.0
nodewebkit 0.10.35

Attaching my journal below in case if anyone has the same trouble and searches it on the internet.

node-webkit node-java journal :
-----------------------------------------------------------
No node.js installed. No Java installed. No Python Installed
Installed node.js Windows 32 bit.
Installed python 2.x latest 32 bit.
-----------------------------------------------------------
switched to working directory "E:\shift"
npm install npm -g      : For enabling npm everywhere

npm install nodewebkit      : to get node-webkit
npm install java        : Failed. error pointed to node-gyp

npm install node-gyp -g     : worked.

node-gyp clean          : worked
node-gyp rebuild        : error pointed out PYTHON is missing.

    FIX : Set Environment variable PYTHON to C:\Python27\python.exe
          and PYTHONPATH to C:\Python27

node-gyp rebuild        : error pointing out "This is most likely a problem with the java package"

    FIX : Installing JDK 8 u 25 now.

npm install java        : Works perfectly.

Now app doesnt work. "Module did not self register".
Checked in https://www.npmjs.com/package/java only to find that I should have used nw-gyp.

npm install -g nw-gyp
npm install java
cd node_modules/java
nw-gyp configure --target=0.10.5
nw-gyp build

"node_modules/.bin/nodewebkit" app  : "app" is the name of my app.

    Error   : The specified procedure could not be found.
          e:\Shift\node_modules\java\build\Release\nodejavabridge_bindings.node

    FIX : Searched online about updating JAVA_HOME and adding the path of jvm.dll to PATH.

set PATH = %PATH%;C:\Program Files\Java\jdk1.7.0_40\jre\bin\server      : Had to find path of jvm.dll on my own

No use. Above error persists.

Closer inspection at the error points to nodeJavaBridge.js:7 :

    Error: The specified procedure could not be found. 
    e:\Shift\node_modules\java\build\Release\nodejavabridge_bindings.node
        at Error (native)
        at Module.load (module.js:354:32)
        at Function.Module._load (module.js:310:12)
        at Module.require (module.js:362:17)
        at require (module.js:378:17)
        at Object.<anonymous> (E:\Shift\node_modules\java\lib\nodeJavaBridge.js:7:16)
        at Module._compile (module.js:454:26)
        at Object.Module._extensions..js (module.js:472:10)
        at Module.load (module.js:354:32)
        at Function.Module._load (module.js:310:12)

Specified file with line numbers :

    3. process.env.PATH += require('../build/jvm_dll_path.json');

    4. 
    5. var path = require('path');

    6. var binaryPath = path.resolve(path.join(__dirname, "../build/Release/nodejavabridge_bindings.node"));

    7. var bindings = require(binaryPath);

Contents of the jvm_dll_path.json :

    ";C:\\Program Files (x86)\\Java\\jdk1.8.0_25\\jre\\bin\\client"

JDK version is different from specified by node-java documentation. So removed all JDK installations
and installed JDK 7 U 71 and tried to run the build sequence again.

cd node_modules/java
nw-gyp configure --target=0.10.5
nw-gyp build

Error continues.

I execute the js file specifically seperately in node, with a console.log(binaryPath); after var binarypath = ... ;

node nodeJavaBridge.js

    Error   : module.js:356
            Module._extensions[extension](this, filename);
                                         ^
            Error: The specified module could not be found.
            E:\Shift\node_modules\java\build\Release\nodejavabridge_bindings.node

But the file exists! somehow nodejs is not able access that file.

I start searching for "module.js:356" in google and end up here : http://goo.gl/8E51N5
I use dependancy walker to analyze nodejavabridge_bindings.node. Dependancy walker says the following files arent available.

E:\Shift\node_modules\java\build\Release\JVM.DLL
E:\Shift\node_modules\java\build\Release\NW.EXE

I figure it has something to do with PATH.
I try to open the vcxproj file to see what i can find. COuld not find anything. Too deep to focus now.
Changing my course. Not using node-java anymore.

Using this code as suggested by http://stackoverflow.com/a/17849455/1256902 to run java class:

var exec = require('child_process').exec;

exec("java myProgram.jar param param", function (error, stdout, stderr) {
    ...
});

-----------------------------------------------------------
--      End of Journal
-----------------------------------------------------------