Witam, piszę nakładkę na windowsowy wiersz poleceń. Mam kod który uruchamia CMD jako subprocess - konieczne aby zachowana była historia wywołań, przejść po katalogach. Następnie przesyłam polecenia i odbieram odpowiedź. Problem pojawia się w momencie parsowania odpowiedzi, nie mam pojęcia w jaki sposób wyznaczyć "koniec" odpowiedzi. Teraz pobieram znaki zanim nie napotkam pustej linii, ale pustych linii w wywołaniu "dir" jest kilka w jednej odpowiedzi... cmdProcess.getInputStream().available() zawsze zwraca zero, cmdOut.ready() też nie pomaga. Proszę o pomoc, pozdrawiam
package app;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.regex.Pattern;
class CommandLine {
private PrintWriter cmdIn = null;
private BufferedReader cmdOut = null;
private Process cmdProcess = null;
private static CommandLine instance = null;
public static CommandLine get() {
if (instance == null) {
instance = new CommandLine();
}
return instance;
}
private CommandLine() {
String[] command = { "cmd" };
try {
cmdProcess = Runtime.getRuntime().exec(command);
} catch (IOException e) {
e.printStackTrace();
}
cmdIn = new PrintWriter(cmdProcess.getOutputStream());
cmdOut = new BufferedReader(new InputStreamReader(cmdProcess.getInputStream()));
}
public String readResponse() {
StringBuilder sb = new StringBuilder();
String line;
try {
while ((line = cmdOut.readLine()) != null && !line.isEmpty()) {
sb.append(line);
sb.append("\n");
}
} catch (IOException e) {
e.printStackTrace();
}
return sb.toString();
}
public void inputCommand(String cmd) {
cmdIn.println(cmd);
cmdIn.flush();
}
}
public class main {
public static void f() {
CommandLine cmd = CommandLine.get();
cmd.inputCommand("dir c:\\");
System.out.println(cmd.readResponse());
System.out.println(cmd.readResponse());
System.out.println(cmd.readResponse());
System.out.println(cmd.readResponse());
cmd.inputCommand("dir c:\\");
System.out.println(cmd.readResponse());
}
public static void main(String[] args) {
f();
}
}
EDIT
class CommandLine {
private PrintWriter cmdIn = null;
private BufferedReader cmdOut = null;
private Process cmdProcess = null;
private static CommandLine instance = null;
StringBuilder commandLineResponse = new StringBuilder();
...
public String readResponse() {
Callable<String> readTask = new Callable<String>() {
@Override
public String call() throws Exception {
String line;
try {
while ((line = cmdOut.readLine()) != null) {// != null &&
// !line.isEmpty())
// {
commandLineResponse.append(line);
commandLineResponse.append("\n");
}
} catch (IOException e) {
return commandLineResponse.toString();
}
return commandLineResponse.toString();
}
};
ExecutorService executor = Executors.newFixedThreadPool(2);
Future<String> future = executor.submit(readTask);
try {
future.get(1000, TimeUnit.MILLISECONDS);
} catch (Exception e) {
}
return commandLineResponse.toString();
}
Rozwiązanie tragiczne, ale tylko to działa w miarę możliwości. pomocy!