cancel
Showing results for 
Search instead for 
Did you mean: 

running os command

Former Member
0 Kudos

hi i am using this code to execute os command

import java.io.File;

import java.io.BufferedReader;

import java.io.InputStreamReader;

public class RunSystemCommand {

public static void main(String args[]) {

String s = null;

// system command to run

String cmd = "copy a.txt b.txt";

// set the working directory for the OS command processor

File workDir = new File("c:
temp");

try {

Process p = Runtime.getRuntime().exec(cmd, null, workDir);

int i = p.waitFor();

if (i == 0){

BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

// read the output from the command

while ((s = stdInput.readLine()) != null) {

System.out.println(s);

}

}

else {

BufferedReader stdErr = new BufferedReader(new InputStreamReader(p.getErrorStream()));

// read the output from the command

while ((s = stdErr.readLine()) != null) {

System.out.println(s);

}

}

}

catch (Exception e) {

System.out.println(e);

}

}

}

it doesnt work,the error is

java.io.IOException: CreateProcess: copy a.txt b.txt error=2

thx,Shai

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Hi, Shai!

The error 2 comes from the CreateProcess() call from Windows GetLastError(): 2 - "The system cannot find the file specified. - ERROR_FILE_NOT_FOUND". Check the filepath!

Regards,

Thomas

Former Member
0 Kudos

I managed to slove the problem using "cmd /c %commandyouwanttouse%" in the command line

now I want to add other os command

how can I do it ?

thx,

Shai