I have this chess engine Rybka.exe, that i have to execute in java
Here is an example how you run Rybka:
Once you clicked on it, console opens and waits for input.
So then you enter "uci" and you press enter and you wait for it to load (approx. 1 sec) and then you have to enter a few more lines as options and stuff.
The problem is that I don't know how to pass those commands from java to Rybka. The fact is that those commands need to be entered one at a time, because you have to wait for some to execute.
This is how I tried to open it.
Code:
Process p1 = Runtime.getRuntime().exec("Rybka.exe");
This works, because you can see that Rybka.exe is active in task manager, but I don't know how to pass commands to it.
a) how to bind a windows console application with java application?
link provided by the courtesy of Google search query:
https://www.google.pl/search?q=java+binding+console+to+an+app&ie=utf-8&oe=utf-8&aq=t&rls=org.mozilla:en-US:official&client=firefox-a
b) in short:
InputStream is = p1.getInputStream();
OutputStream os = p1.getOutputStream();
(supplied by the obvious http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Process.html)
Have you tried passing parameters/commands as below?
Runtime.getRuntime().exec("Rybka.exe parameter1,parameter2");
Related
I'm writing a plugin in Intellij Idea which executes commands on thirdParty product side and prints the output of the commands afterwards. Everything should be done in self-written ToolWindow of Intellij Idea. The idea is to create an instance of ConsoleView with ProcessHandler.
For example:
I input "some command" in consoleView
Then I'm pressing "Enter" and command is sent to external API which executes this command
API returns the result
I take this result and print it to consoleView.
So the major question is: I already have an instance of consoleView (TerminalExecutionConsole) and I am able to print output of the commands into my consoleView. (Currently I'm using simple textField to write the command and button to send this command to external API). I would like to type commands directly in TerminalConsoleView and execute them by pressing "Enter". Is there any way to type commands into user-defined consoleView directly like it was done in Intellij Idea "Terminal" toolWindow?
I already tried GeneralCommandLine but it doesn't do what I needed.
Could you please advise any tips or share any example how to do this?
Explanation of the screenshot_1:
I typed "TIME" command into the text field
Click "Send" button
API processed the command and returned the result of execution.
I captured the output and print it to consoleView using notifyTextAvailable(message) function of processHandler object
screenshot_1
I found this on choco-solver documentation but i do not know how use it with provided choco-solver sample program in order to profile.
NB: I already install cpprofiller in my machine and launch it. So it start a tcp server on port 6565.
Need help please.
First, make sure your code looks like:
try (CPProfiler profiler = new CPProfiler(s1.getSolver(), true)) {
solver.findSolution();
}
Then, you should start CPProfiler first and then run run Java program.
Once you go back to the CPProfiler interface, you should see the search tree being updated.
I am running Matlab2017 on windows 10.
I call a python script that runs some Speech Recognition task on cloud with something like this:
userAuthCode=1;% authentication code for user account to be run on cloud
cmd = ['C:\Python27\python.exe runASR.py userAuthCode];
system(cmd);
When the above command is called, the python script runs the input audio file on the ASR cloud engine, and as it runs, I can see Speech Recognition scores for the audio file from Python in the Matlab console.
I want to do the following:
(1) Execute multiple such commands in parallel. Lets say, I have 2 input audio files (each having different audio segments), and I would like to run the above command 2 times, but in parallel, using separate processes. I was able to create a code snippet that should be able to do this:
for i=1: 2
userAuthCode=i;
cmd = ['C:\Python27\python.exe runASR.py userAuthCode];
runtime = java.lang.Runtime.getRuntime();
pid(i) = runtime.exec(cmd);
end
for i=1:2
pid(i).waitFor();
% get exit status
rc(i) = pid(i).exitValue();
end
Now, when the above code is executed, I can see ASRE scores for data1 ,but not for data 2.
The exit status in variable rc, is 0,1, which confirms this.
The problem is I do not know the cause of the error, as nothing is printed in
Matlab. How can I get error message from Python captured in a java/Matlab
variable so that i could take a look?
The issue could be that multiple Calls to
ASRE in parallel (with different user accounts of course) may not
be supported but I won't know unless I can see the error.
(2) When I run a single command standalone, as mentioned at the start of the post, I am able to see Score messages for each audio segment being printed in the Matlab console, as they are obtained from Python. However, with multi-processing using java.lang.Runtime.getRuntime() and the associated code, no messages appears in the Matlab console. Is there a way to display those messages (I am assuming display might be asynchronous?)
Thanks
sedy
One approach is to use multiprocessing in Python. The results and any error messages can be returned in a list.
Example:
Assuming you have three audio files, your_function will run 3 times in parallel with error messages returned.
import subprocess
from multiprocessing import Pool, cpu_count
def multi_processor(function_name):
# Use a regex to make a list of full paths for audio files in /some/directory
# You could also just pass in a list of audio files as a parameter to this function
file_list = []
file_list = str(subprocess.check_output("find ./some/directory -type f -iname \"*a_string_in_your_aud_file_name*\" ",shell=True)).split('\\n')
file_list = sorted(file_list)
# Test, comment out two lines above and put 3 strings in the list so your_function should run three times with 3 processors in parallel
file_list.append("test1")
file_list.append("test2")
file_list.append("test3")
# Use max number of system processors - 1
pool = Pool(processes=cpu_count()-1)
pool.daemon = True
results = {}
# for every audio file in the file list, start a new process
for aud_file in file_list:
results[aud_file] = pool.apply_async(your_function, args=("arg1", "arg2"))
# Wait for all processes to finish before proceeding
pool.close()
pool.join()
# Results and any errors are returned
return {your_function: result.get() for your_function, result in results.items()}
def your_function(arg1, arg2):
try:
print("put your stuff in this function")
your_results = ""
return your_results
except Exception as e:
return str(e)
if __name__ == "__main__":
multi_processor("your_function")
I want to implement the expect "interact" command using java. In expect, it's possible to open an ssh session, authenticate and, then, use the "interact" command to give the control back to the user. Is that possible with java? I've tried with expectJ, expect4J and expectForJava but there's little documentation and almost no examples of how to do this. TIA.
Update: for "interact" command reference, please check this out: http://wiki.tcl.tk/3914
"Interact is an Expect command which gives control of the current
process to the user, so that keystrokes are sent to the current
process, and the stdout and stderr of the current process are
returned."
In case anyone is interested, I have added basic interactive loop support to ExpectIt, my own open source Expect for Java implementation (sorry for self-promotion), since version 0.8.
Here is an example of interacting with the system input stream in Java 8:
try (final Expect expect = new ExpectBuilder()
.withInputs(System.in)
.build()) {
expect.interact()
.when(contains("abc")).then(r -> System.out.println("A"))
.when(contains("xyz")).then(r -> System.err.println("B"))
.until(contains("exit"));
System.out.println("DONE!");
}
System.in.close();
These libraries might suit your needs better:
SSHJ
https://github.com/shikhar/sshj
JSCH
http://www.jcraft.com/jsch/
I must be missing something here, but how do I call something like "cd /root/some/dir/" with Ganymed SSH API?
I created a Connection object
In the first session created, I called "cd /root/some/dir"
In the second session created, I called "ls ." or "./myApp"
That didnt work, because ganymed probably starts each session with its own directory
So do I need to perform both commands on the same session? something like:
session.getStdin().write("cd /root/somedir \n".getBytes());
session.getStdin().write("ls . ".getBytes());
Is that the correct way?? if so, why do we need Session.execCommand?
After doing some research, the only good solution I managed to find is calling the "cd" command within the same code as the "ls" command, like this
session.execCommand("cd /root/somedir ; ls .");
The semicolon will separate the two commands as in any bash code.
In this way, you can query the session's result [session.getExitStatus()] of both the cd and ls commands, which is much better then writing the two commands to session.getStdIn() (after writing to stdin, you kinda loose all the ability to check for exit status...)
Hope this will help the rest
Eyal
According to the Ganymed FAQ (http://www.ganymed.ethz.ch/ssh2/FAQ.html), you are not allowed to send more than one command per Session object you generate. This is how SSH-2 apparently wants you to handle it. Your two options are to either combine the two commands like
session.execCommand("cd /root/somedir ; ls .");
However this wont always work and it get very ugly if you have more than a couple commands. The other way to do this is to open an interactive shell session and write the commands to standard in. This could look something like this:
Session sess = conn.openSession();
sess.requestDumbPTY();
sess.startShell();
OutputStream os = sess.getStdin();
os.write("cd /root/somedir\n".getBytes());
os.write("ls -1\n".getBytes());
os.write("exit\n".getBytes());
InputStream stdout = new StreamGobbler(sess.getStdout());
BufferedReader br = new BufferedReader(new InputStreamReader(stdout));
//TODO
Note the use of the final exit command. Since this is being treated like a terminal window, if you do not exit from the program any loop you have reading the output of the server will never terminate because the server will be expecting more input
OK, I took a quick look on the Ganymed javadoc and although I did not try it myself I assume that you should use method execCommand() of session instead of writing into the STDIN. I am pretty sure that session is connected to remote shell and therefore handles the shell state including current directory, environment variables etc.
So, just do the following:
session.execCommand("cd /root/somedir \n".getBytes());
session.execCommand("ls . ".getBytes());
I hope this will work for you. Good luck.