英文:
Java Runtime cmd execution in a Spring Boot project
问题
以下是您要翻译的内容:
"A project consists of audio blocks."
我正在实现REST API,当块发生更改时更新项目。
我想要合并块,并在音频块之间添加填充,然后将附加的音频上传回S3存储。音频文件存储在S3存储中。
我想要在运行EC2上的Spring Boot项目时执行sox命令。
sox模块已安装在EC2实例中。这可能是目录路径的问题吗?
当我调用该函数时,它返回退出值1,并且音频块未与填充合并。
我尝试在一个简单的Java文件中执行相同的运行时命令,它可以工作,但在部署的Spring Boot项目中不起作用。
我尝试使用Java的Runtime.getRuntime().exec(cmd)执行sox命令。
我期望在运行Spring Boot项目时成功执行sox命令。
英文:
A project consists of audio blocks.
I'm implementing REST API that updates project when there is change in blocks.
I would like to merge blocks with paddings between the audio blocks, and then upload appended audio back to S3 storages. Audio files are stored in S3 storages.
I want to execute sox command while running a spring boot project in an EC2.
sox module is installed in an EC2 instance. Would this be a problem of directory path?
When I call the function it returns exit value 1, and audio blockes are not merged with paddings.
I tried executing same runtime command in a simple java file and it worked, but it is not working in a deployed spring boot project.
public void mergeBlocks(Long teamId, Long projectId){
try {
List<Block> blocks = blockRepository.findByProjectId(projectId);
long length = 0;
AudioInputStream clip = null;
List<AudioInputStream> list = new ArrayList<AudioInputStream>();
new File(String.valueOf(projectId)).mkdirs();
for (Block b: blocks) {
FileOutputStream fos = new FileOutputStream(new File(projectId + "/temp" + b.getId() + ".wav"));
S3Object o = amazonS3Client.getObject(AUDIO_BUCKET_NAME, teamId + "/" + projectId + "/" + b.getId() + ".wav");
S3ObjectInputStream s3is = o.getObjectContent();
byte[] read_buf = new byte[1024];
int read_len = 0;
while ((read_len = s3is.read(read_buf)) > 0) {
fos.write(read_buf, 0, read_len);
}
fos.close();
String[] cmd = {"sox", projectId + "\\temp" + b.getId() + ".wav", projectId + "\\interval" + b.getId() + ".wav", "pad", "0", String.valueOf(b.getInterval())};
try {
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec(cmd);
BufferedReader input = new BufferedReader(new InputStreamReader(pr.getInputStream()));
String line = null;
while ((line = input.readLine()) != null) {
System.out.println(line);
}
int exitVal = pr.waitFor();
System.out.println("Exited with error code " + exitVal);
} catch(Exception e) {
System.out.println(e.toString());
e.printStackTrace();
}
clip = AudioSystem.getAudioInputStream(new File(projectId + "/interval" + b.getId() + ".wav"));
list.add(clip);
length += clip.getFrameLength();
}
if(length>0 && list.size()>0 && clip!=null) {
AudioInputStream appendedFiles =
new AudioInputStream(
new SequenceInputStream(Collections.enumeration(list)),
clip.getFormat(),
length);
AudioSystem.write(appendedFiles,
AudioFileFormat.Type.WAVE,
new File(projectId + "/appended.wav"));
}
clip.close();
amazonS3Client.putObject(AUDIO_BUCKET_NAME, teamId + "/" + projectId + "/0.wav", new File(projectId + "/appended.wav"));
FileUtils.deleteDirectory(new File(String.valueOf(projectId)));
} catch (AmazonServiceException e) {
System.err.println(e.getErrorMessage());
System.exit(1);
} catch (FileNotFoundException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (IOException e) {
System.err.println(e.getMessage());
System.exit(1);
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
}
}
I tried executingn sox command with java Runtime.getRuntime().exec(cmd);
I expected sox command getting executed successfully while running the spring boot project.
答案1
得分: 1
问题可能出在你使用反斜杠指定目录路径。尽管你在代码中正确转义了它们。你的命令字符串仍然会是:"yourProjectId\temp\...
,在执行时可能仍会导致错误。尝试改用正斜杠 /
,或者使用双重转义的反斜杠,如 \\\\
。
英文:
The problem might lie therein, that you are using backslashes to specify the directory paths. While you are correctly escaping them in your code. The string for your command will still look like: "yourProjectId\temp\...
, which might still cause errors when executed. Try using forward slashes /
instead or maybe using double escaped backslashes like \\\\
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论