aksh1t
aksh1t

Reputation: 5448

Controlling applications through Java

I am looking for a way to mimic operating-system (Windows in specific) actions through Java. Preferably, the program should run in the background, but it is not a big deal if it does not. I got the background part covered thanks to this question. I was looking for the following specific features :

So, my actual question is:

Is it possible to minimize/maximize or close an application window from a java program? (in Windows)

Example Scenario:

Firstly the java program is started, and it runs either as a background process or as a window. Bottom-line is that it should be able to accept triggers like maybe a keyboard shortcut or microphone input to trigger the action. After that suppose a Chrome window is opened and is currently active. Now on pressing the pre-defined shortcut, the Chrome window will minimize/maximize or close.

If the answer to the question is yes, I could use some pointers to start with my application. Thanks!

Upvotes: 3

Views: 3643

Answers (3)

jnovacho
jnovacho

Reputation: 2903

You should look into Autohotkey. It's an system dedicated to simulate user programmaticly.

Using AH scripts you can easily access all open windows, installed programs and even control mouse and keyboard.

Upvotes: 0

user529543
user529543

Reputation:

What you need is like an OS shell programming interface. In Java side you will define a few interfaces. Another Java layer will detect which OS is used and will return an implementation of interface: Windows, Linux, Macosx.

Some functionality you can have with simple bash command: in windows cmd, in linux .. to many. Eg shut down, launch MSPaint, Calculator.

Other functionality you can have it with windows API: you will need to write some JNI functions and call it. eg minimize, maximize. It is possible.

Edit: I see there is no accepted answer, although it is answered properly. Here is a C# code which does what you need in Java.

Now you need to migrate this code to Java:

In your java class declare a function:

private native maximizeOrMinimizeWindowWithName(String windowName, boolean maximize);

Compile -it use Javah.exe - it will generate the necesary .h files Use a C editor, configure environment, use the generated .h file.

-include windows api headers -load user32.dll - do more stuf..

compile your C code to .dll

put the your.dll into your app PATH environment variable. ( windows has the . in path, linux not)

-text, bugfix,

  • for more info you should see a basic JNI tutorials.

-upvote accept :)

Upvotes: 2

Marko Topolnik
Marko Topolnik

Reputation: 200138

This can be initiated from Java, but not actually implemented in Java. In other words, it will take a lot of platform-specfiic JNI library code to get it working.

Java will give you almost no benefit for your use case; you should avoid it altogether for this project.

Upvotes: 2

Related Questions