5

My problem is pretty simple i have an application written in java and i want to send commands to it ex ( click a button , send some key strokes , click a menu item ) from my application witch i will write in delphi. Is this concept even possible ?

1
  • 4
    Do your controls have window handles? Commented Jun 22, 2011 at 11:28

6 Answers 6

4

I actually had to do this at the last place I worked, you can get around it with complex window events etc... as mentioned above but if you have access to the Java source simply write other access methods either that call a specific runtime that closes (i.e. trigger a public static void main(String[] args); via a native call or via the command line.

OR

Implement simple a simple message system between Java/Delphi over TCP/IP and send either XML or some simple string mappings (I think it took about an hour to set up Maps that could pass back and forth).

In my case we were simply handling reporting and talking to the database so it was pretty easy to work around without getting into a native call. Alternatively, there is(was) a port of the JNI for Delphi that worked pretty well with Delphi 7. I have no clue what runtime you're using but it might be an option.

Honestly, the TCP/IP method is probably the easiest. It doesn't take a lot to implement, it doesn't eat a lot of resources and it allows you to execute "myMenuItem.onClick()" pretty easily as a packet, you just have to expose the methods.

http://home.pacifier.com/~mmead/jni/delphi/

Sign up to request clarification or add additional context in comments.

Comments

4

Well It depends on which Java GUI technology is used . If SWT or AWT is used , you can get handle of UI components, because these two toolkit uses native libararies.. On the other hand, if that java application GUI is created by beans of SWING, you can not get any handle. Because, swing toolkit is implemented by pure Java..

Comments

3

If the Java app can be modified, the Java Robot API (included in JRE 1.3 and newer) might be helpful. This would allow to control a Swing application which does not provide windows handles as Gursel wrote. Obviously there would be some IPC required, which could be implemented using sockets for example.

Comments

2

The short: YES, but depending on the Java application, it might be difficult and unreliable.

I'm not a Java guy so I don't know if this is the norm, but the one Java application I had to automate displayed a single dialog that only used 1 (one) window handle! It was made up of several edit boxes, buttons, what looked like combo-boxes, but those were not true Windows controls but widgets re-created by whatever GUI toolkit the original developer used. I wasn't able to use normal Windows messages to manipulate those because, as far as Windows was concerned, it was a single window.

Happily the only thing I had to do was click a single button. I used mouse_event to move the mouse over the expected area for the button and then again to click the button. It works, but manipulating input this way is both unreliable and fragile.

4 Comments

Eek. Font scaling could have made that interesting. Imagine if it meant you clicked the "reformat hard drive" button rather than the "submit form to website" button!!
@David, I know, happily there's no "reformat hard drive" button. The developer of the application (a government agency) provided command line switches to fill the required edit boxes but (unbelievably!) they forgot to auto-start the application, a single button needs to be pushed. Failure mode is pretty safe; if my Delphi app misses the button the operator will likely push it himself.
@David: That is why you try to hide your "eject seat" button as much as possible so it is hard to click :) Unless you like to watch people ejecting all the time :))
Does the reliability really depend only on the Java side? ;)
2

To clarify Daniel Chapman  and mjn  comments, find below a code extract showing Delphi controlling a Java Swing UI component (TextField) contained in a Jframe based on NetBeans ClientEditor sample.

Please notice, that this example does not use the Java source code or use TCP, XML, Windowing events handling technics or IPC, it's just

simply Delphi code calling some Java code.

procedure TForm1.Button1Click(Sender: TObject);
begin
  FJFrame := Tjavax_swing_JFrame.Create('Client Editor');
  FClientEditor := Tclienteditor_ClientEditor.Create;
  FJFrame.GetContentPane().Add(FClientEditor);
  FJFrame.Pack;
  FJFrame.SetVisible(True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  // Delphi setting a value in a Java Swing UI component
  FclientEditor.FirstNameTextField.SetText('Delphi 1stName');
end;

The long type names are just as a matter of clarity for this example and can be shorter of course.

Additionally there is no problem with JNI in this example.

Comments

1

Have you had a look at Java for Delphi?

It let's you call Java from Delphi exposing the Java types as Delphi types.

3 Comments

A late add here, but that looks interesting--what's the API like?
Same problem as with JNI, if a running Java application needs to be controlled (buttons cliecked etc.) it does not help. With JNI maybe a Java virtual machine can be launched and forced to execute the Java app, but I think the original question is about a running Java (Swing / AWT) user interface.
+1 for mentioning JX for Delphi. But the product don't seem to be still really alive. The last officially supported version ist Delphi 2009.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.