How to interact with gui application using windows service - java

I have an application in java in which I have start and stop buttons in swing. I want to make a windows service which automatically clicks on start button. Is it possible and if it's possible how to do it?

You must find a way to identify the main window of your swing application from the Windows service (common usage is the title bar)
Then, you search in subwindows how to identify the Start button (generally the id, but you could use text)
All that must be done only once, using the Windows SDK tools and/or you knowing from the GUI application
Now the service :
searches the top-level windows for the right one
searches the relevant button
gets it position
uses SendInput API function to simulate a MOUSEEVENTF_LEFTDOWN and MOUSEEVENTF_LEFTUP event over the button
The last part could be done in Java (with Robot), but the others will need to use Windows API

Related

Java: interact with another process

If I have a random program, that shows some text and has some text input, is there a way to write a java program, that reads that text labels and/or fill that text input fields and press an ok button?
Text applications are things that run on the cli, and have no windows. On Microsoft Windows the 'dir' command is an example.
Graphical applications are things that the beginning user might see, and has buttons, text boxes, scroll bars and stuff like that. On Microsoft Windows the 'paint' program is an example.
Web applications are websites that provide front ends as web pages. The 'gmail' application is an example.
If you want to interact with an application using Java, the application type will determine your approach.
text application - use ProcessBuilder, it's a java class designed to launch and (to a degree) interact with processes through stdin, stderr, and stdout.
Graphical application - It depends on the graphical widgets the application uses. If those are not supported by a library that can navigate the presentation, then input is limited to x,y coordinates from the application's origin, and input could fail to go into the right component.
Web applications - Use selenium. It is a custom web browser solution that permits testing of web sites; but, you could use it for your task. It is big and complicated, but considering what is required for this task, it is comparatively easy to use.
Yes you can write a new program that can give input and trigger the service of 'OK' key.
Your first program needs to be designed in the way that - it should accept the input from second program.
You can design the first code as web service in a web application . your first program will be web service provider and second program will be web service consumer.
Using second program you can post the required data to first code. and all the triggers of 'OK' button can be processed from first application.

how to get the list of applications opened in system tray using java

I found this SO link to get the currently opened applications in task bar.
In addition I need to get the applications running in system tray using Java.
Also suggest me if there is some other good ways to find the opened apps in task bar.
How to find in which application the mouse click and key stroke has been ocuured?
I also want to track the mouse click by viewing which application is opened by the particular mouse click.
I want this application to run both in Linux and windows environment. I need to do all the above in one of my application using Java.

Java Robot get focus of other application

I have a java Robot program ,where it can type a word on notepad,word etc,.
But the problem is i am not getting focus of window application, when i tried to enter the cursor in its textbox manually by using ALT-TAB,
And the application is not available in the list ,while doing ALT-TAB ,
also it is not visible in Task Manager-> Application tab
but, it is available with Processes tab of Task Manager.
Is there any solution to get focus of that windows application for keyPress using Java Robot.
Also ,in some other PC's ,i am getting Clear Focus of the application when i put the cursor manually after running the java robot program. I have installed JDK1.6 in all my PC's. It is not working only in my PC's.
Thanks in advance!!
I think that your best bet is to use JNA to allow you to make system calls.
Assuming a Windows program, you could use JNA to make calls to the user32.dll including dll functions such as FindWindowEx(...) and SetForegroundWindow(...) to set the window of your choice to the foreground.
Instead of getting the focus of the other app, you could put your robot to the background, returning whatever was in the foreground previously (ie your target app) to get the focus.

Embed external application inside Java application (or fake it)

I have a java application that will run on Windows 7 (using Swing, App #1) that runs as full screen (but not in exclusive mode). I have another application (App #2) that displays a GUI to configure an external device over a serial port that I do not have the source to and cannot change at all.
I want to embed App #2 inside of App #1 so that it looks like it is part of the parent java application (hiding the file --> exit button and hiding the minimize, maximize, and close buttons).
If this kind of integration is not possible inside the Java application, I would be fine with opening the process using Java and just monitoring it to keep it open . It would need to keep the window set to "always on top" because App #1 is fullscreen and hide as much of the external MS Windows UI as possible to trick the user into thinking it isn't an external application. Is there some kind of method whether using JNI or something else to manage another processes window (screen location, title bar, minimize, maximize, close button visibility) and process state from inside my Java application?
I will be happy to provide more information if needed.
The following scheme is language independent, I've managed to embed IE window into a Ruby application this way:
First of all, change the style of the external application window (you can use JNA for calling WinAPI):
style = GetWindowLongPtr(APP_HWND, GWL_STYLE);
style |= WS_CHILD;
style &= ~WS_CAPTION;
style &= ~WS_POPUP;
SetWindowLongPtr(HWND, GWL_STYLE, style);
Define parent-child relationship between windows:
SetParent(APP_HWND, JAVA_HWND);
Listen to Move/Resize events of your Java window, and apply new positions on a child window.

Block all other input to an application and control it from a wrapper in Java

I have a windows application which has a complex GUI that I would like to hide from users. In order to do this, I would like to create a wrapper with an extremely simple interface that overlays this application and automates a number of actions when a user clicks a single button on the wrapper. (I hope "wrapper" is the proper term.) Is it possible to use Java to block input to the underlying application so that users cannot inadvertently mess up the automation? How would I go about this? Also, how can I automate key presses and clicks to the application without hijacking the mouse? Is this possible in Java?
I have looked at java.awt.Robot, but it appears to hijack the mouse.
I have also looked at AutoIT, but it too hijacks the mouse and does not integrate with Java.
Neither of these options seem powerful enough for what I need, but I do not know how else to proceed.
I recommend that automation via the GUI only as the last resort if you really have no other alternative.
If there is an API that your application exposes, I would try to use that. For example, if the GUI is implemented in one DLL and the logic in another, then you can use JNA to load your application logic DLL and invoke the application functions directly from java. Even better would be if your application exposes a COM/OLE interface - there are plenty of Java<>COM briges, that will alow you to call this interface directly, e.g. Jacob.
If you really have no choice but to automate via the GUI, then here's how to go about doing that:
Use JNA to access the windows shell API. You can then use ShellExecute to launch your wrapped application. Specifically, passing SW_HIDE as the window mode should help ensure that the application does not appear.
Use JNA to access the windows API FindWindow to find your application window. You can also make it invisible using the ShowWindow API, just in case step 1 did not work (not all applications are written to use the nCmdShow parameter.)
You can now post messages to the application window using PostMessage. You can send keystrokes and mouse events using windows messages. E.g. See WM_KEYUP, WM_LBUTTONDOWN.
Because the wrapped application window is made invisible, you don't need to "block" that application, The user simply cannot access it's GUI. But you can still programmatically send input to it.

Categories

Resources