gpt4 book ai didi

Java - 带有空格和双引号的 ProcessBuilder 命令参数失败

转载 作者:可可西里 更新时间:2023-11-01 09:24:00 25 4
gpt4 key购买 nike

我正在使用 ProcessBuilder 来运行 Windows 可执行文件...我需要运行的确切命令是:

"C:\Program Files\CCBU\CCBU.exe" -d"C:\My Data\projects\ccbu\ciccb-report.xls" -tf"C:\Program Files\CCBU\loss-billing-filters.txt"

如果我从命令提示符运行上面的命令,它工作正常。

如果我随后按照以下 StackOverflow 帖子 (ProcessBuilder adds extra quotes to command line) 中的指示将命令和参数作为字符串 [] 数组发出,它会失败,因为目录路径中的空格会以某种方式破坏 CCBU.exe 可执行文件的参数:

[log-snippet]
2015-08-31 10:39:08,937 [main] INFO rpd.primary - C:\Program Files\CCBU\CCBU.exe
logging to the given report's directory
Configuration file is: ./CCBUConfigFile.txt
Running with the following settings:
Report Filepath: C:\My
Search Terms FilePath: C:\Program

2015-08-31 10:39:08,948 [main] INFO rpd.primary - STDERR:--------------------
2015-08-31 10:39:08,961 [main] INFO rpd.primary -
Warning: parameter Data\projects\ccbu\ciccb-report.xls not recognized. Ignoring

Warning: parameter Files\CCBU\loss-billing-filters.txt not recognized. Ignoring

Error: C:\Program not found or not readable
[/log-snippet]

如果我将数据文件和过滤器移动到没有空格的目录路径中,则效果很好:

"C:\Program Files\CCBU\CCBU.exe" -d"C:\Users\n0002501\ccbu\ciccb-report.xls" -tf"C:\Users\n0002501\ccbu\loss-billing-filters.txt" 

问题是,此过程的用户会将文件放在确实有空格的文件夹(目录)中。所以我必须以某种方式让它与空间一起工作。我认为这很简单,但我错过了什么?

我正在使用这篇文章中的类来处理 STDOUT 和 STDERR 的线程:http://alvinalexander.com/java/java-exec-processbuilder-process-2

代码如下:

            // Split the Arguments : 
// In Eclipse and runtime, the arguments get broken :
// The STDOUT from the command shows the Report Filepath
// and Search Teams FilePath as broken at the 1st space...
//
// Report Filepath: C:\My
// Search Terms FilePath: C:\Program
//
// SHOULD BE :
//
// Report Filepath: C:\My Data\projects\ccbu\ciccb-report.xls
// Search Terms FilePath: C:\Program Files\CCBU\loss-billing-filters.txt
//
try {
commands.add ( "\"C:\\Program Files\\CCBU\\CCBU.exe\"" );
commands.add ( "-d\"C:\\My Data\\projects\\ccbu\\ciccb-report.xls\"" );
commands.add ( "-tf\"C:\\Program Files\\CCBU\\loss-billing-filters.txt\"" );
commandExecutor = new SystemCommandExecutor(commands);
commandExecutor.setLog ( getLog() );

// DEBUG : Build and printout the commands...
//
lstrCommand = "";
for ( int theIdx=0; theIdx<commands.size (); theIdx++ ) {
if ( theIdx == 0 ) {
lstrCommand = lstrCommand + commands.get ( theIdx );
}
else {
lstrCommand = lstrCommand + " " + commands.get ( theIdx );
}
getLog().debug ( SHORT_NAME + " Building Command[] [" + commands.get ( theIdx ) + "]" );
}

getLog().debug ( SHORT_NAME + " Running Command[] [" + lstrCommand + "]" );

result = commandExecutor.executeCommand();

// get the stdout and stderr from the command that was run
stdout = commandExecutor.getStandardOutputFromCommand();
stderr = commandExecutor.getStandardErrorFromCommand();

// print the stdout and stderr
getLog().info ( "SystemCommandExecutor - Status Code [" + result + "]" );
getLog().info ( "STDOUT:--------------------" );
getLog().info( stdout );
getLog().info ( "STDERR:--------------------" );
getLog().info( stderr );
}
catch ( Exception ltheXcp ) {
getLog().error ( SHORT_NAME + ".runTask () - Error/exception on commands [3-spaces] [" + lstrCommand + "]" );
}
finally {
commands.clear ();
stdout = null;
stderr = null;
commandExecutor = null;
}

Jayan,最终有效的代码:

            try { 
commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe" );
commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt" );

commandExecutor = new SystemCommandExecutor ( commands );
commandExecutor.setLog ( getLog() );

我所要做的就是去掉所有双引号,让 ProcessBuilder 自己处理目录路径...

阿达姆

最佳答案

添加不带“双”引号的单个字符串..

                commands.add ( "C:\\Program Files\\CCBU\\CCBU.exe"                      );
commands.add ( "-d");
commands.add ("C:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
commands.add ( "-tf");
commands.add("C:\\Program Files\\CCBU\\loss-billing-filters.txt" );
commandExecutor = new SystemCommandExecutor(commands);

ProcessBuilder 将负责必要的参数处理。


拉起评论:

Jayan, You're idea got me thinking : The following worked :

 commands.add ( "-dC:\\My Data\\projects\\ccbu\\ciccb-report.xls" );
commands.add ( "-tfC:\\Program Files\\CCBU\\loss-billing-filters.txt"

); – lincolnadym

关于Java - 带有空格和双引号的 ProcessBuilder 命令参数失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32314645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com