Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling applications through Java

Tags:

java

windows

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 :

  • Maximizing/Minimizing the currently active window. (Can be any window, not just the Java application window.)
  • Closing the currently active window.
  • Open installed programs, and system utilities like the calculator, paint, etc. (I figured out this one from this question.)
  • Shutdown/Restart (This one's done too, thanks to the question here.)

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!

like image 544
aksh1t Avatar asked Dec 06 '25 00:12

aksh1t


2 Answers

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 :)

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.

like image 42
Marko Topolnik Avatar answered Dec 08 '25 13:12

Marko Topolnik