javascript - Verify if Java is installed from Node.js - Stack Overflow

I'm building an application that uses Java inside Node.js. I made a Function to check the Java ver

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
Add a ment  | 

1 Answer 1

Reset to default 7

What 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);
})

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744175280a4561724.html

相关推荐

  • javascript - Verify if Java is installed from Node.js - Stack Overflow

    I'm building an application that uses Java inside Node.js. I made a Function to check the Java ver

    9天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信