3

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!

Community
  • 1
  • 1
aksh1t
  • 5,410
  • 1
  • 37
  • 55

3 Answers3

2

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

zEro
  • 1,255
  • 14
  • 24
  • @aksh1t you are welcome, once you will learn the basics with JNI, the next task will be relative easy, the first one is strange, hard and takes long :) –  Jul 09 '13 at 12:48
2

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.

Marko Topolnik
  • 195,646
  • 29
  • 319
  • 436
  • Well, thanks for the input. I was thinking of making a java application for a college project which did system-actions like the mentioned ones after getting speech commands from the user, but it seems that java is not fit for this task. I would be better off manipulating applications like VLC which have a java wrapper. Thanks again. – aksh1t Jul 08 '13 at 15:47
0

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.

jnovacho
  • 2,825
  • 6
  • 27
  • 44