admin管理员组文章数量:1399887
I'm building an application that uses Java inside Node.js. I made a Function to check the Java version:
function javaversion() {
var spawn = require('child_process').spawn('java', ['-version']);
spawn.stderr.on('data', function(data) {
data = data.toString().split('\n')[0];
var javaVersion = new RegExp('java version').test(data) ? data.split(' ')[2].replace(/"/g, '') : false;
if (javaVersion != false) {
// TODO: We have Java installed
} else {
// TODO: No Java installed
}
});
}
But for systems that Java is not installed, Node.js is throwing ENOENT error, because the module "child_process" just can't spawn the process. How can I verify if Java is installed from Node.js?
I appreciate your help!
I'm building an application that uses Java inside Node.js. I made a Function to check the Java version:
function javaversion() {
var spawn = require('child_process').spawn('java', ['-version']);
spawn.stderr.on('data', function(data) {
data = data.toString().split('\n')[0];
var javaVersion = new RegExp('java version').test(data) ? data.split(' ')[2].replace(/"/g, '') : false;
if (javaVersion != false) {
// TODO: We have Java installed
} else {
// TODO: No Java installed
}
});
}
But for systems that Java is not installed, Node.js is throwing ENOENT error, because the module "child_process" just can't spawn the process. How can I verify if Java is installed from Node.js?
I appreciate your help!
Share Improve this question asked Nov 1, 2013 at 20:01 FormigaFormiga 1111 silver badge12 bronze badges 3- have you considered checking for an environment variable like classpath or ...? And then looking for the binary? – WiredPrairie Commented Nov 1, 2013 at 21:42
- @WiredPrairie, Yes, I considered, but it is not a good idea to all systems. For example, in OS X 10.8, CLASSPATH is not set. I solved the problem handling the error of not spawning the process, like Sudsy suggested. Thank you. – Formiga Commented Nov 2, 2013 at 0:09
- I've got Java installed (per the Windows installer), but it's not in the path, so this method fails pletely. So, doesn't really seem like a good solution for "all systems." – WiredPrairie Commented Nov 2, 2013 at 1:40
1 Answer
Reset to default 7What about this?
function javaversion(callback) {
var spawn = require('child_process').spawn('java', ['-version']);
spawn.on('error', function(err){
return callback(err, null);
})
spawn.stderr.on('data', function(data) {
data = data.toString().split('\n')[0];
var javaVersion = new RegExp('java version').test(data) ? data.split(' ')[2].replace(/"/g, '') : false;
if (javaVersion != false) {
// TODO: We have Java installed
return callback(null, javaVersion);
} else {
// TODO: No Java installed
}
});
}
javaversion(function(err,version){
console.log("Version is " + version);
})
本文标签: javascriptVerify if Java is installed from NodejsStack Overflow
版权声明:本文标题:javascript - Verify if Java is installed from Node.js - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744175279a2593970.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论