1

I am trying to write a sample code to create an instance of a java class and then invoke a method using that instance. I am using node-java module to do this. The code compiles without any error. However when I hit the URL which actually hits the same code then I get the class not found exception.

I have verified that the jar and it is there in the same directory as index.js and the jar also contains the class file (Application.class) for which the instance is being created.

My index.js file

var java = require("java");
java.classpath.push("demo.jar");

var express = require('express');
var router = express.Router();
var Application = java.import('Application');

/* GET home page. */
router.get('/', function(req, res) {
var application = new Application();
var resp = application.getResponse();
res.render('index', { title: resp });
});

module.exports = router;
jgillich
  • 71,459
  • 6
  • 57
  • 85
Sashi
  • 135
  • 2
  • 11
  • Are you sure that your `CLASSPATH` is set correctly so that your `Application.class` is in one of the directories in that environment variable? – mscdex Aug 11 '14 at 13:15
  • Yes, my Application.class is in demo.jar which is in the same directory as index.js – Sashi Aug 11 '14 at 15:12

1 Answers1

1

Sorry for my english. I had the same problem. Look [https://github.com/joeferner/node-java/issues/147] my code:

`var java = require("java");
 var path=require("path");
 var fs=require("fs");
 console.log("ruta in directory",path.join(__dirname));
 console.log("exist file:",fs.existsSync(path.resolve(__dirname,"./lib-java/lib-tgd.jar")));
 java.classpath.push("commons-lang3-3.1.jar");
 java.classpath.push("commons-io.jar");
 java.classpath.push(path.resolve(__dirname,"./lib-java/lib-tgd.jar"));
 java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-annotations- 2.5.1.jar"));
 java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-core-2.5.1.jar"));
 java.classpath.push(path.resolve(__dirname,"./lib-java/jackson-databind-2.5.1.jar"));`

with path.resolve solves the problem of the file path