Executing Java applications inside Delphi programs
Posted: (EET/GMT+2)
Executing Java applications inside Delphi programs
10 Sep, 2000
Delphi 5 Professional or Enterprise,
Microsoft Internet Explorer 4.0
When building large-scale applications for the Internet, applications must seamlessly interoperate even though they have been written with difference languages. One way achieve such interoperability is to use a common carrier, such as TCP/IP which is readily available for all Internet applications. However, sometimes you must execute different applications directly.
When you are running your Delphi application on any 32-bit platform, the odds are that a Java Virtual Machine (JVM) has already been installed on the system. Of course, such a JVM is required to run Java applications and applets. Usually one comes installed with a web browser, such as Microsoft Internet Explorer.
One method of executing a Java application from a Delphi program is simply to execute the JVM process and specify the application to be run on the command line. This could look something like this:
c:\jre1.2.1\bin\java.exe d:\java\classes\MySimpleApp -cp d:\java\classes\other
However, this solution is cumbersome and varies from computer to computer. If you know that at least Internet Explorer 4.0 is installed on the target machine, you can rely on COM and the Microsoft Java VM to execute your Java applications. Enter the Java SDK.
Java SDK and IJavaExecute
The Microsoft Java SDK, available for free download from http://www.microsoft.com/java/ defines two invocation interfaces that can be used from any COM compatible Windows application. The problem with using the simple interface from Delphi applications is that the interface has not been translated into Object Pascal.

However, this isn't a problem anymore as we at WhirlWater have done the conversion for you:
unit JavaExec;
interface
Uses ActiveX;
Const
CLS_MSJava : TGUID = '{3EFB1800-C2A1-11cf-960C-0080C7C2BA87}';
Type
PJavaExecuteInfo = ^TJavaExecuteInfo;
TJavaExecuteInfo = Record
cbSize : Integer;
dwFlags : Integer;
pszClassName : POLEStr;
rgszArgs : POLEStrList;
cArgs : Integer;
pszClassPath : POLEStr;
End;
PErrorInfo = ^IErrorInfo;
IJavaExecute = Interface(IUnknown)
['{3EFB1803-C2A1-11cf-960C-0080C7C2BA87}']
Function GetDefaultClassPath(ClassPath : PChar) : HResult; StdCall;
Function Execute(JEI : PJavaExecuteInfo; PErrorInfo : PErrorInfo) : HResult; StdCall;
End;
implementation
end.
As you can probably guess from the header files, using the IJavaExecute interface is trivial. You will just need to fill in a single record (or structure) and then call the Execute method. There's no need to use IJavaExecute2 as it functions similarly in the case of the example application.
Putting Delphi into work
Let's assume you have a Java application named SimpleApp that is used to process a file. For simplicity, the application could look something like this:
import java.io.*;
public class SimpleApp {
//Construct the application
public SimpleApp() {
}
//Main method
public static void main(String[] args) {
try {
FileWriter fw = new FileWriter("c:\\temp\\hello.from.java.txt");
fw.write("Hello from Java application!");
fw.close();
}
catch(Exception e) {
e.printStackTrace();
}
}
}
To execute this Java application from your Delphi program, you could use the following code:
procedure TForm1.ExecuteClick(Sender: TObject);
Var
JE : IJavaExecute;
JEI : TJavaExecuteInfo;
Res : HResult;
Cls : String;
begin
Cls := ChangeFileExt(ExtractFileName(JavaClass.Text),'');
JE := CreateCOMObject(CLS_MSJava) as IJavaExecute;
FillChar(JEI,SizeOf(JEI),0);
With JEI do Begin
cbSize := SizeOf(JEI);
pszClassName := StringToOLEStr(Cls);
pszClassPath := StringToOLEStr(ExtractFilePath(JavaClass.Text));
End;
Res := JE.Execute(@JEI,nil);
OLECheck(Res);
ShowMessage(OLEStrToString(JEI.pszClassName)+' succesfully executed.');
end;
As you can see, executing an Java application is very easy. Please note that if you get an error "Member not found", you are probably trying to execute a Java applet instead of an application. We will address this problem in later articles.
Download the example code
Download executingjavafromdelphi.zip (178 kB) which contains the sample Delphi application "JavaExecute". Please note that the sample application will require Delphi 5 Professional or Enterprise as well as Internet Explorer 4.0 or later.
* * *
Need help integrating Java with Delphi? Contact us!