Java redirect input and output stream - java

To excecute SENNA in the terminal I use the command:
senna.exe < input.txt > result.txt
Now I want to realize this in a java program. This is my code so far
ProcessBuilder builder = new ProcessBuilder("senna.exe");
builder.redirectErrorStream(true);
Process process = builder.start();
OutputStream stdin = process.getOutputStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader (new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
writer.write("This is a test sentence");;
writer.flush();
String line;
while ((line = reader.readLine ()) != null) {
System.out.println ("Stdout: " + line);
}
To redirect the input, output and error stream I used the code from this thread. The problem is that I get the following error message:
FATAL ERROR: unable to open file hash/words.lst
Am I doing something wrong?

From the examples you've given it seems that you're adapting Linux code from this thread to run on Windows with senna.exe.
From the error you're getting it seems that you forgot to change Linux's forward slash (/) to Windows' backslash (\).
Try changing your filepath's forwardslash to backslash.

As far as I can see, you haven't set the directory path to ProcessBuilder object. This error seems to be because there is a folder called 'hash' in senna folder which can't be reached.
Please try this:
builder.directory(new File("/yourpathtosenna/senna/")); (I am on a linux machine)
Your error should most probably change, but I am not sure if you will get the output as I am also struggling with running senna interactively through Java on a Linux machine at the moment.
Good luck and do update if you are successful!

Related

Having a hard time with linux command calls in Java with exec()

I'm trying to use Runtime.getRuntime().exec() to call a program as if it was called from the terminal, but it just crashes with a fatal error after reading the first file.
In the terminal I run the command like so:
mace4 -c -f inputFile.in > outputFile.out
It works as expected, reading from the first file and outputting in the second one.
In Java I try to run it this way:
String args[] = new String[]{"mace4", "-c", "-f", inputFileName ,">",outputFileName};
try {
String s;
Process proc = Runtime.getRuntime().exec(args, null, new File("/home/user/workDirectory/"));
BufferedReader br = new BufferedReader(
new InputStreamReader(proc.getInputStream()));
while ((s = br.readLine()) != null)
System.out.println("line: " + s);
proc.waitFor();
proc.destroy();
As soon as the program reaches the end of the first file, it throws this:
Fatal error: read_all_input, file > not found
The program is quite old and I can't seem to find a way to get a more detailed error out of it..
I tried calling it with these arguments {"sh or bash", "-c", "mace4", "-c", "-f", inputFileName ,">",outputFileName} which makes the program run and then freeze (or at least nothing appears in the console)..
Am I calling the terminal command wrong and if yes what should I change?
PS: this is my first question here, if I missed anything, I'm sorry..
It looks like you're trying to use the Bash output redirection operator >. This redirects the output of the program you're running to a file (or another program)
This answer explains how to do this using ProcessBuilder which should work for what you're trying to do here.
For example:
ProcessBuilder pb = new ProcessBuilder("mace4", "-c", "-f", inputFileName);
pb.redirectOutput(new File(outputFileName));
Process p = pb.start();

Running gnuplot on java

I´m running a java project using gnuplot to generate charts in pdf but I want to save those files in another folder outside my working directory. Is that possible?
I have this for now
Process proc = Runtime.getRuntime().exec("gnuplot test.gp");
BufferedReader reader =
new BufferedReader(new InputStreamReader(proc.getInputStream()));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.print(line + "\n");
}
proc.waitFor();
In gnuplot console check help command line options. There is the option -e.
You need to have the following argument for exec() in Java.
"gnuplot -e myOutput='<YourPDF>' test.gp"
where you have to replace <YourPDF> with your path. Since I do not know Java, the Java-people have to tell you how you get this done.
A minimal gnuplot script would for example look like the following:
set term pdfcairo
set output myOutput
plot sin(x) # or whatever
set output

Execute JAR and read the console output using InputStream - Java

I have a myfile.jar file which I'm executing through another Java program using ProcessBuilder and trying to read the console output of the jar using InputStream as follows:
ProcessBuilder pb = new ProcessBuilder("java", "-jar", "myfile.jar", "arg1");
Process p = pb.start();
InputStream in = p.getInputStream();
InputStreamReader ins = new InputStreamReader(in);
BufferedReader br = new BufferedReader(ins);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
p.waitFor();
p.destroy();
When I execute the above code, I'm not getting anything printed on the console. The myfile.jar was not developed by myself. But when I read through the code of myfile.jar it uses the Logger to print on the console using ConsoleHandler. I assume this may be the issue.
EDIT: When java -jar myfile.jar arg1 is executed standalone on the console, it prints the output on the console.
This is driving me nuts for past 3 days. The direction to solve this problem would help me to learn more.
When processing an external process, you should not only consume/monitor the stdout, but also the stderr.
You could use a second reader to read the stderr stream from the process, but generally it's easier to combine the streams using ProcessBuilder#redirectErrorStream, which redirect the stderr through the processes InputStream.
Sometimes a process will output to the stderr instead of the stdout.
Even if you're not using the output of these streams, it's always a good idea to read them as some processes can stall if the streams are not flushed.

Interfacing CMUSphinx with PyAIML - Input/Output Streams

I have a simple chatbot written in Python using PyAIML and I am running a Java Speech-to-Text system running alongside it.
Now, I want to pipe the output from Java into Python.
Here is what I've tried so far :
File file = new File("C:\\Users\\path\\to\\chat_bot\\");
Process process = Runtime.getRuntime().exec("python chat.py", null, file);
OutputStream stdin = process.getOutputStream();
InputStream stderr = process.getErrorStream();
InputStream stdout = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
while ((line = reader.readLine()) != null) {
System.out.println("Stdout: " + line);
}
String input = in.nextLine();
input += "\n";
writer.write(input);
writer.flush();
Now, I have AIML installed in Python 2.7 but for some reason, the default has been set to Python 3.3
Also, the error I get from Java is :
java.io.IOException: The pipe is being closed
I have changed the Windows Registry to set Python 2.7 as default, but that was of no use.
So, how can I set Java to run the python in the C:\Python27 folder?
I really don't wanna uninstall Python 3.3 just yet.
You should probably use a ProcessBuilder (instead of Runtime.exec). Besides that, you should probably use the full path to the version of python (e.g. use c:\\Python27\\bin\\python instead of just python) that you want.

Running a batch file in Java on a remote server

I'm trying to use Java to interface with a large batch file that uses psexec to execute commands on remote servers.
I'm able to launch the file using process builder and it works fine for most commands, but seems to be getting hung up.
One particular command from the batch file is as follows:
ECHO .
Echo Which would you like to reboot?
Echo 1-10. For computers, enter computer number.
Echo E. Exit
set /p userinp=choose a number(0-22):
but from Java I get:
.
Which would you like to reboot?
1-10. For computers, enter computer number.
E. Exit
and then it hangs
It's clearly not reading the set line, but more importantly I haven't yet figured out how to pass input back to the subprocess.
String[] command = {"cmd", "/c", "batchfile", "restart"};
ProcessBuilder builder = new ProcessBuilder(command);
builder.directory(new File("C:\\"));
Process process = builder.start();
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is);
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null) {
System.out.println(line);
}
Any input would be appreciated.
Your batch job requires that you actually provide input in order to proceed, which is why it appears to 'hang'. You need to supply this input to the process, via its output stream. A highly simplified example:
PrintWriter writer = new PrintWriter(process.getOutputStream());
writer.println("10");
writer.flush();
Your process doesn't hang, it is just waiting for some input at the command line, before to proceed.
As you are reading the output from the process via Process.getInputStream(), you can send input back to it using Process.getOutputStream().
public abstract OutputStream getOutputStream()
Gets the output stream of the subprocess. Output to the stream is piped into the standard input stream of the process represented by this Process object.
Implementation note: It is a good idea for the output stream to be buffered.
Returns:
the output stream connected to the normal input of the subprocess.

Categories

Resources