0

I am using https://github.com/angular/quickstart

npm install
npm install java --save
node node_modules/java/postInstall.js
npm install @types/java --save-dev

package.json

"dependencies": {
    ...
    "java": "^0.8.0",
    ...
},
"devDependencies": {
    ...
    "@types/java": "^0.7.31",
    ...
},

This is how I added the library in systemjs.config.js

map: {
  ...
  'java': 'npm:java/lib/nodeJavaBridge.js'
},

When I run npm start the output is

Terminal:

304 GET /java/lib/nodeJavaBridge.js
304 GET /java/build/jvm_dll_path.json

404 GET /lodash
404 GET /async
404 GET /path
404 GET /fs

Chrome dev tools console:

http://localhost:3000/lodash Failed to load resource: the server responded with a status of 404 (Not Found)

ZoneAwareError__zone_symbol__error: Error: (SystemJS) XHR error (404 Not Found) loading http://localhost:3000/lodash
    Error: XHR error (404 Not Found) loading http://localhost:3000/lodash
        ...
    Error loading http://localhost:3000/lodash as "lodash" from http://localhost:3000/node_modules/java/lib/nodeJavaBridge.js
        ...

http://localhost:3000/async Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/path Failed to load resource: the server responded with a status of 404 (Not Found)
http://localhost:3000/fs Failed to load resource: the server responded with a status of 404 (Not Found)

The code to test that I can load node-java and run java code is something like:

import { Component, OnInit} from '@angular/core';
import * as java from 'java';

@Component({
  selector: 'my-app',
  template: `<h1>Hello {{name}}</h1>`,
})
export class AppComponent  implements OnInit { 
    name = 'Angular';

    ngOnInit() {
        var sys = java.import('java.lang.System');
        sys.out.printlnSync('Hello from java :)'); // outputs to terminal
        var list1 = java.newInstanceSync("java.util.ArrayList");
        console.log(list1.sizeSync()); // 0
        list1.addSync('item1');
        console.log(list1.sizeSync()); // 1
    }
}

But the code never gets called because of the missing libraries.

The node_modules/java/lib/nodeJavaBridge.js contains these lines:

var _ = require('lodash');
var async = require('async');
var path = require('path');
var fs = require('fs');

How could I load the node-java library in the angular 2 quickstart app correctly ?

cosma
  • 1
  • angular isn't node, it runs in the browser. It may include node as a convenience for spinning up a development server, but it doesn't run in the node environment. – Claies Feb 24 '17 at 00:20
  • Can you provide me with a solution to loading the library? Or are you saying it can't work because of the node fs dependency? – cosma Feb 24 '17 at 08:03
  • I'm definitely saying that it isn't a compatible library with angular, for a variety of reasons not the least of which is the `fs` dependency. `fs` is short for `filesystem`, and browsers don't have access to the filesystem at all, therefore neither does angular. – Claies Feb 24 '17 at 10:46
  • if you were to overcome this dependency issue, the library still probably wouldn't perform as expected, due to node apps having their own context, while browser apps have a `window` context instead. – Claies Feb 24 '17 at 10:53
  • Thank you for clarifying that @Claies . The only solution I could come up with so far has been to run the ng2 app with electron and make use of it's ipcMain and ipcRenderer modules: http://stackoverflow.com/questions/36286592/how-to-integrate-electron-ipcrenderer-into-angular-2-app-based-on-typescript – cosma Feb 24 '17 at 12:38

1 Answers1

0

SystemJS doesn't know how to load those node-java dependencies, you need to tell it where to find them in your systemjs.config.js file, e.g.:

map: {
  ...
  'java': 'npm:java/lib/nodeJavaBridge.js',
  'lodash': 'npm:lodash/lodash.js',
  'async': 'npm:...',
  'path': 'npm:...',
  'fs': 'npm:...',
},
Saeb Amini
  • 23,054
  • 9
  • 78
  • 76
  • I was able to install lodash async and path using npm install and referencing them in map as you suggested to the point where I am not getting 404 but I could not find a npm lib for fs. What lib could I use for fs? – cosma Feb 24 '17 at 08:10