在我的java应用程序中,我需要运行一个带有文件standalone.bat
的Jboss服务器。
我尝试了ProcessBuilder
,虽然它确实启动了服务器,但我的应用程序被阻止等待服务器关闭
@RequestMapping(value = "api/project/liststart", method = RequestMethod.POST)
public HttpEntity<Boolean> postListServer(@RequestBody ListStart modules) throws Throwable {
String cmd = "";
Boolean response = false;
ProcessBuilder processBuilder = new ProcessBuilder();
String path = "C:\jboss-as-7.1.1.Final\bin\";
String command = standelone.bat+ " >sometext.txt" ;
processBuilder.command("cmd.exe", "/c", command);
processBuilder.directory(new File(path));
Process process = processBuilder.start();
BufferedReader reader =
new BufferedReader(new InputStreamReader(process.getInputStream()));
String ligne;
while ((ligne = reader.readLine()) != null) {
System.out.println(ligne);
}
int exitCode = process.waitFor();
System.out.println("nExited with error code : " + exitCode);
String F = path + "\SomeFile.txt";
System.out.println("------------------------file: " + F);
File file = new File(F);
Scanner scanner = new Scanner(file);
//now read the file line by line...
int lineNum = 0;
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
if (line.contains("Started server")) {
response = true;
System.out.println("-------------------------------------" + response.toString());
}
}
ResponseEntity responseEntity = new ResponseEntity<Boolean>(response, HttpStatus.OK);
return responseEntity;
}
}
我希望上面的方法返回true
值,但在返回值之前它被阻止了。
似乎 scanner.hasNextLine(( 无法从循环中退出,所以尝试如下:
while (scanner.hasNextLine()) {
String line = scanner.nextLine();
lineNum++;
if (line.contains("Started server")) {
response = true;
System.out.println("-------------------------------------" + response.toString());
return new ResponseEntity<Boolean>(response, HttpStatus.OK);
} else {
return new ResponseEntity<Boolean>(response, HttpStatus.OK);
}
}