Solved

Running Java applet from Delphi

Posted on 2003-11-30
3
679 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month6 days, 19 hours left to enroll

623 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