Home > Net >  Command not running in Spring-Boot application
Command not running in Spring-Boot application

Time:06-07

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.

Run dir c: in terminal returns all the files and directories, seems Process process = Runtime.getRuntime().exec("cmd /c dir C:"); does not run at all.

I put 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.

Why 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:");
        }
    }
}

CodePudding user response:

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. The stream obtains data piped from the standard output stream of the process represented by Process object.

Third, to display all directories in C drive, you should use this path:

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. Let's call that class 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:

   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. enter image description here

  • Related