问题描述
我试图为LFTP制作一个maven插件,并涉及从我的java应用程序调用LFTP命令行程序。
I'm attempting to make a maven plugin for LFTP and that involves invoking the LFTP command line program from my java application.
但我无法得到它使用单引号,双引号,espcaped单/双引号来处理我传递的命令。
However I am unable to get it to process my passed in commands using single quotes, double quotes, espcaped single / double quotes.
到目前为止,我所使用的代码是:
What I have for code so far is:
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);
以及寻找接下来尝试的建议。
And what looking for suggestions on what to try next.
编辑#1:我尝试使用 org.apache.commons.exec.CommandLine
和作为预完成字符串但它会导致以下错误:
Edit #1: I have attempted to utilize org.apache.commons.exec.CommandLine
and as a pre-done String but it results in the following error:
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)
但是当我打印出生成的命令并手动运行它时,它工作正常。
But when I print out the generated command and run that manually it works fine.
编辑#2:
推荐答案
Commons-exec强调,更容易处理 CommandLine
实例:
Commons-exec stresses that it's easier to deal with CommandLine
instances by building incrementally:
final CommandLine cmdLine = new CommandLine("lftp");
cmdLine.addArgument("-e");
cmdLine.addArgument("set ftp:ssl-protect-data true; put -O /data/upload/ src/test/resources/test-file.txt; bye");
...
executor.execute(cmdLine);
我没有看过他们的命令行解析器,但是如果他们不完全信任它,我也不会。
I haven't looked at their command line parser, but if they don't fully trust it, I wouldn't, either.
这篇关于命令行中的引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!