Discussion Locked

How to execute program in different dire

0 helpful
5 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I try to use exec()(in java.lang.Runtime)system call to execute my program in different directory. I check the specification in java API document but I stll cannot use it.Does somebody know how to use it?

5 replies

by okujava

hi!

with this method u can start a native process of your system: word, excell ...

the first parameter is the Strin name of the application (word.exe, write, command.com...)
the second parameter is an array of arguements for your system process, if required (or null for none), the third parameter is the working directory for your new process (should exist!)

so, if u run this:

public class Test{

public static void main(String args[]) throws Exception{
Runtime r = Runtime.getRuntime();
Process p = r.exec("write test.txt", null, new java.io.File("c:\\"));
}
}

it should open (on windows) c:\test.txt in wordpad and it doesn't matter in which directory u run the Test.class

ciao

shota

by rob wolfe

Here is a direct quote from the lookups at SUN (And hopefully a translation into English). The site is at "http://www.siteforum.com/servlets/sfs?s=FQTok1Co5F3RjHtL&t=import&i=960992632796&b=960992632796&c=Default&l=0&seite=http://java.sun.com/products/jdk/1.3/docs/api%2Fjava%2Flang%2FRuntime.html&keywords=exec"

"Process exec(String[] command, String[] envp, File dir)" Executes the specified string command in a separate process with the specified environment and working directory.

What all that junk really means is that you must have the "command" specified as a command that will actually execute if you type it on the command line in the directory described by "dir".

What I usually do is to create a dos box (or terminal session -pick your o/s), change to the directory i am going to use and then just type in the command and args (just to be sure). One of the most common errors that causes it not to work is for there to be a path error of some sort which kills the command. (Windows is the most miserable for this)

Hope this helps some. And if it doesnt check siteforum.com for quick searches (no i dont work for em )

This discussion is closed to new replies.