简体   繁体   English

命令未在 Spring-Boot 应用程序中运行

[英]Command not running in Spring-Boot application

I created a simplest Spring-Boot application to test running command, see the completed code below, after click Run 'Application' in InteliJ, console ends up with Process finished with exit code 0 , no directory structures in console display.我创建了一个最简单的 Spring-Boot 应用程序来测试运行命令,请参见下面的完整代码,在 InteliJ 中单击Run 'Application'后,控制台以Process finished with exit code 0 ,控制台显示没有目录结构。

Run dir c: in terminal returns all the files and directories, seems Process process = Runtime.getRuntime().exec("cmd /c dir C:"); Run dir c: in terminal 返回所有文件和目录,似乎Process process = Runtime.getRuntime().exec("cmd /c dir C:"); does not run at all.根本不运行。

I put process = Runtime.getRuntime().exec("dir C:");我把process = Runtime.getRuntime().exec("dir C:"); , running application again, this time console output ends up with java.io.IOException: CreateProcess error=2 , apparently Java runtime recognized command as invalid. ,再次运行应用程序,这次控制台输出以java.io.IOException: CreateProcess error=2结束,显然 Java 运行时将命令识别为无效。

Why Process process = Runtime.getRuntime().exec("cmd /c dir C:");为什么Process process = Runtime.getRuntime().exec("cmd /c dir C:"); does not return any result?不返回任何结果?

package

import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    public static void main(String[] args) { SpringApplication.run(Application.class); }

    @Bean
    public CommandLineRunner CommandLineRunnerBean() {
        return (args) -> {
            Process process = Runtime.getRuntime().exec("cmd /c dir C:");
//          process = Runtime.getRuntime().exec("dir C:");
        }
    }
}

First, code that you posted doesn't compile.首先,您发布的代码无法编译。

When you write this line of code:当你写这行代码时:

Process process = Runtime.getRuntime().exec("cmd /c dir C:");

Process is executed, but you need to write code that will display result of this process in console.流程已执行,但您需要编写代码以在控制台中显示此流程的结果。

You can get result of process by calling :您可以通过调用获得处理结果:

 process.getInputStream()

getInputStream() method gets the input stream of the subprocess. getInputStream()方法获取子进程的输入流。 The stream obtains data piped from the standard output stream of the process represented by Process object.流从由 Process 对象表示的进程的标准输出流中获取通过管道传输的数据。

Third, to display all directories in C drive, you should use this path:第三,要显示C盘中的所有目录,你应该使用这个路径:

Process process = Runtime.getRuntime().exec(String.format("cmd /c dir %s", "C:\\"));

To summarize, if you want to see all directories in your C: drive, first you will need to create a class which will used to output result of process to java console as I mentioned above.总而言之,如果您想查看 C: 驱动器中的所有目录,首先您需要创建一个类,用于将进程结果输出到 java 控制台,如上所述。 Let's call that class HelperReader .我们称该类HelperReader

Here is the code:这是代码:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.function.Consumer;

public  class HelperReader implements Runnable {
    private InputStream inputStream;
    private Consumer<String> consumer;

    public HelperReader(InputStream inputStream, Consumer<String> consumer) {
        this.inputStream = inputStream;
        this.consumer = consumer;
    }

    @Override
    public void run() {
        new BufferedReader(new InputStreamReader(inputStream)).lines()
                .forEach(consumer);
    }
}

Change code in your Application class to something like this:Application类中的代码更改为如下所示:

   import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import java.util.concurrent.Executors;

@SpringBootApplication
public class Application implements CommandLineRunner  {



    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);

     
    }

    @Override
    public void run(String... args) throws Exception {
        Process process = Runtime.getRuntime().exec(String.format("cmd /c dir %s", "C:\\"));
        HelperReader streamGobbler =
                new HelperReader(process.getInputStream(), System.out::println);
        Executors.newSingleThreadExecutor().submit(streamGobbler);

    }
}

Run the application and you should see something like following picture in console.运行应用程序,您应该会在控制台中看到类似下图的内容。 在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM