我有一个Spring boot web应用程序,我想在命令行中调用一些命令。当我在运行进程后使用ProcessBuilder和process类时,ExecutorService被关闭。
方法,我在其中运行进程:
public void runTestsInProject(String projectPath){
System.out.println("Starting runTestsInProject() ------");
try{
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"" + projectPath + "\" && mvn clean test");
builder.redirectErrorStream(true);
Process p = builder.start();
BufferedReader r = new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while (true) {
line = r.readLine();
if (line == null) { break; }
}
} catch (IOException e){e.printStackTrace();}
}错误日志:
2020-07-27 20:33:20.246 INFO 7248 --- [ Thread-4] o.s.s.concurrent.ThreadPoolTaskExecutor : Shutting down ExecutorService 'applicationTaskExecutor'
2020-07-27 20:33:20.250 INFO 7248 --- [ Thread-4] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2020-07-27 20:33:20.254 INFO 7248 --- [ Thread-4] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown initiated...
2020-07-27 20:33:20.270 INFO 7248 --- [ Thread-4] com.zaxxer.hikari.HikariDataSource : HikariPool-1 - Shutdown completed.春天又开始了..。
2020-07-27 20:33:29.778 INFO 7248 --- [nio-8080-exec-9] o.a.c.loader.WebappClassLoaderBase : Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.
java.lang.IllegalStateException: Illegal access: this web application instance has been stopped already. Could not load [META-INF/services/javax.xml.bind.JAXBContext]. The following stack trace is thrown for debugging purposes as well as to attempt to terminate the thread which caused the illegal access.发布于 2020-07-29 17:50:03
1)第一个问题在命令中
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "c: && cd \"" + projectPath + "\" && mvn clean test");而不是
ProcessBuilder builder = new ProcessBuilder(
"cmd.exe", "/c", "cd \"" + projectPath + "\" && mvn clean test");Windows cmd可能并不总是在同一个起始点,所以当cmd启动并指向D: disc上,并且您的命令si例如cd C:\smt\doc是指向时,它就不起作用了。因此,需要在命令开始时判断要在哪个磁盘上操作。
2)第二个问题可能是集成开发环境用来构建和运行应用程序的maven正在被我由ProcessBuilder执行的maven命令关闭。因此,解决方案是构建应用程序jar文件并从cmd运行它。
https://stackoverflow.com/questions/63121537
复制相似问题