Created
September 16, 2011 13:01
-
-
Save jinwei233/1222071 to your computer and use it in GitHub Desktop.
nodejs 中调用 Java 程序的最简单的例子
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var spawn = require('child_process').spawn; | |
var cmds = [["/c","dir"],["/c","java"],["/c","java","-jar"]] | |
// cmds.forEach(function(v){ | |
// v && exe(v); | |
// }); | |
//YUI compressor jar file folder | |
var ori = __dirname+"/../tools"; | |
// convert posix-style path to dos-style path | |
var toolPath = posix2dos(ori); | |
// absolute path of yuicompressor.jar | |
var Ycompressor = toolPath+"\\yuicompressor.jar"; | |
exe(["/c","java","-jar",Ycompressor,"test.js"]); | |
function exe(command){ | |
var cmd = spawn("cmd",command); | |
cmd.stdout.setEncoding("ASCII"); | |
cmd.stdout.on("data",function(data){ | |
console.log("------------------------------"); | |
console.log("exec",command); | |
console.log("stdout:"+data); | |
}); | |
cmd.stderr.on("data",function(data){ | |
console.log("------------------------------"); | |
console.log("stderr:"+data); | |
console.log("------------------------------"); | |
}); | |
cmd.on("exit",function(code){ | |
console.log("exited with code:"+code); | |
console.log("------------------------------"); | |
}); | |
}; | |
function posix2dos(path){ | |
/* | |
/cygdrive/d/att -> d:\\att | |
*/ | |
var re = new RegExp(/^\/cygdrive\/(\w)/); | |
path = path.replace(re,function(match,drive){ | |
//console.log(drive); | |
return drive+":"; | |
}).replace(/\//g,'\\'); | |
return path; | |
} | |
//中文注释 | |
google 搜索 “nodejs 调用 java” ,居然第一个链接就到这里来了,特此补充一篇blog:
http://tomycat.github.io/blog/other/2011/11/29/nodejs-call-java.html
哈哈~
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://github.com/nearinfinity/node-java