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!
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.
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.
Related
I want to develop a Java application, hoping that the system never goes to hibernate when my application running.
The application will be deployed in Windows.
Is there any way to realize this?
There is nothing in java for this.
There are windows API to prevent hibernation. See this thread Prevent windows from going into sleep when my program is running?
You could all call them via JNI or JNA
There are two ways to avoid your system going into Hibernate mode when your application is running:
1) I don't know which Windows operating system you are talking about; but we can disable or enable Hibernation at an operating system level. The enabling/disabling method might differ for different Window versions.
2) Another way would be to write a C++ program that uses Win32 API to interact at system level. After writing the code, you can export it as a DLL library and then use it in the Java program. Below link provides a sample code that will help you achieve similar functionality.
http://www.codeguru.com/cpp/w-p/system/messagehandling/article.php/c6907/JavaC-PC-Standby-Detect-and-Prevent.htm
I had similar problems when i wanted to connect via RDP to my pc, i left teamviewer on, but my pc went to hibernate/sleep and this is my solution how i keep my pc "active".
Try this, go to Control Panel->Power Options:
and there u can select power plan, click on "Change plan settings" and u will get to this screen:
Hope that this will help u.
If u need some programmatic solution, try with this link:
How do you keep the machine awake?
I want this to happen:
Start my java program
My java program launches notepad.exe
If I type a letter in notepad my java program should react and print out the letter
In short: Is it possible to make a key event listener for another process? The java program won't have focus.
If it's impossible I want to be able to change focus between my Java swing window and the notepad process. For example:
A) My java program has focus (small swing window), I type the letter "A"
B) Notepad is given focus quickly and the letter A is typed there (for example using the Robot class)
C) My java program gets focus again
Quoting the answer given in Creating a keyboard hook for a Java program that sits in the System Tray
so , creating a keyboard hook isn't as easy as it looks , java doesn't provide a direct way to interact with the system events for purposes of security ; as it might lead to serious problems concerning the JVM , system's memory and the portability of Java Framework..
you have 4 ways to make global keyboard hooks :
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
Use JIntellitype , which - as you said - issues some problems.
the elegant solution by Sergei Biletnikov here http://biletnikov-dev.blogspot.com/2009/09/global-hotkeys-for-java-applications_25.html
ignore Java , and try Autoit V3 ( I'm not 100% sure about it , but I think you could send signals/events from Autoit to your Java app , so Autoit would just work as a bridge that catches the global key strokes)
Going with number 3, which is a good tutorial. If the link has 404'd maybe the project source is still up: gigapeta.com/dl/1917618aba749a
With your presented solution #1:
Learn JNI / JNA, and I prefer JNA since its much easier than JNI , in both cases you shall deal with .dll files.
You should take a look at this stackoverflow thread:
JNA Keyboard Hook in Windows
There is copy/pastable code that demonstrates a JNA key hook. You will need to download the JNA dependencies from http://jna.java.net/ for the example to work. You won't even need to fiddle with silly DLL's.
Additionally, you might want to give this library a try. While I have no experience with it, it popped up on my google search for "java keyboard hook." It seems to accomplish the goal of intercepting the keystrokes:
http://code.google.com/p/jnativehook/
Note that you would need some additional native code to see what the current "active window" is. If the active window matches "Notepad.exe" then you should record a native key event.
Good luck!
I am creating a program using Java Sockets in which I capture the client desktop and send messaging to client. Its working properly but now I want to block Client applications like Notepad, MS-Word, etc.
How can I do this?
Thanks.
It is hard to do using pure java API.
I do not know what do you mean when you say "block". The easiest way is to check from time to time running processes and kill one named "notepad" by executing taskkill from java.
If you wish to achieve effect of inactivity of application, i.e. user sees the notepad but cannot type you can do the following.
You have to check which application is on front. There is no clean pure java solution for this but you can probably write VBScript or JScript that does this task and run it from java. Once you detected that notepad is on top create transparent window (or even probably half-transparent window) that occupies full screen. Bring it on top. User will not be able to type into notepad because your window is on top but will see it.
Here is reference how to create transparent windows: http://java.sun.com/developer/technicalArticles/GUI/translucent_shaped_windows/
Good luck.
First of all, I'm a java developer and I am currently working on a small application for Windows only.
In my application, I wish to do as dropbox or tortoise do : add an overlay icon in windows explorer to show the user some state of files managed by my application. (I want the icon of the file change depending on some data stored in the file)
Is it possible to do so in Java ? Do you have examples ?
If it is doable but not efficient, how would you do instead ?
Thanks in advance
Fluminis
It would be possible to do this via JNI - you would need to hook into the Windows registry and from there into the Explorer shell, probably into the various file classes held there.
However, unless you have at least some familiarity with C++ and the windows API, you are unlikely to be able to achieve this.
Java is not the ideal language for what you want to do.
I am calling MATLAB with Java but I want to suppress the command window of MATLAB to make users feel that I use only one program which is Java.
In addition I read about something called standalone executable for MATLAB, but it didn't work; will that help me?
Check out the Matlab Engine. The engine runs in the background (without a GUI or visible command-line) and you call it from your code. The examples are in C and Fortran, not Java, unfortunately. I got it working with Python once but I don't recall the details.
Also see: 2 ways to use the engine with Java.
ETA: 'matlab -r "statement"' on the (Windows) command line will execute "statement" in Matlab. My Python hack was putting my Matlab code in a .m file and my data into a text file referenced by the .m file then sending 'matlab -r myFile.m' to the Windows command line. See the matlab Windows command. Again, there's no visible GUI for Matlab this way.
When you say "calling it from Java", are you shelling out to Matlab for batch computations, or do you want to embed a long-lived Matlab session in your process and call M code repeatedly from Java code? What OSes do you want to run on?
Matlab has some deployment tools that let you embed a Matlab interpreter and a collection of Matlab source code inside a host language, such as C/C++ or Java. This is what the "Matlab compiler" is - not a real compiler, but a tool that packages a Matlab runtime along with .m source code in a package that looks like a DLL or application. A Matlab "standalone application" is Matlab code that has been packaged this way along with a thin C wrapper that calls an application entry point in your M code.
The Matlab Java Builder is a similar thing that bundles this deployed Matlab engine inside a Java class. If you want to get a license for it, that could make it easy and cosmetically clean to embed Matlab inside your Java application. This is probably what you want.
These deployed Matlab apps do not have a command window because they're intended to blend in with your application. They live in the same process. And, importantly, they do not require license fees for running the deployed app. Shelling out to regular Matlab requires all users running it to have licenses for Matlab and each toolbox that is used.
If shelling out, the "matlab -nosplash -nodesktop" command line will suppress the GUI on Unix. But on Windows you'll still get a minimal Matlab command window. The "-automation" switch on Windows will at least make it minimized. I don't know a way to suppress it entirely on startup.
However, once Matlab is running, you can take advantage of the fact that the Matlab GUI is itself implemented in Java, and have it hide itself. Get your Matlab session to run this hidematlab() using the "-r" command line switch or a startup.m. Note that this is a hack using undocumented Matlab internals and is surely unsupported by MathWorks.
function hidematlab()
%HIDEMATLAB Hide the main Matlab desktop window (HACK)
dtWin = desktopwindow();
if ~isempty(dtWin)
dtWin.setVisible(0);
end
function out = desktopwindow()
%DESKTOPWINDOW Find the main Matlab desktop window (HACK)
wins = java.awt.Window.getOwnerlessWindows();
out = [];
for i = 1:numel(wins)
if isa(wins(i), 'com.mathworks.mde.desk.MLMainFrame')
out = wins(i);
return;
end
end
Beware of gotchas when shelling out on Windows, where Matlab is inherently a GUI app. If your M code throws errors that bubble up to the top level or segfaults, you may find your Matlab session hung, waiting for nonexistent user input, instead of returning you an error.
I don't know of a way to do what you're asking entirely. If your script does an exit manually instead of naturally terminating, you may be able to start the script so that the window that pops up is minimized.
See Launch Application in a minimized state from Java
Start the script with
matlab -nojvm -nosplash -nodesktop -wait -r script_name
You will want the "-wait", otherwise MATLAB will immediately return.
See How can I stop MATLAB from returning until after a command-line script completes?
matlabcontrol is a Java API which will allow you to interact with a running session of MATLAB. It will launch the session and then you will be able to invoke eval and feval as well as set and get variables. By default the session of MATLAB will be visible, but it can be hidden. On Windows it will not be entirely hidden because that is not supported by MATLAB, but it will be started minimized and no splash screen will be shown. To get started using matlabcontrol, take a look at the walkthrough.