2013-05-22 4 views
0

Java 클래스에서 node.js 스크립트를 실행하려고하면 다음 오류가 발생합니다. java.io.IOException : "node events.js"프로그램을 실행할 수 없습니다 : 오류 = 2, 그런 파일이나 디렉토리가 없습니다.java 응용 프로그램에서 node.js 스크립트를 실행할 때의 문제

여기 내 코드는 누군가 내가 무엇을 놓치고 말해 줄 수 있습니까?

public class NodeInitializer { 
    private static final Logger logger = Logger.getLogger(SpringLauncher.class);  
    private Process nodeProcess; 
    ProcessBuilder processBuilder; 

    public void start(){ 
     try { 
      processBuilder = new ProcessBuilder("node events.js"); 
      nodeProcess = processBuilder.start(); 
     } catch (IOException e) { 
      logger.error(e.getCause(), e); 
     } 
    } 
} 

답변

1

사용

List<String> commands = new LinkedList<String>(); 
commands.add("node"); 
commands.add("event.js"); 

ProcessBuilder processBuilder = new ProcessBuilder(commands); 
processBuilder.start(); 

내 경험에

a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent. For example, it is common for each conceptual argument to be an element in this list, but there are operating systems where programs are expected to tokenize command line strings themselves - on such a system a Java implementation might require commands to contain exactly two elements.

는 대부분의 운영 체제의 당신이 실행하려는 명령의 요소를 토큰 화하는 데 필요한 javadoc for ProcessBuilder 상태, 그래서 [node] (the command/program) [events.js] (the argument)이 같은 요소 목록에. 자바는 OS를 통해이 아닌 프로그램과 프로그램의 인수로, 프로그램으로 "노드 events.js"를 실행하려고하기 때문에

당신은

java.io.IOException: Cannot run program "node events.js": error=2, No such file or directory 

을 얻고있다.

+0

당신이 말한대로 논쟁의 lis를 편집 해 사용했지만, 지금은 무엇을 얻을 수 있습니까? java.io.IOException : "node"프로그램을 실행할 수 없습니다 : error = 2, No such file or directory and its system Path, do 변수를 설정해야합니까? –

+0

@ B.TIger 명령 줄에서 정상적으로 실행하면 작동합니까? –

+1

@ B.TIger는 노드의 전체 경로를 넣으려고합니다. 예 : c : \\ nodejs \ node – fmodos

관련 문제