简体   繁体   English

使用Express Framework和Node.js时在运行时出现java.lang.NoClassDefFoundError

[英]java.lang.NoClassDefFoundError at runtime when using express framework and node.js

I am trying to write a sample code to create an instance of a java class and then invoke a method using that instance. 我正在尝试编写示例代码来创建Java类的实例,然后使用该实例调用方法。 I am using node-java module to do this. 我正在使用node-java模块来执行此操作。 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. 但是,当我击中实际上击中相同代码的URL时,我得到未找到的类异常。

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. 我已经验证了jar和它在与index.js相同的目录中,并且jar还包含要为其创建实例的类文件(Application.class)。

My index.js file 我的index.js文件

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;

Sorry for my english. 对不起我的英语不好。 I had the same problem. 我有同样的问题。 Look [ https://github.com/joeferner/node-java/issues/147] my code: 看[ https://github.com/joeferner/node-java/issues/147]我的代码:

`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 使用path.resolve解决了文件路径的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM