RunTime

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

RunTime

Robert L Hamilton
This is what I'm trying to follow:

FROM:

http://intekhabsadekin.wordpress.com/2009/03/05/java-runtimegetruntimeexec-will-and-will-not-work/

Java Runtime.getRuntime().exec(…) will and will not work…

The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class’s exec() method.

There are four overloaded versions of the exec() command:

* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);

For each of these methods, a command — and possibly a set of arguments — is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.

Let me give an example:

Runtime r=Runtime.getRuntime();
Process p=null;

try {
String notepad = “c:\\windows\\notepad.exe”;
String wordpad = “c:\\windows\\regedit.exe”;

p = r.exec(notepad);
p = r.exec(wordpad);

}catch(Exception e) {
System.out.println(“error===”+e.getMessage());
e.printStackTrace();
}

The example given above clearly shows how to invoke executable through the runtime. If you look carefully the exec() method is responsible for creating a new process for the executable, the parameter of which is a command or rather a location to the executable in this case. Look at the command carefully. Lets take one of the commands as example:

“c:\\windows\\regedit.exe”

If you notice the backslashes are escaped using yet another backslash. What if there was a space in the command? For example, “c:\\Program Files\\someFolder\\some.exe”. If you try to print the string then the out would to exactly, c:\Program Files\someFolder\some.exe with the backslashes properly escaped and the space exactly where it should be. But the fact of the matter is that it wont be executed by the exec() method. An exception will be thrown suggesting that “c:\Program” cannot be found. Everything beyond the space has been omitted by the method. Therefore the spaces are not escaped by the exec() method which is bug posted in the Sun Developer Network (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468220). There happens to be a workaround. Instead of assigning the command to a string object, if it was assigned to a string array, the exec() properly executes the command.

End-Note

In  .exec()   I use  exec('Dir C:\Program Files' )

and it gives RunTime() not found . . .

BobH


_______________________________________________
Ibm-netrexx mailing list
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: RunTime

rvjansen
That is because there is no executable for "Dir", as is it a builtin from the shell.

On 7 nov 2010, at 18:56, Robert Hamilton wrote:

This is what I'm trying to follow:

FROM:

http://intekhabsadekin.wordpress.com/2009/03/05/java-runtimegetruntimeexec-will-and-will-not-work/

Java Runtime.getRuntime().exec(…) will and will not work…

The class java.lang.Runtime features a static method called getRuntime(), which retrieves the current Java Runtime Environment. That is the only way to obtain a reference to the Runtime object. With that reference, you can run external programs by invoking the Runtime class’s exec() method.

There are four overloaded versions of the exec() command:

* public Process exec(String command);
* public Process exec(String [] cmdArray);
* public Process exec(String command, String [] envp);
* public Process exec(String [] cmdArray, String [] envp);

For each of these methods, a command — and possibly a set of arguments — is passed to an operating-system-specific function call. This subsequently creates an operating-system-specific process (a running program) with a reference to a Process class returned to the Java VM. The Process class is an abstract class, because a specific subclass of Process exists for each operating system.

Let me give an example:

Runtime r=Runtime.getRuntime();
Process p=null;

try {
String notepad = “c:\\windows\\notepad.exe”;
String wordpad = “c:\\windows\\regedit.exe”;

p = r.exec(notepad);
p = r.exec(wordpad);

}catch(Exception e) {
System.out.println(“error===”+e.getMessage());
e.printStackTrace();
}

The example given above clearly shows how to invoke executable through the runtime. If you look carefully the exec() method is responsible for creating a new process for the executable, the parameter of which is a command or rather a location to the executable in this case. Look at the command carefully. Lets take one of the commands as example:

“c:\\windows\\regedit.exe”

If you notice the backslashes are escaped using yet another backslash. What if there was a space in the command? For example, “c:<a href="smb://Program">\\Program Files\\someFolder\\some.exe”. If you try to print the string then the out would to exactly, c:\Program Files\someFolder\some.exe with the backslashes properly escaped and the space exactly where it should be. But the fact of the matter is that it wont be executed by the exec() method. An exception will be thrown suggesting that “c:\Program” cannot be found. Everything beyond the space has been omitted by the method. Therefore the spaces are not escaped by the exec() method which is bug posted in the Sun Developer Network (http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6468220). There happens to be a workaround. Instead of assigning the command to a string object, if it was assigned to a string array, the exec() properly executes the command.

End-Note

In  .exec()   I use  exec('Dir C:\Program Files' )

and it gives RunTime() not found . . .

BobH

_______________________________________________
Ibm-netrexx mailing list
[hidden email]



_______________________________________________
Ibm-netrexx mailing list
[hidden email]