首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何从Spring boot webapp运行commads

如何从Spring boot webapp运行commads
EN

Stack Overflow用户
提问于 2020-07-28 02:38:25
回答 1查看 77关注 0票数 1

我有一个Spring boot web应用程序,我想在命令行中调用一些命令。当我在运行进程后使用ProcessBuilder和process类时,ExecutorService被关闭。

方法,我在其中运行进程:

代码语言:javascript
复制
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();}
}

错误日志:

代码语言:javascript
复制
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.

春天又开始了..。

代码语言:javascript
复制
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.
EN

回答 1

Stack Overflow用户

发布于 2020-07-29 17:50:03

1)第一个问题在命令中

代码语言:javascript
复制
ProcessBuilder builder = new ProcessBuilder(
                "cmd.exe", "/c", "c: && cd \"" + projectPath + "\" && mvn clean test");

而不是

代码语言:javascript
复制
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运行它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63121537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档