[Linux-Biella] Java runtime

Federico 'Phate' Pistono fppain a gmail.com
Ven 11 Maggio 2007 12:30:05 CEST


Ciao,
come cavolo faccio a far eseguire a java un comando unix con i vari pipe,
grep e wildcards?
Ho semplificato l'esempio per farmi capire. Command viene eseguito
correttamente, se invece provo con command0 non riconosce il pipe e mi dice:

/bin/ls: |: No such file or directory
/bin/ls: grep: No such file or directory
/bin/ls: m: No such file or directory

Codice:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Ex {
    public static void main(String args[]){
        String line = "";
        BufferedReader b;
        String command = "ls -la | grep m";
        try {
            Process myCommand = Runtime.getRuntime().exec(command);
            //Process myCommand = new ProcessBuilder(myArgs).start();
            // stdout
            b = new BufferedReader(new InputStreamReader(
myCommand.getInputStream()));
            while ((line = b.readLine()) != null)
                System.out.println(line);
            // stderr
            b = new BufferedReader(new InputStreamReader(
myCommand.getErrorStream()));
            while ((line = b.readLine()) != null)
                System.out.println(line);
        } catch (IOException e) {
            System.out.println(e.getMessage());
            e.printStackTrace();
        }
    }
}

Grazie

-- 
Federico Pistono   Department of Computer Science - Verona, Italy
Linux Registered User #340392

http://pain.altervista.org
http://www.federicopistono.org
http://studentiunivr.wordpress.com
http://todayitypedongoogle.wordpress.com

> "I can't afford to be sick, so I'm just gonna plow through it. I'm gonna
get wasted tonight. You know, alcohol kills bacteria.'"
> "Mmm-hmm. So does bleach, why don't you drink that?"
-------------- parte successiva --------------
Un allegato HTML è stato rimosso...
URL: http://ml.bilug.linux.it/mailman/private/linux/attachments/20070511/066beab9/attachment.html 


Maggiori informazioni sulla lista Linux