geibi
geibi

Reputation: 55

Java command prompt commands throw "No such File or Directory"

I am currently working on a programm which should execute some console commands.

My code looks like this:

  private String executeCommands(String[] commands)
  {
    String result = "";
    try
    {
      ProcessBuilder pb = new ProcessBuilder();
      String s = null;
      Charset charset = Charset.forName("IBM850");
      BufferedReader stdInput;
      Process proc;
      for (String command : commands)
      {
        System.out.println("Ausfuehrung von: " + command);
        pb.command(command);
        proc = pb.start();
        stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream(), charset));
        while ((s = stdInput.readLine()) != null)
        {
          result += s;
        }
        System.out.println();
      }
    }
    catch (Exception ex)
    {
      result = ex.getMessage();
    }
    return result;
  }
  private void userLogIn(IUserInteraction userInteraction)
  {
    String[] command = { "svn auth --show-passwords" };
    String result = executeCommands(command);
    System.out.println(result);
  }

The output is "Cannot run program "svn auth --show-passwords": error=2, No such file or directory", but when i manually enter the command in the console, it works. What did i do wrong?

Thanks in advance!

Upvotes: 1

Views: 844

Answers (2)

geibi
geibi

Reputation: 55

After some tries I found a working solution

String result
String line;
      Process process = Runtime.getRuntime().exec(command);
      Reader r = new InputStreamReader(process.getInputStream());
      BufferedReader in = new BufferedReader(r);
      while ((line = in.readLine()) != null)
        result += line;
      in.close();

Upvotes: 1

res
res

Reputation: 815

svn means nothing for the jvm if environment variables are not carried to your application.

Try to execute the command with its full path:

String[] command = { "/bin/dir1/dir2/svn auth --show-passwords" };

If you don`t know where the program is, use the command below to figure it out:

which svn

Upvotes: 0

Related Questions