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.
Related
I am currently working on an application that is meant to start and stop different software applications independent of their implementation. Therefore, executable Files are added through simple Drag and Drop and executed on Buttonpress.
I already tried different approaches in Java such as Desktop.open(), Runtime.exec(), using ProcessBuilder or Apache Default Executor. While these approaches work fine for the most part, it seems that OpenRefine would neither open up with the OpenRefine.exe nor with the OpenRefine.bat file. Each time I click the button, nothing happends. I already checked if the execution through doubleclick or commandline works, but for some strange reason I can't get it running in Java. Do you have any additional suggestions to what I can try or what might be the issue with this specific application?
Here is a short sample for the ProcessBuilder Approach
ProcessBuilder processBuilder = new ProcessBuilder(String.valueOf(toolPath));
final Process process = processBuilder.start();
App.globalLogger.info("Started Tool with Process Builder");
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
Where toolPath is respectively the java.nio.file.Path of the File that I imported.
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
This question already has answers here:
Bitlocker script to unlock drive
(8 answers)
Closed 6 years ago.
I am trying to unlock a drive secured by bitlocker from Java. As far as I know there are no libs which can help me to handle that, so I was trying it through cmd. Here's the code:
public static boolean unlockDisk(String pwd) throws IOException
{
String[] script =
{
"manage-bde.exe", "-unlock", "D:", "-password",
};
Process process = new ProcessBuilder(script).start();
InputStream inputStream = process.getInputStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
final OutputStream outputStream = process.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream));
writer.write(pwd);
writer.newLine();
writer.close();
System.out.println("--------------------------------------");
System.out.println("Bitlocker log:");
String line;
while ((line = bufferedReader.readLine()) != null)
{
System.out.println(line);
}
bufferedReader.close();
BufferedReader stdError = new BufferedReader(new InputStreamReader(process.getErrorStream()));
System.out.println("Here is the standard error of the command (if any):\n");
String tmp;
while ((tmp = stdError.readLine()) != null)
{
System.out.println(tmp);
}
System.out.println("--------------------------------------");
return true;
}
My Problem
If I execute this java code I get The handle is invalid with Code 0x80070006.
What I already tried
Different JDK Version 32 and 64 Bit Java 8 and Java 7 (JDK 32 complains somehow that it can't find the command manage-bde)
Different combinations of output streams, with and without newline...
Another script command for the processbuilder like "cmd.exe", "/k", "manage-bde.exe", "-unlock", "D:", "-password", or with /c instead of /k
With and without admin rights
Simple *.bat with the command manage-bde.exe -unlock D: -password (which works perfectly)
Locking the drive through a java command (which works perfectly)
The command without -password (which let's bitlocker claim that I have to define how I want to unlock the drive)
I googled around for some time and found others having this problems but in a different way with other applications. So it seems like a very common error message.
My guess
I think it has something to do with how I handle my Java output as Bitlocker input. Maybe I am using the wrong streams to write to.
I can't provide the value of the password within the script variable, because Bitlocker want doesn't accept that way of entering the password. Usually you enter manage-bde -unlock D: -password within the command line and after a few lines of output Bitlocker asks you for the password.
Well I described it as good as I can and hope that someone knows what the problem is.
Any suggestion, even if it just leads to a more precise error message, would be appreciated. If you have any questions, just let me know!
Thanks in advance!
I encountered same problem recently. I did a lot search. It seems that mange-bde.exe doesn't read user input from stdin. Someone said ssh client and telent clent running on Linux doesn't read password from stdin. Another example Linux command passwd. It has a flag called -stdin which enable the shell to read password from stdin. Therefore, I guessed manage-bde.exe may works in a similar way.
My solution is simulating keyboard input. The awt package can do the job.
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!
I am trying to interact with a simple .exe I created from some Python code. I have tested the .exe through Windows cmd and it works just fine. When I try to send the same input to the .exe through my java program to produce the graphs I need, OutputStream just writes "error" to the console. I have tried sending a string and an integer through OutputStream but the same results are obtained no matter what. I have already interacted with X-Foil.exe, a console application use to produce airfoil data files, with great success through this same java app. As I have to preform curve fitting to the data, I used Python with the matplotlib plugin then used py2exe to create the .exe. I am trying to create a web application with the end goal of designing an aircraft wing, hence the use of java. Here is the method being use which I am having the problem with:
public void PyGrapher(String NACA_4d) {
try {
ProcessBuilder builder = new ProcessBuilder("PyAirfoilGraphing\\dist\\GraphPolars.exe");
builder.redirectErrorStream(true);
Process pr = builder.start();
OutputStream out = pr.getOutputStream();
BufferedReader in = new BufferedReader(new InputStreamReader(pr.getInputStream()));
double CL_alpha;
out.write((NACA_4d + "\n").getBytes());
System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());
System.out.println(in.readLine());
//CL_alpha = Double.parseDouble(in.readLine());
pr.waitFor();
pr.destroy();
out.close();
in.close();
} catch (IOException | InterruptedException ex) {
}
}
Here is what I have read in from the console:
Input NACA 4-digit code: error
Traceback (most recent call last):
File "GraphPolars.py", line 16, in <module>
IOError: [Errno 2] No such file or directory: '..\\..\\AirfoilPolars\\NACA_0024.dat'
I am stumped, and have been for quite some time. There is not a problem with the python file, and it runs fine by itself in the current directory. Can anyone help, please?
-Nick K
Could it be something with relative paths? Python default path may not be picking up the local directory/file.
Did you try passing it the full windows path, 'C:\Foo\bar\AirFoil\... ' with properly escaped '\' characters?