i get cannot find symbol error in compile time

2020-05-09 19:27发布

import java.io.File;
 import java.io.BufferedReader;
 import java.io.InputStreamReader;
 import java.io.*;
 import java.io.InputStream;
 class pbdemo {

     static public void main(String[] args) throws Exception {
         String s;
         try {
             ProcessBuilder pb = new ProcessBuilder("cmd", "/f", "dir");
             pb.directory(new File("F:\\WINDOWS"));
             pb.start();
             BufferedReader br = new BufferedReader(new InputStreamReader(pb.getInputStream()));

             while ((s = br.readLine()) != null)
             System.out.println(s);

         } catch (Exception e) {
             System.out.println("sorry" + e);
         }
     }
 }

what is the problem in

BufferedReader br=new BufferedReader(new InputStreamReader(pb.getInputStream()));

I get cannot find symbol error.

标签: java
2条回答
Viruses.
2楼-- · 2020-05-09 19:46

Simple answer.

ProcessBuilder don't have getInputStream method.

See this: ProcessBuilder

You can use:

BufferedReader br=new BufferedReader(new InputStreamReader(pb.start().getInputStream()));
查看更多
看我几分像从前
3楼-- · 2020-05-09 19:50

You are trying to get an InputStream from a ProcessBuilder and that's wrong.

ProcessBuilder.start() returns a Process object, and that's the one who has a getInputStream() method.

Try switching that line for this one.

BufferedReader br=new BufferedReader(new InputStreamReader(pb.start().getInputStream()));

Or

Process pro = pb.start();
BufferedReader br=new BufferedReader(new InputStreamReader(pro.getInputStream()));
查看更多
登录 后发表回答