Solved

Running Java applet from Delphi

Posted on 2003-11-30
3
629 Views
Last Modified: 2010-04-06
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
Comment
Question by:mcmahling
3 Comments
 
LVL 7

Accepted Solution

by:
sftweng earned 500 total points
ID: 9846988
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
 
LVL 2

Expert Comment

by:insomniac92
ID: 9847439
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
 
LVL 11

Expert Comment

by:robert_marquardt
ID: 9848381
If you want to know something about the Java Native Interface then have a look here
http://www.pacifier.com/~mmead/jni/delphi/
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now