3

始めたばかりのJavaの初心者、助けていただければ幸いです。だから私のコードはこれであり、何らかの理由で出力を機能させることができません..私はこれに何時間も座っていました..

package askisi1;

import java.net.*;
import java.util.*;
import java.lang.*;
import java.io.*;


public class Main{

public static void main(String[] args){

    try{

        String command = "ifconfig eth1 | grep -oP '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'";
        Process child = Runtime.getRuntime().exec(command);

        System.out.println("So far so good");
        BufferedReader r = new BufferedReader(new InputStreamReader(child.getInputStream()));
        String s;
        while ((s = r.readLine()) != null) {
        System.out.println(s);
        }
        r.close();
        System.out.println("Continue..");
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}



} 
4

1 に答える 1

2

Runtime.exec()Unixコマンドを実行するには、いくつかの追加情報が必要です。

したがって、私のイーサネットカードがlo0次のようになっていると仮定します。

String[] command = {
                    "/bin/sh",
                    "-c",
                    "ifconfig lo0 | grep -oP '[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}'"
            }; 
Process child = Runtime.getRuntime().exec(command);
// following here your remaining unchanged code

これは印刷します:

So far so good
127.0.0.1
Continue..
于 2012-10-21T18:00:51.680 に答える