Verify if Java is installed from Node.js

前端 未结 1 797
生来不讨喜
生来不讨喜 2020-12-07 02:58

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(\'c         


        
相关标签:
1条回答
  • 2020-12-07 03:47

    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);
    })
    
    0 讨论(0)
提交回复
热议问题