在Java中如何从命令提示符访问带空格的文件路径
我正在尝试从Java程序运行批处理文件。 例如:我在“程序文件”的文件夹中有一批“abc.bat” 我想从我的Java程序中执行这个批处理。我正在使用命令行类Commons exec jar在Java中如何从命令提示符访问带空格的文件路径,java,command-line,apache-commons-exec,Java,Command Line,Apache Commons Exec,我正在尝试从Java程序运行批处理文件。 例如:我在“程序文件”的文件夹中有一批“abc.bat” 我想从我的Java程序中执行这个批处理。我正在使用命令行类Commons exec jar CommandLine cmdLine = CommandLine.parse("cmd"); cmdLine.addArgument("/c start \"\" \"C:\\Program Files\\abc.bat\""); DefaultExecutor exec = new DefaultEx
CommandLine cmdLine = CommandLine.parse("cmd");
cmdLine.addArgument("/c start \"\" \"C:\\Program Files\\abc.bat\"");
DefaultExecutor exec = new DefaultExecutor();
Process p = Runtime.getRuntime().exec(cmdLine.toString());
exec.execute(cmdLine);
上面的代码抛出一个错误,提示“Windows找不到该文件。请确保键入的名称正确,然后重试”。这是因为路径中的空间
因此,我尝试了@brso05在这里提供的答案,这很有效。但我想在以后的课上。请在下面找到我的代码并帮助我修复它
final CommandLine cmdLine = CommandLine.parse("cmd.exe");
cmdLine.addArgument("/c");
cmdLine.addArgument("start");
cmdLine.addArgument("\""+ batchFileExecute.getParent().toString() + "\"");
ExecutorService es = Executors.newFixedThreadPool(1);
Future<?> future = es.submit(new Runnable() {
public void run() {
DefaultExecutor exec = new DefaultExecutor();
try {
Process p = Runtime.getRuntime().exec(cmdLine.toString());
exec.execute(cmdLine);
System.out.println(p.waitFor());
}
catch (IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
});
String thread_status = null;
try
{
thread_status = future.get().toString();
System.out.println(thread_status+" completed the execution");
}
catch (NullPointerException e)
{
System.out.println("The execution of the received project is complete.");
// In here I want to do some processing again.
}
final CommandLine cmdLine=CommandLine.parse(“cmd.exe”);
cmdLine.addArgument(“/c”);
cmdLine.addArgument(“开始”);
cmdLine.addArgument(“\”+batchFileExecute.getParent().toString()+“\”);
Executors服务es=Executors.newFixedThreadPool(1);
Future=es.submit(new Runnable(){
公开募捐{
DefaultExecutor exec=新的DefaultExecutor();
试一试{
进程p=Runtime.getRuntime().exec(cmdLine.toString());
exec.execute(cmdLine);
System.out.println(p.waitFor());
}
捕获(IOE异常)
{
e、 printStackTrace();
}
捕捉(中断异常e)
{
e、 printStackTrace();
}
}
});
字符串线程状态=空;
尝试
{
thread_status=future.get().toString();
System.out.println(线程状态+“已完成执行”);
}
捕获(NullPointerException e)
{
System.out.println(“接收项目的执行已完成”);
//在这里,我想再次做一些处理。
}
我提到的代码可以工作,但如果批处理文件的路径中有空格,则无法工作。你能帮我修一下吗
因为你给的片段很有用,但我不能把它放在未来。它没有按预期的方式工作
提前谢谢 你试过单引号吗?根据,它应该可以工作。这是另一种方法:
Runtime rt = Runtime.getRuntime();
rt.exec("cmd.exe /c start \"\" \"C:\\Program Files\\abc.bat\"");
我在使用ImageMagick时遇到了相同的文件名和空格问题。以下是解决此问题的代码:
String imageOutput = null;
ByteArrayOutputStream identifyStdout = new ByteArrayOutputStream();
ByteArrayOutputStream identifyStderr = new ByteArrayOutputStream();
try
{
DefaultExecutor identifyExecutor = new DefaultExecutor();
// End the process if it exceeds 60 seconds
ExecuteWatchdog identifyWatchdog = new ExecuteWatchdog(60000);
identifyExecutor.setWatchdog(identifyWatchdog);
PumpStreamHandler identifyPsh = new PumpStreamHandler(identifyStdout, identifyStderr);
identifyExecutor.setStreamHandler(identifyPsh);
identifyExecutor.setExitValue(0); //0 is success
CommandLine identifyCommandline = new CommandLine("identify");
identifyCommandline.addArgument(destFile.getAbsolutePath(), false);
DefaultExecuteResultHandler identifyResultHandler = new DefaultExecuteResultHandler();
identifyExecutor.execute(identifyCommandline, identifyResultHandler);
identifyResultHandler.waitFor();
if (identifyResultHandler.getExitValue() != 0)
{
String output = identifyStdout.toString();
_logger.debug("Standard Out = " + output);
_logger.debug("Standard Err = " + identifyStderr.toString());
String msg = "ImageMagick overlay encountered an error. ImageMagick returned a value of " + identifyResultHandler.getExitValue();
throw new Exception(msg);
}
else
{
imageOutput = identifyStdout.toString();
_logger.debug("Standard Out = " + imageOutput);
identifyStdout.close();
identifyStderr.close();
}
}
catch(Exception e)
{
_logger.debug("Error: " + e.getLocalizedMessage(), e);
}
finally
{
identifyStdout.close();
identifyStderr.close();
}
这里的重要部分是:
identifyCommandline.addArgument(destFile.getAbsolutePath(), false);
这一行允许正确处理带有空格的文件路径。它应该可以工作。您确定文件在那里吗?是的。。我试过了。。而且它不起作用。。我正在尝试从Java通过命令提示符运行批处理..尝试我的答案我想它会对你有用…我刚刚发布了另一种方法来启动更少的代码检查我的答案…嘿,伙计,我刚刚编辑了我的答案,现在应该可以了。这只给了我控制台上的路径“D:\Execution\Java>”,我认为这是cmd提示符的默认路径。这对我来说很好。。但是我有个问题。。我在循环中运行它,每个批都有一个特定的操作。。完成操作后,我希望执行下一批。。。为此,我需要使用命令行类。。但是这个解决方案对命令行不起作用…你能帮忙吗?更新了我在问题中剪下的部分。请参阅提供的链接中的这一部分:“由于文件名中的空格,我们必须用单引号或双引号引用文件名”