Discussion:
running Java program from Delphi
(too old to reply)
sv07171024
2008-12-09 07:12:17 UTC
Permalink
Hi,
I'm trying to run a Java program from within my Delphi app using the
ShellExecute function.

When I execute

java -jar C:\Apps\batik-1.7\batik-rasterizer.jar D:\17B0C3B4.svg

from the console it works, but when I use this as the file parameter in my
ShellExecute function, as in

ShellExecute(Handle, 'open', 'java -jar
C:\Apps\batik-1.7\batik-rasterizer.jar D:\17B0C3B4.svg', nil, nil,
SW_SHOWNORMAL);

I get an ERROR_FILE_NOT_FOUND. Replacing "java" with its full path doesn't
help.
Ideaz any1?
TIA
Steven
Rob Kennedy
2008-12-09 15:49:48 UTC
Permalink
Post by sv07171024
I'm trying to run a Java program from within my Delphi app using the
ShellExecute function.
When I execute
java -jar C:\Apps\batik-1.7\batik-rasterizer.jar D:\17B0C3B4.svg
from the console it works, but when I use this as the file parameter in my
ShellExecute function, as in
ShellExecute(Handle, 'open', 'java -jar
C:\Apps\batik-1.7\batik-rasterizer.jar D:\17B0C3B4.svg', nil, nil,
SW_SHOWNORMAL);
I get an ERROR_FILE_NOT_FOUND. Replacing "java" with its full path doesn't
help.
Well, yeah. There is no file named "java -jar C:\Apps\bati..."

The third parameter is named lpFile for a reason. It specifies the
*file* upon which the lpOperation parameter is applied. You have asked
the shell to "open" the file with that long name.

The file you want to open is java.exe. That should be the lpFile
argument. The rest of the command-line parameters should go in the
lpParameters argument.

But since you already know exactly the command line you wish to run, you
should use CreateProcess. That's what it's for. ShellExecute is for when
you have a document or other file, and you're not quire sure what you
need to do with it. You know you want to open it, for instance, but you
don't know what program you're supposed to use or how it expects to
receive its command-line parameters. Or you want to print a file, but
you don't know how to render its contents to the printer's canvas.
--
Rob
sv07171024
2008-12-09 17:42:01 UTC
Permalink
Post by Rob Kennedy
[snip]
ShellExecute(Handle, 'open', 'java -jar
C:\Apps\batik-1.7\batik-rasterizer.jar D:\17B0C3B4.svg', nil, nil,
SW_SHOWNORMAL);
I get an ERROR_FILE_NOT_FOUND. Replacing "java" with its full path
doesn't help.
Well, yeah. There is no file named "java -jar C:\Apps\bati..."
Ah, I knew there was something fishy about it... :-)
Post by Rob Kennedy
The third parameter is named lpFile for a reason. It specifies the *file*
upon which the lpOperation parameter is applied. You have asked the shell
to "open" the file with that long name.
The file you want to open is java.exe. That should be the lpFile argument.
The rest of the command-line parameters should go in the lpParameters
argument.
But since you already know exactly the command line you wish to run, you
should use CreateProcess. That's what it's for. ShellExecute is for when
you have a document or other file, and you're not quire sure what you need
to do with it. You know you want to open it, for instance, but you don't
know what program you're supposed to use or how it expects to receive its
command-line parameters. Or you want to print a file, but you don't know
how to render its contents to the printer's canvas.
CreateProcess does the job. Thanks a lot(*), you've been a great help(*).

I found some (older, but working) code by Chris Bray that wraps the
CreateProcess for easier use:
http://www.delphicorner.f9.co.uk/articles/wapi4.htm

Steven

(*) Why is it that in English these things always sound so ironic?
Anyway, thanks a bunch! (seriously)
Rob Kennedy
2008-12-10 04:24:15 UTC
Permalink
Post by sv07171024
CreateProcess does the job. Thanks a lot(*), you've been a great help(*).
I found some (older, but working) code by Chris Bray that wraps the
http://www.delphicorner.f9.co.uk/articles/wapi4.htm
Make sure you use the "updated and improved" version. The previous leaks
two handles for every call.
--
Rob
Loading...