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

Problems with using Java's Runtime.exec()

3 views
Skip to first unread message

Aaron McClimont

unread,
Aug 17, 1998, 3:00:00 AM8/17/98
to
I can't get the following code to work:

Runtime r= Runtime.getRuntime();
r.exec("copy copy c:\autoexec.bat c:\autoexec.old");

The error message that I get is as follows:

java.io.IOException: CreateProcess: copy c:\autoexec.bat c:\autoexec.old
error=2
at java.lang.Win32Process.<init>(Win32Process.java:59)
at java.lang.Runtime.exec(Runtime.java:175)
at java.lang.Runtime.exec(Runtime.java:137)

However, if I try to run a windows program the same way as follows then the
code works... :

Runtime r= Runtime.getRuntime();
r.exec("notepad.exe");

does anyone have any suggestions or solutions?

Regards,

Aaron.

Paul Schwann

unread,
Aug 17, 1998, 3:00:00 AM8/17/98
to
One reason might be that 'copy' is not a real executable (like notepad). May
be you have to start a command shell first, because copy, del, dir, ren etc.
are shell commands:
r.exec("shell copy c:\autoexec.bat c:\autoexec.old");

Paul

Thorsten Seelend

unread,
Aug 17, 1998, 3:00:00 AM8/17/98
to Aaron McClimont

The problem is, that `copy´ is a builtin-command of cmd.exe
resp. command.com. So you have to call one of these shells with the
option /c :

Process p = Runtime.getRuntime().
exec("cmd /c \"copy c:\\autoexec.bat c:\\autoexec.old\"");

Also don't forget to escape the chars " and \ .

Thorsten Seelend

Stuart D. Gathman

unread,
Aug 18, 1998, 3:00:00 AM8/18/98
to Aaron McClimont
Aaron McClimont wrote:
> I can't get the following code to work:
>
> Runtime r= Runtime.getRuntime();
> r.exec("copy copy c:\autoexec.bat c:\autoexec.old");
>
> The error message that I get is as follows:
>
> java.io.IOException: CreateProcess: copy c:\autoexec.bat c:\autoexec.old
> error=2
> at java.lang.Win32Process.<init>(Win32Process.java:59)
> at java.lang.Runtime.exec(Runtime.java:175)
> at java.lang.Runtime.exec(Runtime.java:137)

Do you have a program named "COPY." on your system? I thought not. There is
a program called "COMMAND.COM" on win32 systems, and it has an internal
subcommand named "COPY". Perhaps that is what you meant to run. This has
nothing to do with Java, BTW. You would get the same error from a C program
(but no stack trace :-).

From memory, the /C option to COMMAND.COM lets you run internal commands from
the command line:

r.exec("COMMAND.COM /C copy c:\\autoexec.bat c:\\autoexec.old");

--
Stuart D. Gathman <stu...@bmsi.com>
Business Management Systems Inc. Phone: 703 591-0911 Fax: 703 591-6154
"Microsoft is the QWERTY of Operating Systems" - SDG
"Confutatis maledictus, flamis acribus addictus" - background song for
a Microsoft sponsored "Where do you want to go from here?" commercial.
(HINT: Find translation for the "Confutatis" movement of the Mozart Requiem).

0 new messages