Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Problems with Runtime.exec()

3 views
Skip to first unread message

Whitecrane

unread,
Apr 16, 2003, 3:26:25 PM4/16/03
to
Hi

I've a little problem with the exec() method of the Runtime class

I should send on the command line something similar to: "command.bat
parameter1 parameter2"

So I wrote the following code

String[] commandArray = new String[3];
commandArray[0]="comando";
commandArray[1]="parametro1";
commandArray[2]="parametro2;
Runtime rt = java.lang.Runtime.getRuntime();
rt.exec(commandArray);

But it doesn't work and when I catch the relative exception in the
try/catch block and I print the Stack Trace this is what I get:

java.io.IOException: CreateProcess: command parameter1 parameter2
error=2
at java.lang.Win32Process.create(Native Method)
at java.lang.Win32Process.<init>(Win32Process.java:63)
at java.lang.Runtime.execInternal(Native Method)
at java.lang.Runtime.exec(Runtime.java:550)
at java.lang.Runtime.exec(Runtime.java:475)
at java.lang.Runtime.exec(Runtime.java:441)
at bdEngineTest.bdEngineTest.<init>(bdEngineTest.java:34)
at bdEngineTest.bdEngineTest.main(bdEngineTest.java:50)

the last two lines (those with the error in bdEngineTest.java) are
indicating when the method exec() is called (reported above) and when
the main class is istanziated

I've also tried to put in the first element of the array string
"command.bat" instead of "command" but there is always the same
errore.

In your opinion what can mean that error=2?

Fede

p.s.
I should ask you also another thing.

the .bat file takes almost ten second to end the tasks it put in
motion. (I've tested it launching the bat file from the command line)
and the next operation in the java program should be esecuted when the
bat file has ended its tasks.


What can I do? Can I put the program in sleep or the operation next to
the exec() line is esecuted only when the command present in this
method has been completated?

Or I colud put another exec() just after the previous exec() and
before the line I wish to be blocked? It should be usueful only to
block the next line until the previous exec() is concluded. Can I do
in this way or the exec() are esecuted in a parallel way?


Still Thanks

Ray Sidney

unread,
Apr 16, 2003, 5:26:42 PM4/16/03
to
Regarding your requirement to "wait" .....

Process process = Runtime.exec("yourprocess.bat");
process.waitFor();

This will put you into a wait state until the external process completes.

Ray


Whitecrane <tardis...@hotmail.com> wrote in message
news:pj7r9v4igmk8vblmd...@4ax.com...

Ola Gustafsson

unread,
Apr 17, 2003, 3:10:29 AM4/17/03
to
Whitecrane <tardis...@hotmail.com> writes:


> java.io.IOException: CreateProcess: command parameter1 parameter2
> error=2
> at java.lang.Win32Process.create(Native Method)
> at java.lang.Win32Process.<init>(Win32Process.java:63)
> at java.lang.Runtime.execInternal(Native Method)
> at java.lang.Runtime.exec(Runtime.java:550)
> at java.lang.Runtime.exec(Runtime.java:475)
> at java.lang.Runtime.exec(Runtime.java:441)
> at bdEngineTest.bdEngineTest.<init>(bdEngineTest.java:34)
> at bdEngineTest.bdEngineTest.main(bdEngineTest.java:50)

<snip>


>
> I've also tried to put in the first element of the array string
> "command.bat" instead of "command" but there is always the same
> errore.

You've already got a good answer for how to wait for the process.
The reason the bat-file doesn't execute is, I believe,
because it's not an executable.
I'm not entirely sure, but I think I remember something about
only executable files being able to be run. This is somewhat
weird because shell-scripts work on Unices from Java.
But even so, it seems that the Win32Process class actually tries to load
the executable directly, which means that it cannot execute a bat file.
Try using a simple C-program like this:

#include <stdlib.h>
void main(int argc,char **argv) {
if(argc<2) { return 1; }
return system(argv[1]);
}

and run from java something like this:

Runtime.exec("theProg theBatfile.bat");

sort of.

HTH
--
Ola Gustafsson

Gordon Beaton

unread,
Apr 17, 2003, 3:34:39 AM4/17/03
to
On 17 Apr 2003 09:10:29 +0200, Ola Gustafsson wrote:
> Try using a simple C-program like this:
>
> #include <stdlib.h>
> void main(int argc,char **argv) {
> if(argc<2) { return 1; }
> return system(argv[1]);
> }
>
> and run from java something like this:
>
> Runtime.exec("theProg theBatfile.bat");

It's not necessary to write your own launcher for the batch file. The
command shell is perfectly capable of that:

command.exe /C myBatch.bat

(or cmd.exe, or whatever)

/gordon

--
[ do not send me private copies of your followups ]
g o r d o n . b e a t o n @ e r i c s s o n . c o m

Ola Gustafsson

unread,
Apr 17, 2003, 3:53:34 AM4/17/03
to
Gordon Beaton <n...@for.email> writes:

> It's not necessary to write your own launcher for the batch file. The
> command shell is perfectly capable of that:
>
> command.exe /C myBatch.bat
>
> (or cmd.exe, or whatever)

Ohh. Of course. Pardon me for being stupid. =)
Sometimes you don't think as much as you should do.

I guess this is the reason that shell-scripts work,
because the invocation of them uses the shebang-line
and start them in the way you demonstrated.

--
Ola Gustafsson

Whitecrane

unread,
Apr 17, 2003, 4:41:47 AM4/17/03
to
Gordon Beaton <n...@for.email> writes:

> It's not necessary to write your own launcher for the batch file. The
> command shell is perfectly capable of that:
>
> command.exe /C myBatch.bat
>
> (or cmd.exe, or whatever)


So this mean that I have to put the complete absolute path of the
myBatch.bat, because it doesn't mean anything for the cmd.exe that the
the bat file and the java class are in the same folder, right?

Thank you for all your answers.

Fede

Gordon Beaton

unread,
Apr 17, 2003, 4:52:47 AM4/17/03
to
On Thu, 17 Apr 2003 08:41:47 GMT, Whitecrane wrote:
> So this mean that I have to put the complete absolute path of the
> myBatch.bat, because it doesn't mean anything for the cmd.exe that
> the the bat file and the java class are in the same folder, right?

Any program you execute with Runtime.exec() will run in the same
current working directory as the JVM, and this normally has little or
nothing to do with the location of any class files the JVM has read
(there is nothing that says an applications classfiles are collected
in one place either, so this distinction is important).

So you should give cmd.exe either an absolute path to the batch file,
or a path relative to the current working directory.

Note too that you can tell Runtime.exec() to start cmd.exe in a
different directory (see the API docs), in which case a relative path
to the batch file should be relative to that directory instead.

Ray Sidney

unread,
Apr 17, 2003, 10:43:02 PM4/17/03
to
Sorry folks, but you can indeed run .bat files from Runtime.exec(); Also,
pretty darn sure that (on Billy Gates boxes), the .bat will be located via
your PATH environment variable setting.

Cheers,
Ray


Gordon Beaton <n...@for.email> wrote in message
news:b7lq0v$ghh$1...@news.du.uab.ericsson.se...

Whitecrane

unread,
Apr 18, 2003, 5:54:20 AM4/18/03
to
Well, at last I've succed it. Anyway the problem is that if i put the
process.waitFor() command the process seems to go in loop and doesn't
end the task. I have to stop the process from the java editor to make
the bat file ends its tasks.

If i put away the process.waitFor all works

What it could be?

Buona Pasqua

Federico


On Fri, 18 Apr 2003 02:43:02 GMT, "Ray Sidney" <ray...@coral.com>
wrote:

Gordon Beaton

unread,
Apr 18, 2003, 6:09:08 AM4/18/03
to
On Fri, 18 Apr 2003 09:54:20 GMT, Whitecrane wrote:
> Well, at last I've succed it. Anyway the problem is that if i put the
> process.waitFor() command the process seems to go in loop and doesn't
> end the task. I have to stop the process from the java editor to make
> the bat file ends its tasks.
>
> If i put away the process.waitFor all works
>
> What it could be?

Are you reading from both of the process' output streams?
(p.getInputStream() and p.getErrorStream())?

asj

unread,
Apr 20, 2003, 2:03:02 AM4/20/03
to
Ray Sidney wrote:
>
> Regarding your requirement to "wait" .....
>


here's another way that works fine for me:


Runtime rt = Runtime.getRuntime();
try{
Process proc = rt.exec(scriptLocation);
BufferedReader di = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
BufferedReader de = new BufferedReader(new
InputStreamReader(proc.getInputStream()));

String s;
while ( (s = di.readLine()) != null) {
//messenger.out (appId+": on stdout, read: " +
s,3);
}
while ( (s = de.readLine()) != null) {
//messenger.out (appId+": on stderr, read: " +
s,3);
}
} catch (IOException e){
rc=false;
}

return rc;
}

0 new messages