LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-02-2011, 11:13 PM   #1
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Rep: Reputation: 4
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
 
Old 05-03-2011, 04:09 AM   #2
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
please describe ur prob in brief. You wont get reply for this kinda explanations.
 
1 members found this post helpful.
Old 05-03-2011, 05:01 AM   #3
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
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:-

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.
....................

the application will call this executable(moses) and should display the above mentioned output in textarea.

for doing this i have used following code in java:-
Code:
        Process p1,p2,p3; 
        String s=null,s2=null,kk=null,s1[]; 
        s=jTextField1.getText(); 
        System.out.println(s); 
        try 
        { 
        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.
Please tell me again if something is not clear or if i was a nt intelligble...

With Regards,
ntu929.
 
Old 05-03-2011, 04:56 PM   #4
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Quote:
Originally Posted by ntu929 View Post
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 View Post
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?
 
1 members found this post helpful.
Old 05-03-2011, 11:26 PM   #5
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Re

Quote:
Originally Posted by Nominal Animal View Post
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.
 
Old 05-04-2011, 02:22 AM   #6
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
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.

You did good. Where exactly are u facing prob ?
 
1 members found this post helpful.
Old 05-04-2011, 11:34 PM   #7
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Smile Re: Problem developing application which invokes linux execuable

Hello,

Quote:
Originally Posted by vdx View Post
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).


let me try to explain my code i a bit...
Code:
        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"};
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 View Post
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.

Please tell how to solve this problem...


Thanks in advance,
ntu929.
 
Old 05-05-2011, 02:43 AM   #8
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Dear ntu929,

Please consider the below small prog.

Code:
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.

Kindly put ur command and params insted of 'ls'.
 
1 members found this post helpful.
Old 05-09-2011, 04:58 AM   #9
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Unhappy

Hi vdx,
Code:
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);
        BufferedReader stdInput=new BufferedReader(new InputStreamReader(pro2.getInputStream()));
     
       String s11="";

       while((s11=stdInput.readLine())!=null)
       {
           jTextArea1.append(s11);
       }

        }catch(Exception e){

            System.out.println(e);
        }

    }
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.

Code:
     BufferedReader stdInput=new BufferedReader(new InputStreamReader(pro2.getInputStream()));
Should we not try to get the outputStream ?

Thanks in advance,
ntu929.
 
Old 05-10-2011, 02:50 AM   #10
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Yeah It would got executed from ur GUI.

Dont use echo in ur arg1. Use like below and u will get ur output in GUI text-area.
Quote:
String arg1="/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"
 
1 members found this post helpful.
Old 05-10-2011, 06:34 AM   #11
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Unhappy

Quote:
Originally Posted by vdx View Post
Yeah It would got executed from ur GUI.

Dont use echo in ur arg1. Use like below and u will get ur output in GUI text-area.

i did not use echo but instead used the following arg1:-
Code:
String [] arg1={"/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"};
But inspite of this, when i execute the above just nothing happens....

i undertook debugging of this application and found that at following code:-

Code:
while((s11=stdInput.readLine())!=null)
       {
           jTextArea1.append(s11);
       }
stdInput.readLine() evaluates to NULL always and so while loop gets never executed.....

Please tell how to solve this....



Thanks in advance,
ntu929.
 
Old 05-11-2011, 02:14 AM   #12
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Dear ntu929,

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
Code:
BufferedReader stdError=new BufferedReader(new InputStreamReader(pro2.getErrorStream()));
The getErrorStream() method will give u the error encountered after executing the binary(if any).
 
Old 05-13-2011, 01:01 AM   #13
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Unhappy

Hi vdx,

Quote:
Originally Posted by vdx View Post
Third
=======


because the command isnt executed successfully....For this u can get help with the below
Code:
BufferedReader stdError=new BufferedReader(new InputStreamReader(pro2.getErrorStream()));
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.

Thanks in advance,
ntu929
 
Old 05-14-2011, 02:08 AM   #14
vdx
Member
 
Registered: Aug 2007
Location: The Greate INDIA
Distribution: CentOS, RHEL, Fedora
Posts: 102

Rep: Reputation: 24
Quote:
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.
 
1 members found this post helpful.
Old 05-16-2011, 06:36 AM   #15
ntu929
Member
 
Registered: Jun 2010
Location: India
Distribution: Ubuntu
Posts: 128

Original Poster
Rep: Reputation: 4
Unhappy Re:Problem

Dear vdx,


i have executed following code
Code:
String s22="abc.txt";
        // String[] arg1 = {"/bin/sh", "-c","ls"};
         String[] arg1 = {"/bin/sh", "-c","/home/nakul/moses/mosesdecoder/trunk/moses-cmd/src/moses -f /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/moses.ini","-i",s22, "-v", "2"};
        System.out.println(s);
        Process pro2=Runtime.getRuntime().exec(arg1);
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(pro2.getErrorStream()));

         InputStream o1=pro2.getInputStream();

            String s11 = stdInput.readLine();
            
            System.out.println(s11);
            while (s11 != null) {

               jTextArea2.append(s11);
                s11=stdInput.readLine();
                System.out.println(s11);
            }
The executable seems to ask for some additional things after showing some results as follows:-

Result is :-
..............
Start loading LanguageModel /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/hindi_lm5.lm : [5.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 : [5.000] seconds
Start loading PhraseTable /home/nakul/moses/mosesdecoder/trunk/scripts/training/moses-scripts/scripts-20110405-1055/training/model/phrase-table.gz : [5.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

After this the executable seems to waiting for some input...

But all the parameters are already given in arg1.

If i execute this file(moses) from outside the application, it asks for input only in case when the paramters are not given properly.

This output should be given when getInputstream is used, but it does not show anything.

Thanks in advance,
ntu929.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Developing StickyNote Application getBoa Programming 2 06-17-2010 03:44 PM
LXer: Developing a real-time Linux data acquisition application LXer Syndicated Linux News 0 08-21-2006 11:54 PM
Developing an application with scilab vrdhananjay Linux - Software 3 12-15-2005 05:26 PM
Developing web application desarrolladores Linux - General 3 06-23-2004 02:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration