我想在main方法中调用dir1方法的输出。实际上我想在JSP中输出dir1方法。它帮助我在jsp页面中打印输出。但首先我无法在主要方法中这样做。
由于程序正确编译,因此在运行期间面对main方法中的Null指针异常。
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class test_1 {
String[] b;
public String dir1 (String ps,String file1)
{
String result="";
int i=0;
try{
File file=new File(file1);
ProcessBuilder pb = new ProcessBuilder("cmd","/C",ps);
pb.directory(file);
Process p = pb.start();
BufferedReader reader = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String readline;
while ((readline = reader.readLine()) != null)
{
this.b[i]=readline;
i++;
}
int exitValue = p.waitFor();
}
catch(InterruptedException e)
{
e.printStackTrace();
}
catch(Exception e)
{
System.out.println(e);
}
/*try {
int exitValue = p.waitFor();
}
catch (InterruptedException e) {
e.printStackTrace();
}*/
/*finally{
out.close();
} */
return result;
}
public static void main(String[] args)
{
test_1 f1=new test_1();
f1.dir1("dir", "C:\\Program Files\\test\\test.txt");
for(int i=0;i<100;i++)
{
System.out.println(f1.b[i]);
}
System.out.println(f1.dir1("dir", "C:\\Program Files\\test\\test.txt"));
}
}