Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Running Java applet from Delphi

Posted on 2003-11-30
3
Medium Priority
?
693 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 1500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

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…
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…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

721 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