通过java进程执行node脚本

package exec;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;

import org.omg.CORBA.portable.InputStream;

public class Main {
	public static void main(String[] args) {
		System.out.println(System.getProperty("user.dir"));
		try {
			Process p = null;
			String line = null;
			BufferedReader stdout = null;	
			String command = "node ./test.js";
			p = Runtime.getRuntime().exec(command);
			stdout = new BufferedReader(new InputStreamReader(
					p.getInputStream()));
			while ((line = stdout.readLine()) != null) {
				System.out.println(line);
			}
			stdout.close();

			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

输出:

当前路径为工程路径,所以脚本放在工程目录下即可

你可能感兴趣的:(JAVA后台,Node.js)