开发者

Calling a preprocessor tool from Java

开发者 https://www.devze.com 2023-02-22 06:06 出处:网络
I have a C source that I have preprocessed using MCPP, a preprocessor. I want to parse this preprocessed source with Java parser. For that I need to call it from Java. Is there some API available to h

I have a C source that I have preprocessed using MCPP, a preprocessor. I want to parse this preprocessed source with Java parser. For that I need to call it from Java. Is there some API available to handle such call. I plan to execute DOS commands as a batch file and execute it at Java Runtime. When I used the standard exec method

Runtime r = Runtime.getRuntime();  
Process dos = r.exec("cmd.exe /c C:\\mcpp\\bin\\mcpp.exe -Iinclude csample.c");

It gives error that preprocessor cannot open input file.

Sugge开发者_JAVA百科stions awaited.

cmd parameters edited


ProcessBuilder comes to my mind


What about

Runtime r = Runtime.getRuntime();  
Process dos = r.exec("C:\\mcpp\\bin\\mcpp.exe -Iinclude csample.c");

?


Your problem is that your command is being run in a different directory than you expect. You need to provide the appropriate directory using the three-argument version of exec():

public Process exec(String command,
                    String[] envp,
                    File dir)
             throws IOException

where the third argument gives the current directory for the command being executed. Make sure to set it to the directory where the input is located.

EDIT: Example:

r.exec("cmd.exe /c C:\\mcpp\\bin\\mcpp.exe -Iinclude csample.c", 
       null, // inherit current process environment
       new File("/path/containing/csample.c"));
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号