I have the following Folder structure:
- Project
- Lexer
- mylexer (this is a C executable program)
- Lexer
- Parser
- MyJavaFile.java
From the java file in parser I want to execute the mylexer program and wait for a result. I have the following code:
public static String getTokensFromFile(String path) {
String s = null;
StringBuilder sb = new StringBuilder(path);
try {
Runtime rt = Runtime.getRuntime();
String[] command = {"mylexer", "<", path, ">", "output.txt"};
ProcessBuilder pb = new ProcessBuilder(command);
pb.directory(new File("../Lexer"));
Process pr = pb.start();
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(pr.getInputStream()));
BufferedReader stdError = new BufferedReader(new
InputStreamReader(pr.getErrorStream()));
while ((s = stdError.readLine()) != null) {
sb.append(s+"\n");
}
}catch(Exception e) {
System.out.println(e);
}
return (sb.toString().length() > 0)? sb.toString() : "";
}
I didn't get any result, the program never ends the execution, and if I do this String[] command = {"./mylexer", "<", path, ">", "output.txt"};
It says that The file is not found. How can I achieve that?
Also I did this on my terminal
../Lexer/mylexer < /Users/jacobotapia/Documents/Compiladores/Proyecto/Lexer/sample.txt > output.txt
But this don't work on Java.