2

I am trying to run some code, but I get an error:

String strCmd = "psexec.exe \\server -u use -p aaa  tasklist";
Process process  = Runtime.getRuntime().exec(strCmd);
BufferedReader stdout = null;
stdout = new BufferedReader(new InputStreamReader(process.getInputStream())); 
String line;

for(int l = 0; ( line = stdout.readLine()) != null; )
{     
    System.out.println ("output ->"+line);  
} 
stdout.close(); 
BufferedReader stdErr = new BufferedReader(new InputStreamReader(process.getErrorStream())); 
for(int l = 0; (line = stdErr.readLine()) != null; ) 
{  
    System.out.println ("Error Output ->"+line);     
} 
stdErr.close();   

And now I am facing this error:

Exception in thread "main" java.io.IOException:
Cannot run program "psexec.exe":
CreateProcess error=2, The system cannot find the file specified

What do need to change to make this code working?

zx485
  • 28,498
  • 28
  • 50
  • 59
brucewayne
  • 53
  • 2
  • 7

1 Answers1

3

The program psexec.exe is not in the PATH variable of your java process.

Try to launch the .exe file with its full path or add the directory where psexec.exe is to your PATH environment variable.

Lechuk
  • 61
  • 4