error: failed to find java.exe.,错误:导致:java.io.IOException:CreateProcess error = 2,系统找不到指定的文件。适用于所有可执...

My goal is to run SVN commands from java for one of my requirements, for the same i have already installed TortoiseSVN command line tool. Added the appropriate path"C:/Program Files"/TortoiseSVN/bin" to my environment "Path" variable.

With the above setup, i can run my svn commands from windows command line using say "svn --version" and it works perfectly fine.

Now coming back to the code to execute the same, i am using processbuilder for this. However i end up with the above error - java.io.IOException: Cannot run program "svn --version": CreateProcess error=2, The system cannot find the file specified.

I tried following things already,

Using ProcessBuilder.environment checked the Path and PATH values. Path is emply, but PATH has all the necessary application paths configured including "TortoiseSVN/bin" path. So that clears the theory of ProcessBuilder not have executable location in its path.

While executing, instead of just svn --version i tried giving the complete path i.e. "C:/Program Files/TortoiseSVN/bin/svn.exe". That too gave the same error.

I tried the same code for other executable like "java -version" that too failed with the same exception.

I now have a feeling something very basic is not right. But tried hitting my head around this for more than a day now, but i ain't getting any clues.

Ok one more thing, i am running this on Windows 7 box.

Below is the code that i am using,

import java.io.BufferedReader;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStreamReader;

public class RunningExecutable {

public static void main(String[] args){

String command = "svn --version";

ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

try {

ProcessBuilder svnProcessBuilder = new ProcessBuilder(command);

String PATH = svnProcessBuilder.environment().get("PATH");

System.out.println("PATH - " + PATH);

String path = svnProcessBuilder.environment().get("Path");

System.out.println("Path - " + path);

Process procObject = svnProcessBuilder.start();

BufferedReader cmdStreamReader = new BufferedReader(new InputStreamReader(procObject.getInputStream()));

String cmdOutput;

while ((cmdOutput = cmdStreamReader.readLine()) != null) {

outputStream.write((cmdOutput + "\n").getBytes());

}

System.out.println("O/p - " + outputStream.toString());

} catch (IOException e) {

e.printStackTrace();

} catch (Throwable th) {

th.printStackTrace();

}

}

}

Looking forward to any hints/pointers at all.

Thanks,

Vicky

解决方案

It's because you're not using ProcessBuilder correctly. The Javadocs are pretty clear cut.

You can't pass the --version argument as part of the process name you're trying to invoke; that's not the filename of the process. Behind the scenes you're exec'ing a process directly - there's no shell involved.

ProcessBuilder svnProcessBuilder = new ProcessBuilder("svn", "--version");

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值