Results 1 to 16 of 16
  1. #1
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default Commands on java.

    Hi, I have an error when I execute some commands on java, overall those who have "pipes" or ">". When I execute "ps aux" it works well, giving to me the processes executing, but when I want to know the number of processes and I use the command "ps aux | wc -l" it gives me the following error.


    ERROR: Garbage option.
    ********* simple selection ********* ********* selection by list *********
    -A all processes -C by command name
    -N negate selection -G by real group ID (supports names)
    -a all w/ tty except session leaders -U by real user ID (supports names)
    -d all except session leaders -g by session OR by effective group name
    -e all processes -p by process ID
    T all processes on this terminal -s processes in the sessions given
    a all w/ tty, including other users -t by tty
    g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
    r only running processes U processes for specified users
    x processes w/o controlling ttys t by tty
    *********** output format ********** *********** long options ***********
    -o,o user-defined -f full --Group --User --pid --cols --ppid
    -j,j job control s signal --group --user --sid --rows --info
    -O,O preloaded -o v virtual memory --cumulative --format --deselect
    -l,l long u user-oriented --sort --tty --forest --version
    -F extra full X registers --heading --no-heading --context
    ********* misc options *********
    -V,V show version L list format codes f ASCII art forest
    -m,m,-L,-T,H threads S children in sum -y change -l format
    -M,Z security data c true command name -c scheduling class
    -w,w wide output n numeric WCHAN,UID -H process hierarchy



    I've also tried to redirect those to an archive ("ps aux > /tmp/procs.txt") but I get the same error.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I use a variable to introduce the number of processes.

    Var = ej.Execute("ps aux | wc -l")

    Now Var is a number (The number of processes).

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    This has nothing to do with Java, but rather is a javascript issue. The two languages are as similar as ham is to hamster. I recommend you google for the correct forum for your question.

  5. #5
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    But I'm using java.

    The same sentece, changing the command works.

    If I do:

    ej.Execute("ps aux");

    it works, it gives me the procesess, why "| wc -l" doesn't give me the number of processes?

    Maybe I should post all my code.

  6. #6
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Omg... Do you think I don't know what am I programming? I am programming on Java. JAVA, not javascript. I will post code now...


    EjecutarComando.java
    Java Code:
    package computerprou;
    
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    
    /**
     *
     * @author usuario
     */
    public class EjecutarComando {
        String Ejecuta(String comando){
            String salida = "";
    
            String s = null;
    
            try {
    
                Process p = Runtime.getRuntime().exec(comando);
    
                BufferedReader stdInput = new BufferedReader(new InputStreamReader(
                        p.getInputStream()));
    
                BufferedReader stdError = new BufferedReader(new InputStreamReader(
                        p.getErrorStream()));
    
                // Salida
    
                while ((s = stdInput.readLine()) != null) {
                    System.out.println(s);
                    salida += s;
                }
    
                // Errores
    
                while ((s = stdError.readLine()) != null) {
                    System.out.println(s);
                }
            } catch (IOException e) {
                System.out.println("Excepción: ");
                e.printStackTrace();
                System.exit(-1);
            }
            return salida;
        }
    
    }

    FraCPU.java
    Java Code:
     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            this.jPanel1.setOpaque(true);
            this.jPanel1.repaint();
            EjecutarComando ej= new EjecutarComando();
            ej.Ejecuta("ps aux | wc -l");
    }
    Last edited by Slithice; 03-05-2011 at 02:40 PM.

  7. #7
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Replies like that are a great way to make people not help you. Was the person helping you supposed to read your mind? The proper code should generally be included in the question. This allows those trying to help you to get more details they may need in finding the problem.

  8. #8
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    The problem is that he deleted his reply. That's why I replayed like that. He answered to me like if I wouldn't know the difference between java and javascript, like if I were stupid. Then I said my last post, but he deleted his thread. So don't talk if you don't know what is happening.

  9. #9
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    I am in error -- javascript is not involved here, but having said that, I still don't believe that the problem is a Java problem but rather that your runtime command -- the parameter being passed in -- is causing error. This error message is not a Java error message.

  10. #10
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    But then I don't understand. If I use "ps aux | wc -l" in the terminal it works. Is there any restriction using commands on java?

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    The pipe symbol '|' is a shell command, the 'ps' command itself doesn't understand it.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    Quote Originally Posted by JosAH View Post
    The pipe symbol '|' is a shell command, the 'ps' command itself doesn't understand it.

    kind regards,

    Jos
    Then, how should I redirect it on java?, I mean how can I make same work as if I use a pipe?

  13. #13
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  14. #14
    Slithice is offline Member
    Join Date
    Mar 2011
    Posts
    8
    Rep Power
    0

    Default

    I'm coding with NetBeans on ubuntu. It would be any way to give you the proyect? maybe if you execute the program you will see something I can't.

  15. #15
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,526
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Slithice View Post
    I'm coding with NetBeans on ubuntu. It would be any way to give you the proyect? maybe if you execute the program you will see something I can't.
    Netbeans has nothing to do with it; start your shell (bash?) with your piped command as an argument to your shell interpreter because your shell knows how to handle pipes and I/O redirects.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  16. #16
    yunnysunny is offline Member
    Join Date
    Nov 2012
    Posts
    1
    Rep Power
    0

    Default Re: Commands on java.

    Quote Originally Posted by Slithice View Post
    Hi, I have an error when I execute some commands on java, overall those who have "pipes" or ">". When I execute "ps aux" it works well, giving to me the processes executing, but when I want to know the number of processes and I use the command "ps aux | wc -l" it gives me the following error.


    ERROR: Garbage option.
    ********* simple selection ********* ********* selection by list *********
    -A all processes -C by command name
    -N negate selection -G by real group ID (supports names)
    -a all w/ tty except session leaders -U by real user ID (supports names)
    -d all except session leaders -g by session OR by effective group name
    -e all processes -p by process ID
    T all processes on this terminal -s processes in the sessions given
    a all w/ tty, including other users -t by tty
    g OBSOLETE -- DO NOT USE -u by effective user ID (supports names)
    r only running processes U processes for specified users
    x processes w/o controlling ttys t by tty
    *********** output format ********** *********** long options ***********
    -o,o user-defined -f full --Group --User --pid --cols --ppid
    -j,j job control s signal --group --user --sid --rows --info
    -O,O preloaded -o v virtual memory --cumulative --format --deselect
    -l,l long u user-oriented --sort --tty --forest --version
    -F extra full X registers --heading --no-heading --context
    ********* misc options *********
    -V,V show version L list format codes f ASCII art forest
    -m,m,-L,-T,H threads S children in sum -y change -l format
    -M,Z security data c true command name -c scheduling class
    -w,w wide output n numeric WCHAN,UID -H process hierarchy



    I've also tried to redirect those to an archive ("ps aux > /tmp/procs.txt") but I get the same error.
    Process aProcess = Runtime.getRuntime().exec(new String[]{"sh","-c","ps aux | wc -l"});

Similar Threads

  1. At commands in java
    By strictlydivine.elite in forum Advanced Java
    Replies: 1
    Last Post: 11-22-2010, 09:55 AM
  2. Run Unix commands from Java
    By yahyaaa in forum New To Java
    Replies: 13
    Last Post: 06-17-2010, 11:50 PM
  3. Help file for Java commands?
    By tyang in forum New To Java
    Replies: 2
    Last Post: 01-31-2010, 05:09 AM
  4. Low-level java commands
    By artemff in forum New To Java
    Replies: 8
    Last Post: 01-01-2010, 06:23 AM
  5. Java Robot commands not working
    By CoolLove in forum Java Applets
    Replies: 2
    Last Post: 01-13-2009, 02:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •