Skip to content

Instantly share code, notes, and snippets.

@jinwei233
Created September 16, 2011 13:01
Show Gist options
  • Save jinwei233/1222071 to your computer and use it in GitHub Desktop.
Save jinwei233/1222071 to your computer and use it in GitHub Desktop.
nodejs 中调用 Java 程序的最简单的例子
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;
}
//中文注释
@no7dw
Copy link

no7dw commented May 21, 2013

@jinwei233
Copy link
Author

google 搜索 “nodejs 调用 java” ,居然第一个链接就到这里来了,特此补充一篇blog:

http://tomycat.github.io/blog/other/2011/11/29/nodejs-call-java.html

@shmnhdouble2013
Copy link

哈哈~

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment