• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 797
  • Last Modified:

Running Java applet from Delphi

I need to run a java .jar file from my Delphi 7 application.  I have not used java before. What are the options for calling the .jar file?
0
mcmahling
Asked:
mcmahling
1 Solution
 
sftwengCommented:
I recommend that you look at the JEDI site - they have release (free) the JCL and JVCL component libraries - among them is a "Shell" unit called JclShell that makes it easy to invoke the underlying Windows shell capabilities, including running an application. You can find them at http://www.delphi-jedi.org/
0
 
insomniac92Commented:
I would suggest embedding the TWebBrowser component and loading the Java Applet into that. Here is a cut down version of a component I wrote a while ago using a TPageProducer and the TWebBrowser component . I have just stripped out a lot of extra stuff without re-compiling it or testing it but it should give you enough of an idea of how to do this.
//component unit starts here...

unit JavaAppletComponent;

interface

uses
  SysUtils, Messages, Classes, Controls, HTTPApp, DsgnIntf, FileCtrl, ActiveX,
  ComObj, SHDocVw, extctrls, Forms, olectrls, dialogs;

type
  TJavaAppletComponent = class(TCustomPanel)
  private
    FAppletPath            : string;
    FAppletFile            : string;
    FAppletParams          : TStrings;
    FWebBrowser            : TWebBrowser;
    FActivate              : boolean;
    FAutoSize              : boolean;
    FPageProducer          : TPageProducer;
    FHTMLFile1, FHTMLFile2 : string;

    procedure SetAppletParams(Value: TStrings);
    procedure SetActive(Value: boolean);
    procedure SetAppletPath(Value: string);
    procedure SetAppletFile(Value: string);
    procedure SetAutoSize(Value: Boolean);

  protected

  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
    property AppletPath: string read FAppletPath write SetAppletPath;
    property Canvas;

  published
    property AppletParams: TStrings read FAppletParams write SetAppletParams;
    property Active: boolean read FActivate write SetActive;
    property AppletFile: string read FAppletFile write SetAppletFile;
    property AutoSize: boolean read FAutoSize write SetAutoSize;
    property Caption;
    property BevelInner;
    property BevelOuter;
    property BevelWidth;
    property BorderStyle;
    property BorderWidth;
    property Color;
    property Cursor;
    property Hint;
    property ShowHint;
    property TabStop;
    property TabOrder;
    property Visible;
    property OnMouseDown;
    property OnMouseMove;
    property OnMouseUp;
    property OnDragOver;
    property OnEndDrag;
    property OnDragDrop;
    property OnStartDrag;
  end;

  TAppletFileNameProperty = class(TStringProperty)
  public
    procedure Edit; override;
    function GetAttributes: TPropertyAttributes; override;
  end;


procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('InteracTV', [TJavaAppletComponent]);
  RegisterPropertyEditor(TypeInfo(string), TJavaAppletComponent, 'AppletFile', TAppletFileNameProperty);
end;

constructor TJavaAppletComponent.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  Width := 65;
  Height := 65;
  Caption := '';
  BevelOuter := bvNone;
  BevelInner := bvNone;
  BorderStyle := bsNone;
  FAppletParams := TStringList.Create;
  FPageProducer := TPageProducer.Create(Self);
  FPageProducer.Dispatcher := nil;
  FPageProducer.HTMLDoc.Clear;
  FPageProducer.HTMLFile := '';
  FWebBrowser := TWebBrowser.Create(Self);
  FWebBrowser.Align := alClient;
  FWebBrowser.Silent := True;
  FWebBrowser.MenuBar := False;
  FWebBrowser.AddressBar := False;
  FWebBrowser.StatusBar := False;
  FWebBrowser.TheaterMode := False;
  FWebBrowser.RegisterAsBrowser := False;
  FWebBrowser.RegisterAsDropTarget := True;
  FWebBrowser.TabStop := FALSE; //
  FWebBrowser.ToolBar := -1;
  FWebBrowser.OffLine := False;
  Self.InsertControl(FWebBrowser);
  TOleControl(FWebBrowser).Visible := False;
  Invalidate;
end;

destructor TJavaAppletComponent.Destroy;
begin
//  if FHTMLFile1 <> '' then
//    DeleteFile(PChar(FHTMLFile1));
//  if FHTMLFile2 <> '' then
//    DeleteFile(PChar(FHTMLFile2));
  FAppletParams.Free;
  if FWebBrowser <> nil then
  begin
    FWebBrowser.Free;
    FWebBrowser := nil;
  end;
  FPageProducer.free;
  FPageProducer := nil;
  inherited Destroy;
end;


procedure TJavaAppletComponent.SetAutoSize(Value: boolean);
var
 i, z, wPos, hPos : integer;
 wStr, hStr, sWidth, sHeight : string;
 WidthFound, HeightFound : Boolean;
begin
  FAutoSize := Value;
  if FAutoSize = True then
  begin
    if FAppletParams.Count > 0 then
    begin
      wPos := 0;
      hPos := 0;
      for i := 0 to FAppletParams.Count - 1 do
      begin
        if wPos = 0 then
        begin
          wPos := Pos('WIDTH', UpperCase(FAppletParams.Strings[i]));
          if wPos > 0 then wstr := Copy(FAppletParams.Strings[i], (wPos + 5), (Length(FAppletParams.Strings[i]) - wPos));
        end;
        if hPos = 0 then
        begin
          hPos := Pos('HEIGHT', UpperCase(FAppletParams.Strings[i]));
          if hPos > 0 then hstr := Copy(FAppletParams.Strings[i], (hPos + 6), (Length(FAppletParams.Strings[i]) - hPos));
        end;
      end;
      if wPos > 0 then
       begin
         WidthFound := False;
         i := 0;
         while (i < (Length(wStr) - 1)) and (not WidthFound) do
         begin
           if wStr[i] = '"' then
            begin
              for z := i + 1 to Length(wStr) - 1 do
              begin
                if WidthFound = False then
                 begin
                  if wStr[z] <> '"' then sWidth := sWidth + wStr[z];
                  if wStr[z] = '"' then WidthFound := True;
                 end;
              end;
            end;
            inc(i);
         end;
         if WidthFound then Self.Width := StrToInt(sWidth);
       end;

      if hPos > 0 then
       begin
         HeightFound := False;
         i := 0;
         while (i < (Length(hStr) - 1)) and (not HeightFound) do
         begin
           if hStr[i] = '"' then
            begin
              for z := i + 1 to Length(hStr) - 1 do
              begin
                if HeightFound = False then
                 begin
                  if hStr[z] <> '"' then sHeight := sHeight + hStr[z];
                  if hStr[z] = '"' then HeightFound := True;
                 end;
              end;
            end;
            inc(i);
         end;
         if HeightFound then Self.Height := StrToInt(sHeight);
       end;
    end;
  end;
  Invalidate;
end;

procedure TJavaAppletComponent.SetAppletPath(Value: string);
begin
  if FAppletPath <> Value then
  begin
    FAppletPath := Value;
    Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetAppletFile(Value: string);
var
 i, j : integer;
 s1 : string;
begin
  if FAppletFile <> Value then
  begin
     FAppletFile := Value;
     if FAppletFile <> '' then
     begin
       for i := 0 to Length(FAppletFile) do
       begin
         if FAppletFile[i] = '\' then j := i;
       end;
       FAppletPath := Copy(FAppletFile, 0, j - 1);
     end;
     Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetActive(Value: Boolean);
var
 wFlags            : OleVariant;
 wTargetFrameName  : OleVariant;
 wPostData         : OleVariant;
 wHeaders          : OleVariant;
 i                 : integer;
 ipos              : integer;
 v                 : variant;
begin
  if Value <> FActivate then
  begin
    FActivate := Value;
    case FActivate of
      TRUE:
        begin
          if FAppletParams.Count= 0 then
          begin
            FActivate := False;
            showmessage('Error: Applet Params not set.');
            exit;
          end;
          if FAppletFile = '' then
          begin
            FActivate := False;
            showmessage('Error: Applet File property not set.');
            exit;
          end;
          if not fileExists(FAppletFile) then
          begin
            FActivate := False;
            showmessage('Unable to locate Applet file.');
            exit;
          end;

            if FAutoSize = True then SetAutoSize(True);
            FPageProducer.HTMLDoc.Clear;
            FPageProducer.HTMLDoc.Add('<body topmargin="0" leftmargin="0" rightmargin="0" bottomargin="0" SCROLL="no">');
            for i := 0 to FAppletParams.Count - 1 do
            begin
              FPageProducer.HTMLDoc.Add(FAppletParams.Strings[i]);
            end;
            FPageProducer.HTMLDoc.Add('</applet>');
            FPageProducer.HTMLDoc.Add('<body/>');
            FPageProducer.HTMLDoc.SaveToFile(FAppletPath + '\' + inttostr(Self.handle) + '.html');
            FHTMLFile1 := FAppletPath + '\' + inttostr(Self.handle) + '.html';
            wFlags := 2;
            wPostData := False;
            FWebBrowser.Navigate(FAppletPath + '\' + inttostr(Self.handle) + '.html', wFlags, wTargetFrameName, wPostData, wHeaders);
            TOleControl(FWebBrowser).Visible := True;
          Self.Visible := True;
        end;

      FALSE:
        begin
          TOleControl(FWebBrowser).Visible := False;
        end;
    end;
    Invalidate;
  end;
end;

procedure TJavaAppletComponent.SetAppletParams(Value: TStrings);
begin
  if Value <> FAppletParams then
  begin
    FAppletParams.Assign(Value);
    if FAutoSize = True then SetAutoSize(True);
    Invalidate;
  end;
end;

procedure TAppletFilenameProperty.Edit;
var
  Dialog: TOpenDialog;
begin
  Dialog := TOpenDialog.create(Application);
  Dialog.Filter := 'Class Names (*.class)|*.CLASS';
  Dialog.Title := 'Select an Applet File...';
  Dialog.Options := [ofHideReadOnly,ofPathMustExist,ofFileMustExist];
  Dialog.Execute;
  SetStrValue(Dialog.FileName);
  SetValue(Dialog.FileName);
  Dialog.Free;
end;

function TAppletFilenameProperty.GetAttributes: TPropertyAttributes;
begin
  Result := [paDialog];
end;

end.

//end of component unit....

Cheers

0
 
robert_marquardtCommented:
If you want to know something about the Java Native Interface then have a look here
http://www.pacifier.com/~mmead/jni/delphi/
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now