How to execute " cat foo | grep something " in java

Hi guys ...
Am trying to execute " cat foo | grep dude " , where foo is a text file which contains a lot of texts and words. I tried a lot of choices but I end up with nothing like -
Process p = Runtime.getRuntime().exec(new String[] {"cat", "foo"}); , and by creating a stream reader on the process I get the content of foo file , but I want to grep something and I tried , Process p = Runtime.getRuntime().exec(new String[] {"cat", "anas","| grep Dude"}); but I got the same result from the above command , I tried a lot of possibilities like -
(new String[] {"cat", "anas","|", "grep", "Dude"});
(new String[] {"cat anas | ","grep Dude"});
and so on , also I tried to put the command in a string and execute it like here
String command = "cat" + " foo" + " |" + " grep Dude" ;
Process p = Runtime.getRuntime().exec(command);
.. I don't know it's like work fine but when it reached the part that related to " grep something" it just ignore it . so can someone help me in this .thanks for reading and thanks in advance for any further help ... by the way this the content of the foo file
Dude I Am here
what !! you cant
see me or you
pretend to be blind !!

if cat and grep are executable programs that work outside of the console app, it should work. But I don't think this is the case, in which case you need to open a console first.
I haven't tried this on Unix, but below is how to do it on Windows. If you wanted to run "more" or "less" or "cd" etc in on dos, you need to open the console first.
          String cmd = "cmd.exe";
          TextAreaOutputStream taos = new TextAreaOutputStream(this);
          try {
               mDOS = Runtime.getRuntime().exec(cmd);
               Piper p = new Piper (mDOS.getInputStream(), taos);
               new Thread(p).start();
               p = new Piper (mDOS.getErrorStream(), taos);
               new Thread(p).start();
               mDosOut = new PrintStream(mDOS.getOutputStream());
          catch (IOException iox) {
               iox.printStackTrace();
          }

Similar Messages

  • How to execute a jsp instruction from a java class?

    How to execute a jsp instruction from a java class?
    Any help please.
    Thank's

    I'll detail my problem:
    Supposing that I have a jsp file called: start.jsp
    In the start.jsp I instanciated an object called global from the class Global ( for example ).
    Then, I called a custom taglib:
    <ix:mytag/>
    In the suitable tld file: the mytag is defined:
    <tag>
    <tag class>Tag1<.....
    The java file Tag1 has method:
    doStartTag(){
    // here is the problem
    global.doSomeFunctions();
    Okay, the problem is that the object gobal is not defined in the class Tag1. what to do?
    I think that I have to pass the object global as a parameter to the class Tag1. How to do that?
    Actually, the problem was to insert the follwoing in the doStartTag method:
    doStartTag(){
    pageContext.getOut().print("<%= hello world %>");
    The output is <%= hello world %> not hello world.
    Okay, the new problem is how to pass an object as a parameter to a class called from a tld file.
    Any help please.
    Thank's too much

  • How to execute a unix/dos command in Java

    Hi,
    I want to execute dos/unix commands in my java program. Can anyone tell me how to do this. Say I want to restart my httpd daemon using the command: "service httpd restart" or test my httpd.conf file using the command "testparn"
    thanks in advance
    Hugo Hendriks

    hallo,
    test this:
    Process p = Runtime.getRuntime().exec(Your_Programm);
    p.waitFor();
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(p.getInputStream()));
    Your_Programm must be a shellscript. regard that your java programm must have the right to start the httpd!
    Carsten Bluetner

  • How to execute an ABAP Mapping after a Java Mapping

    Hi, i have found a Bug in XI and SAP said to me that the only solution is to execute an ABAP mapping after my java mapping.
    I have an IDOC to FILE scenario
    Could anyone orient me on what do i have to configure, so after my java mapping i can execute an abap mapping ?.
    Do i have to use process integration?
    thanks
    Mariano.

    i have developed a Java mapping originally, but XI has a bug. When you do not write anything to the outputStream XI instead of writing an emtpy file writes a file containing one byte ( 0x00 ) and this is a problem.
    SAP said to me that they will not fix this issue so they recomend me to create an abap mapping after my java mapping that will send no information if it receives this byte (0x00) and if not it will write all the information as received.
    My doubt regarding putting several mappings in the interface mapping configuration is this...
    Actual configuration:
    SOURCE MSG A -> ORIGINAL MAPPING -> TARGET MSG B
    Proposed configuration
    SOURCE. MSG A -> ORIGINAL MAPPING -> TARGET MSG B
    SOURCE. MSG A -> PATCH MAPPING -> TARGET MSG B
    I dont understand what receives de "patch mapping" it receives the structure from TARGET MSG B?
    regards
    mariano

  • How to execute a procedure or function from Java Stored procedure

    Hi,
    I am new to Java Stored Procedures. I am working on Oracle 8i and JVM 1.3.1. I want to call a PL/SQL procedure from within Java. I have tried looking at severa; cources but they are quite high level for me. Can someone provide a simple example including the Source Code for .java file and also the calling function's code?
    Heres a sample of what I have been working on: I an including Java code, and Function code and how I call the function. Instead of doing "select sysdate from dual" I want to do like "create table temp1(var1 varchar2(10))" or similar... like "exec procname(var1)" etc.
    Thanks in advance.
    PS. The variable passed in function is just a dummy variable.
    -----Begin Java code-------
    import java.sql.SQLException;
    import java.sql.PreparedStatement;
    import java.sql.Statement;
    import java.sql.Connection;
    import java.sql.ResultSet;
    //Oracle Extensions to JDBC
    import oracle.jdbc.driver.OracleDriver;
    public class Test2{
    public static String Testing(String d) {
    Connection connection = null; // Database connection object
    try {
    // Get a Default Database Connection using Server Side JDBC Driver.
    // Note : This class will be loaded on the Database Server and hence use a
    // Se[i]Long postings are being truncated to ~1 kB at this time.

    what your after is
    Connection conn = DriverManager.getConnection
    ("jdbc:oracle:oci:@<hoststring>", "scott", "tiger");
    CallableStatement cs = conn.prepareCall ("begin ? := foo(?); end;");
    cs.registerOutParameter(1,Types.CHAR);
    cs.setString(2, "aa");
    cs.executeUpdate();
    String result = cs.getString(1);
    a more complete description can be found in the documentation at
    http://download-west.oracle.com/docs/cd/B10501_01/java.920/a96654/basic.htm#1001934
    Dom

  • How to execute solaris lpd printing command from java code

    hi folk,
    I want to print a post script file on a network printer (my java code will receive the Printer's IP Address and the filename ) , i'm using the solaris lpd printing command :
    lpadmin -p banana_ps -o protocol=bsd,dest=IP -v /dev/null
    -m netstandard -T PS -I postscript
    my question is how execute this command from the java code :)
    i really appreciate ur advices

    Hi,
    See RunTime.getRuntime ().exec (...).
    Hope that help,
    Jack

  • How to execute a file using jsp or java

    hi..
    i had a jsp file as front end with start and stop button,
    if i click the start button a file on my pc should execute.
    if stop button is clicked the file should stop execution.
    Any one know how to do this..especially how to run a file in ur system using java..
    Thanks in advance

    thanks for u r quick replies...
    Actually i want to run a file on a client machine
    only...
    Okay.
    for example, a client should execute a file which is
    in another client...
    This is not the same thing as what you just said.
    can u tell me the method to execute the file......java.lang.Runtime.exec

  • How to execute a jar file in a java application

    Hi,
    I've already try to execute a jar file by this way :
    Runtime.getRuntime().exec(java -jar myJarFile.jar);
    But the problem is that the display doesn't launch. Normally a "JFrame" display.
    however the corresponding process is launched.
    So i would to know if there is a way to execute a Jar File without using the Runtime class.
    Is there a specific class for the Jar File?
    Thank you.
    Richard

    Create a 'manifest' file pointing to the 'main' class, jar it into the .jar file, place it onto your desktop - or whereever you can click on it, and click on it.

  • How to execute sh file of unix in java

    iam trying to execute a sh file in target machine which will startup the database from source machine..
    sh file is ..
    # Synopsys
    # This program startup's the database.
    # Parameters
    # @1: tuserconnect
    # @2: tenvfile
    # @3: tUserHome
    progname=ajstartdb
    echo "ssh ${1} . ${2} \&\& ${3}"
    ssh ${1} . ${2} \&\& ${3}/ajss/sql/ajstrtdb.sh
    retval=$?
    echo "DEBUG::${progname}--> Completing execution with return code: $retval" > $progname.out
    If i try to execute this ajstartdb.sh file in target itself it is executing perfectely.So i think no prob's with sh file..
    but when i try to execute this sh file from java.. the process control is remaining in the prgm itself even after the cmd is executed.. i mean prgm is not exiting.
    java prgm is :
    import java.io.*;
    class StartShut1
    public static void main(String[] args)
    try {
    String cmd="ssh [email protected] . /d03/AppsGOLD/golddb/9.2.0/GOLD_hydapps.env \\&\\& /home/oragold/ajss/ajstrtdb.sh";
                             System.out.println(" the cmd is: "+cmd);
         Process p =Runtime.getRuntime().exec(cmd);
         int i = p.waitFor();
         System.out.println("Exit value of process is "+j);
                             System.out.println("Output of the Cmd is : "+i);
         String s=null;
         if (i!=0){
                                  System.out.println("ERROR in executing the command");
                                  BufferedReader cmdr = new BufferedReader(new InputStreamReader(p.getErrorStream()));
         while ((s = cmdr.readLine()) != null) {
                                                 System.out.println(s);
                                                      s = null;
         BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
                                       // read the output from the command
                                       s=stdInput.readLine();
                             System.out.println(s);
         while (s!= null)
         System.out.println(s);
    InputStream in = p.getInputStream();
    int ch;
    StringBuffer sb = new StringBuffer(512);
    while ( ( ch = in.read() ) != -1 )
    { sb.append((char) ch); }
    System.out.println("output from string buffer"+sb);
    }catch(Exception e)
    System.out.println("ERROR :"+e);
    e.printStackTrace();
    }//main
    }//class
    Kindly Reply me if u get an idea

    And also u hv specified & in the cmd ... it is executing when we specify \&\& there.
    Do u hv any idea ragarding this symbol (\&\&) in unix..Let us try first without vowels (the OP tends to drop them):
    & n cmmnd prmpt wll trmnt th cmmnd nd rn t n th bckgrnd.
    && wll wrk lk n C (r Jv fr tht mttr): t wll vlt th frst trm nd g n xctng nd vltn
    g f t ws "tr", tht s, f t yldd "zr" s rtrn cd.
    For the faint the one with vowels:
    & in a command prompt will terminate the command and run it in the background.
    && will work like in C (or Java for that matter): it will evalute the first term and go on executing and evaluating if it was "true", that is, if it yielded "zero" as return code.
    Thus
    posman@proli:~/ivan/fnt> test -d mydir && ls -l mydir
    posman@proli:~/ivan/fnt> mkdir mydir
    posman@proli:~/ivan/fnt> ls >mydir/1
    posman@proli:~/ivan/fnt> test -d mydir && ls -l mydir
    total 4
    -rw-r--r--  1 posman users 522 Oct  4 12:24 1

  • How to execute a .exe file in java(Jsp) without using a process ???

    Hi All ,
    How to execute a .exe file in Jsp without using a process ??? ...
    Is it Possiable ????

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • How to execute stored procedure in sqlplus

    i have procedure something like this
    create or replace "gettimeslots"
    v_acc in varchar,
    slot_cursor in out sys_refcursor
    as
    begin
    open slot_cursor for
    select * from tslots where tid=v_acc;
    end
    how to execute this proc in sqlplus?please help in this regard

    As you perfectly know (since you're a member for more than a year now) you could have looked that up via google or the forum search. Just one link for you:
    SQLPLUS in Morgan's Library!

  • How to execute the hardcode line

    i have a problem that i want to do the looping and copy to the itab_shipmark
    when read 1 line, then copy to itab_shipmark. (the field will change depend on number of looping as sy-tabix)
    let say, there is 7 rows and loop 7 times
    i want to execute when looping
    clear itab_shipmark.
    loop start...
    1st time : move t_tline-tdline to itab_shipmark-row1.
    2nd time: move t_tline-tdline to itab_shipmark-row2.
    7th time:  move t_tline-tdline to itab_shipmark-row7.
    end loop.
    append itab_shipmark.
    see the following draft coding ***************
    CALL FUNCTION 'READ_TEXT'
        EXPORTING
          ID                      = '002'
          LANGUAGE                = SY-LANGU
          NAME                    = THE_TDNAME
          OBJECT                  = 'VBBP'
        TABLES
          LINES                   = T_TLINE1
        EXCEPTIONS
      IF SY-SUBRC = 0.
      CLEAR ITAB_SHIPMARK.
        LOOP AT T_TLINE1.
        tindex = sy-tabix.
        concatenate 'itab_shipmark-row' tindex into tshipmark.
        concatenate 'move t_tline-tdline to' tshipmark into tshipmark.
    ???? (how to execute move t_tline-tdline to itab_shipmark-row1.)
        ENDLOOP.
    APPEND ITAB_shipmark.
      ENDIF.
    ENDFORM.                    "GET_SHIP_MARK

    Hi,
       I think you can use the command INSERT <workarea> INTO <table position>. You can use the Loop index (Number of Loop traversal) as the table index for determining the position at which the new line is to be inserted.
    Follow the F1 help for the command.
    Does that answer the question or I missed something here?
    Regards
    Barada

  • How to execute one .exe file with as3 in air ?

    Hi
    How to execute one .exe file with as3 in air?
    I want do this work without fscommand .
    plize help me .

    itsdhanasaraa wrote:
    But as this a web application ... By using Runtime i'm getting some probs ..
    Let me guess, you want your web application to run a program on the client and to your surprise that's not working?
    Ain't gonna happen.
    its taking more time to execute .... that's y is there any other option to execute .exe file other than Runtime.getRuntime().exec("filename");Write proper English and you may be taken more seriously.
    1) it's not "taking more time to execute", whatever that's supposed to mean.
    2) there's no other way to execute something. Not that you should every use even that way anyway
    3) whenever you start thinking of executing external programs from Java, start thinking of not using Java in the first place.

  • How to execute external command?

    How to execute follow command and get its ouput on linux ?
    $ grep processor /proc/cpuinfo | wc -l

    sabre150 wrote:
    nitelia wrote:
    String[] command = {"sh","-c","grep processor /proc/cpuinfo | wc -l"};
              Process pro = Runtime.getRuntime().exec(command);
              BufferedReader bf = new BufferedReader(new InputStreamReader(pro
                        .getInputStream()));
              String x = null;
              while ((x = bf.readLine()) != null) {
                   System.out.println(x);
         This should workWow! So all one has to do is to use a shell to run the command! Isn't it a pity I did not give this solution in reply #3?You old sabre rattler! ;-)

  • How to execute dynamic if statement...

    Hi All,
    My PL/SQL Blocks looks like this
    declare
    i_string :='if 3>20 then 1 else 2 end if;' varchar2(100);
    begin
    end;
    Between begin and end i want a piece of code which executes that if statement and sends 2 as output in dbms_output.put_line since 3 is not >20 .Basically i want to know how to execute dynamic if statement.Please help
    Thanks,
    Preethi

    Hi,
    i agree with Cyn. Dynamic SQL is often a bad idea. Try to avoid it.
    Dynamic PL/SQL is even worse. If you really must do something dynamic, at least try to cast it as SQL rather than PL/SQL.
    Does this do what you want?
    CREATE OR REPLACE FUNCTION     eval_case
    (     in_txt     IN     VARCHAR2
    RETURN     NUMBER
    DETERMINISTIC
    IS
         return_val     NUMBER;
         sql_txt          VARCHAR2 (1000);
    BEGIN
         sql_txt := 'SELECT  CASE '
              ||           in_txt
              || '         END'
              || '  FROM  dual';
         EXECUTE IMMEDIATE sql_txt INTO return_val;
         RETURN     return_val;
    END     eval_case;
    SHOW ERRORSYou might use the function above like this:
    declare
         i_string  varchar2(100) :='WHEN 3>20 THEN 1 ELSE 2';
    begin
         dbms_output.put_line (  TO_CHAR (eval_case (i_string))
                        || ' = results of eval_case ('''
                        || i_string
                        || ''')'
    end;
    /

Maybe you are looking for

  • Unable to Create a Subfolder in Company Folder

    Gurus, I am setting up Essentials R2 and I see all the preinstalled folders including the Company folder. When I try to create a subfolder in it, it gives me a message, "The folder location is not valid. This location cannot be added as a server fold

  • Illustrator CS6 trial install damaged

    I have CS5 Design Standard installed. I downloaded Illustrator CS6 trial and installed it. I quit Illustrator CS5 for the install but accidentally started it during the install process and quit it again. Not sure if this is related to my problem. It

  • Infocube activation

    HI Experts, the infocube contains 3 years data and we are added 1 field to the infocube with out deleting the data.when we try to activate the infocube its taking lot of time and before we have added field to the datasource , written customer exit an

  • CS-Order - Extras - settings - Default values

    Hi, in tx IW31 Menu -> Extras -> settings -> Default values it is possible to set a lot of values by default for a specific user. Is this also possible somehow for a whole usergroup or all users? The requirement is that all users from one salesorg sh

  • How do i upgrade my os

    My MacBook Pro has operating software Mac OS X 10.5.8 and when I check for udates it doesn't find any needed. I need to upgrade to at least Mac OS X 10.6.8 to update my itunes to  use my new iPad mini. I can't find a download for the needed OS. Any i