问题描述
我想通过 SystemCommandTasklet 运行系统命令.使用下面的示例代码尝试了此操作,但出现错误.
我认为这是因为命令参数,但我无法修复它.
如果有帮助,我会很高兴.
参考例子;
错误详情;
CreateProcess error=2,系统找不到指定的文件"
代码示例;
@Bean@StepScopepublic SystemCommandTasklet fileSplitterSystemCommandTasklet(@Value("#{jobParameters['file']}") File file) 抛出异常 {final String fileSeparator = System.getProperty(file.separator");String outputDirectory = file.getPath().substring(0, file.getPath().lastIndexOf(fileSeparator)) + fileSeparator + out"+ 文件分隔符;文件输出 = 新文件(输出目录);如果(!输出.存在()){输出.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(新字符串[]{"JAVA_HOME=/java",BATCH_HOME=/Users/batch"});返回 fileSplitterTasklet;}
您需要使用 file.getAbsolutePath()
而不是 file.getPath()
.>
此外,您在命令中使用了 file.getName()
:
final String command = String.format("split -a 5 -l 10000 %s %s",file.getName(),outputDirectory);
您应该传递文件的绝对路径或确保设置正确设置工作目录,以便执行 split
命令在与文件相同的目录中.
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 ;
- Using SystemCommandTasklet for split the large flat file into small files
- Trying to split files using SystemCommandTasklet - Execution of system command did not finish within the timeout
Error Detail ;
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;
}
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 setthe working directory correctly so that the split
command is executedin the same directory as the file.
这篇关于使用 SystemCommandTasklet 拆分文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!