Problem developing application which invokes linux exceutable
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Problem developing application which invokes linux exceutable
Hi All,
i am again stuck with the same problem. I need to run an executable which i got from University of Edinburgh. I have to pass various parameters to it as follows:-
File to run (Executable) :/home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses
The parameters:"-f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/corpus/tuning/mert/moses.ini","-v","2".
The output and the intermediate steps(like loading of other files etc.) of this executable (moses), i want them in a textfield/TextArea.
When i run this executable from outside the application, i get following results and intermediate calls.
Command:-echo " what is the main obstacle in your work ?"| TMP=/tmp /home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses -f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/mert/moses.ini
the intermediate and final results after executing this command are as follows:-
Defined parameters (per moses.ini or switch):
config: /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/mert/moses.ini
distortion-file: 0-0 wbe-msd-bidirectional-fe-allff 6 /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/reordering-table.wbe-msd-bidirectional-fe.gz
distortion-limit: 6
input-factors: 0
lmodel-file: 0 0 3 /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/hindi_lm5.lm
mapping: 0 T 0
ttable-file: 0 0 0 5 /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/phrase-table.gz
ttable-limit: 20
v: 0
weight-d: 0.000181 0.001952 0.001871 0.000044 0.000035 -0.001831 0.001144
weight-l: 0.006173
weight-t: 0.000006 0.015365 0.159487 0.000179 -0.757147
weight-w: -0.054585
Loading lexical distortion models...have 1 models
Creating lexical reordering...
weights: 0.002 0.002 0.000 0.000 -0.002 0.001
Loading table into memory...done.
Start loading LanguageModel /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/hindi_lm5.lm : [4.000] seconds
/home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/hindi_lm5.lm: line 417: warning: non-zero probability for <unk> in closed-vocabulary LM
Finished loading LanguageModels : [4.000] seconds
Start loading PhraseTable /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/phrase-table.gz : [4.000] seconds
filePath: /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/phrase-table.gz
Finished loading phrase tables : [13.000] seconds
IO from STDOUT/STDIN
Created input-output object : [13.000] seconds
Translating: what is the main obstacle in your work ?
Collecting options took 0.000 seconds
Search took 0.200 seconds
BEST TRANSLATION: क्या तुम्हारे obstacle|UNK|UNK|UNK काम मुख्य ? [111111111] [total=-104.119] <<-16.000, -6.000, -100.000, -1.609, 0.000, -3.530, 0.000, -1.946, -5.590, -43.510, -10.792, -14.615, -0.992, -5.737, 4.999>>
क्या तुम्हारे obstacle काम मुख्य ?
Translation took 0.200 seconds
Finished translating
End. : [13.000] seconds
So i want all the result mentioned above to be placed within a TextArea
i have written a java code for the same as follows:-
Code:
private void jButton1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MousePressed
// TODO add your handling code here:
Process p1,p2,p3;
String s=null,s2=null,kk=null,s1[];
s=jTextField1.getText();
System.out.println(s);
jTextArea1.append("Input String is::");
//jTextArea1.append(s);
try
{
String[] args = { "cat", System.getenv("HOME") + "/hindi.txt" };
//String[] args = { "cat", "~/hindi.txt" };
Process p = Runtime.getRuntime().exec(args);
InputStream o = p.getInputStream();
byte[] bytes = new byte[4096];
for (;;)
{
int nread = o.read(bytes);
if (nread == -1) break;
jTextArea1.append(new String(bytes, 0, nread));
}
jTextArea1.append("Source Sentences are :");
jTextArea1.append(null);
jTextArea1.append(jTextField1.getText());
jTextArea1.append(null);
jTextArea1.append("Calling Moses Decoder...\n\n");
String [] arg1={"echo","$s", "|TMP=/tmp","/home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses","-f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/corpus/tuning/mert/moses.ini","-v","2"};
System.out.println(s);
Process pro2=Runtime.getRuntime().exec(arg1);
InputStream o1=pro2.getInputStream();
byte[] bytes1 = new byte[4096];
for (;;)
{
int nread = o.read(bytes1);
if (nread == -1) break;
jTextArea1.append(new String(bytes1, 0, nread));
}
}catch(Exception e){
System.out.println(e);
}
}
Please tell how to solve this problem ?
Thanking you in advance....
Regards
ntu929
Re: Problem developing application which invokes linux execuable
Once again telling my problem... But now in brief...
i want to develop a application which calls and displays the intermediate results(calling of various files, etc) of a executable file in linux..
location of executable:/home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses
This execuable also requires the following parameters:-
"-f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/corpus/tuning/mert/moses.ini","-v","2".
When i run this executable (moses) from outside the application i get the following result:- Result:-
I need to run an executable which i got from University of Edinburgh. I have to pass various parameters to it
Quote:
Originally Posted by ntu929
The output and the intermediate steps(like loading of other files etc.) of this executable (moses), i want them in a textfield/TextArea.
Do you mean you want to run the executable via your browser, and not in a normal Linux desktop environment (or SSH connection, or remote X session)? Or are you after a local GUI to run it? Or a script to run it and create a set of offline HTML files, for browsing later?
Or are you after a local GUI to run it? Or a script to run it and create a set of offline HTML files, for browsing later?
Yes, this application will consists of local GUI built using JSwing. This GUI will call moses ( with its paramters) and will display its result in a TextArea.
As per my understanding u need a piping kinda mechanism.
You already have an app 'A' running, u need one more, another app 'X' to run and generate some output and exit. You need to display tht output in app 'A'.
Now, I read ur prog and it seems to be working, though I am not expert JAVA guy, but I read some sample programs like urs.
Re: Problem developing application which invokes linux execuable
Hello,
Quote:
Originally Posted by vdx
As per my understanding u need a piping kinda mechanism.
Please tell what should be the exact code for piping. If i can understand correctly it means making use of pipe mechanism which we have in linux commands..(ls|more).
The arg1 contains the string to invoke moses executable along with all its parameters.
Code:
Process pro2=Runtime.getRuntime().exec(arg1);
This will run moses...
The problem mainly starts from here. I want to get whatever is executed by moses should be shown in a textarea of this application (GUI based)(just as you had mentioned regarding piping). Now for this i have following code :-
Code:
InputStream o1=pro2.getInputStream();
byte[] bytes1 = new byte[4096];
for (;;)
{
int nread = o.read(bytes1);
if (nread == -1) break;
jTextArea1.append(new String(bytes1, 0, nread));
}
}catch(Exception e){
System.out.println(e);
}
Quote:
Originally Posted by vdx
You did good. Where exactly are u facing prob ?
Now the problem is that this total code does not run at all. Means that nothing gets displayed in the textarea.
import java.io.*;
public class JavaRunCommand {
public static void main(String args[]) {
String s = null;
try {
// run the Unix "ls" command
Process p = Runtime.getRuntime().exec("ls");
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
// read the output from the command
System.out.println("Here is the standard output of the command:\n");
while ((s = stdInput.readLine()) != null) {
System.out.println(s);
}
System.exit(0);
}
catch (IOException e) {
System.out.println("exception happened - here's what I know: ");
e.printStackTrace();
}
}
}
Here, you already have output in Process object(here 'p'). The only thing u need to do is reading tht string and appeding it to ur textfield.
Google for how to get string from BufferedReader object and u are done.
After executing above code i get the arguments of arg1 in jTextArea...
Just clarifying once again in case i was misunderstood...
-i want to execute moses executable file. this file must be executed from withina GUI based application.
-The result of moses executable file must be displayed in the a textArea of this GUI application.
First
======
Please make sure that ur binary moses executes successfully(and gives output) on commandline by executing the below command
Code:
/home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses -f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/corpus/tuning/mert/moses.ini -i s -v 2
If it doesnt, it may have some environmental issues (like PATH)
Second
=======
We are just executing a command inside a new process by below
Code:
Process pro2=Runtime.getRuntime().exec("COMMAND STRING");
So whatever u write inside the "COMMAND STRING", It will got executed and 'Runtime' object will capture the output.
Nothing difficult in this. Please do some digging with google(ur best friend).
Third
=======
Quote:
stdInput.readLine() evaluates to NULL always and so while loop gets never executed.....
because the command isnt executed successfully....For this u can get help with the below
The getErrorStream() method will give u the error encountered after executing the binary(if any).
i have used getErrorStream() in case of getOutputStream. i get the partial output from the execuable (moses).
When i now try to run the execuable from outside the application it runs with a different output.
Is it possible that getErrorStream/OutputStream could interfere with the exec of moses. Now even outside of the application is giving different results.
Is it possible that getErrorStream/OutputStream could interfere with the exec of moses. Now even outside of the application is giving different results.
Nops, It never interfere with ur app. It only executes ur app in new process and gets the output in string.
Now, It may be environmental issues. When u run ur app inside a terminal, It already having the controlling terminal and env variables.
But its diff when run ur app inside the Runtime.getruntime.exec("app"). You need to consider abt this too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.