【发布时间】:2013-01-09 16:19:03
【问题描述】:
我正在尝试为 LFTP 制作一个 maven 插件,这涉及从我的 java 应用程序调用 LFTP 命令行程序。
但是我无法让它使用单引号、双引号、转义单/双引号处理我传入的命令。
到目前为止,我所拥有的代码是:
final String command = "lftp -e 'set ftp:ssl-protect-data true; put -O /data/upload/ src/test/resources/test-file.txt; bye' -u username,password -p 21 192.168.1.100"
final CommandLine cmdLine = CommandLine.parse(command.toString());
final DefaultExecutor executor = new DefaultExecutor();
executor.setWorkingDirectory(new File(baseDir));
final int result = executor.execute(cmdLine);
以及寻找关于下一步尝试的建议。
编辑 #1: 我尝试使用 org.apache.commons.exec.CommandLine 并作为预先完成的字符串,但会导致以下错误:
Unknown command `set ftp:ssl-protect-data true; put -O /data/upload/ src/test/resources/test-file.txt; bye'.
Exception in thread "main" org.apache.commons.exec.ExecuteException: Process exited with an error: 1 (Exit value: 1)
at org.apache.commons.exec.DefaultExecutor.executeInternal(DefaultExecutor.java:377)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:160)
at org.apache.commons.exec.DefaultExecutor.execute(DefaultExecutor.java:147)
at org.welsh.build.automation.lftp.plugin.TestClass.main(TestClass.java:30)
但是当我打印出生成的命令并手动运行它时,它工作正常。
编辑#2:增加了一些清晰度。
【问题讨论】:
-
难道没有某种执行器将字符串数组作为参数而不是单个字符串?
-
@baba 我不认为这是相关的,因为这里是执行外部命令的问题
-
@fge 是否外部,不推荐使用 sun API。这就是为什么像 Commons Cli 这样更好的库与尝试使用已弃用的 API 手动解析引号和单引号相比可以创造奇迹的原因。
-
@baba 我认为他正试图从他的程序中执行一个外部命令,而不是编写一个接受命令行选项的程序。
标签: java command-line lftp