0

I want to run System Commands via SystemCommandTasklet.Itried this with the sample code below but I get an error.

I think this because of command parameter,But I could not fix it.

I would be very glad if it will help.

Reference Examples ;

Error Detail ;

"CreateProcess error=2, The system cannot find the file specified"

Code Sample ;

 @Bean
@StepScope
public SystemCommandTasklet fileSplitterSystemCommandTasklet(@Value("#{jobParameters['file']}") File file) throws Exception {

    final String fileSeparator = System.getProperty("file.separator");
    String outputDirectory = file.getPath().substring(0, file.getPath().lastIndexOf(fileSeparator)) + fileSeparator + "out" + fileSeparator;

    File output = new File(outputDirectory);

    if (!output.exists()) {
        output.mkdir();
    }

    final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);

    var fileSplitterTasklet = new SystemCommandTasklet();
    fileSplitterTasklet.setCommand(command);
    fileSplitterTasklet.setTimeout(60000L);
    fileSplitterTasklet.setWorkingDirectory(outputDirectory);
    fileSplitterTasklet.setTaskExecutor(new SimpleAsyncTaskExecutor());
    fileSplitterTasklet.setSystemProcessExitCodeMapper(touchCodeMapper());
    fileSplitterTasklet.afterPropertiesSet();
    fileSplitterTasklet.setInterruptOnCancel(true);
    fileSplitterTasklet.setEnvironmentParams(new String[]{
            "JAVA_HOME=/java",
            "BATCH_HOME=/Users/batch"});
    return fileSplitterTasklet;
}
musti
  • 21
  • 6

1 Answers1

0

You need to use file.getAbsolutePath() instead of file.getPath().

Also, you are using file.getName() in the command:

final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);

You should pass the absolute path of the file or make sure to set the working directory correctly so that the split command is executed in the same directory as the file.

Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • First off all thank you so much for your help #Ma @Mahmoud Ben Hassine.I tried but it didn't work.file.getAbsolutePath() and file.getPath() return same value. – musti Jul 06 '20 at 11:06
  • Your issue `The system cannot find the file specified` means the file cannot be found. Make sure file path is correct regarding the working directory: `fileSplitterTasklet.setWorkingDirectory(outputDirectory);` – Mahmoud Ben Hassine Jul 06 '20 at 11:10
  • Directory and file is correct and exists.If I try with not exists file and directory,the error message is "directory path must exists". – musti Jul 06 '20 at 11:16
  • You are using `file.getName()` in the command, you need to make sure the final `split` command is correct (the command is executed in the correct directory, the file path is correct, etc). Try to debug to see the final command and see if it's correct. – Mahmoud Ben Hassine Jul 06 '20 at 11:22
  • The split command is correct.I have checked command by a command executer,and it is executed successfully.Do you have any code example which executed successfully? – musti Jul 06 '20 at 12:36