How to prevent system from going to hibernate? - java

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?

Related

Close Child Java windows using C#

I have a java application running on Windows (javaw.exe) and I would like to close the child windows through a C# application. I can see the child windows through task manager under the Applications tab but when I right-click on the child windows and click Go To Process, it takes me to the javaw.exe process running under the Processes tab.
I have tried iterating through active processes to close each window however, I am unable to find the child java windows and only can see the javaw process.
Process[] childProcesses = Process.GetProcessesByName("javaw");
I have searched the internet forums and have not been able to find a proper solution that deals with a java application running on windows to be be dealt with using C#. I'm sure there is an obvious solution so any help is much appreciated. Thanks.
If I understand correctly your problem, you want to close one or more window among many of a java application ?
You won't have child processes on javaw, you only have threads running inside the JVM. Maybe using the Threads property of the process object could help.
But even doing that, all graphic objects in a java application are rules by the AWT Thread. Killing it will mean closing all the windows.
So i don't think it's possible to do what you want to do :/
Could you try to get handle of the child window and send message to it using P\Invoke methods and calling win32 api?
Maybe this answer can cover this idea:Cannot use pinvoke to send WM_CLOSE to a Windows Explorer window

Can Java inject DLL in another process?

I've actually already asked questions regarding my problem. To prevent people asking for irrelevant details over again, I'll post this background info you can skip:
I'm making a automation application. This application does some tasks
for the user over time for specific window. When these taks are
required to be made, window flashes in taskbar and sometimes even
steals focus to get the user to do the task. Once the automation is
here, this is no longer wanted - the user will only focus the window
when he wants to check how well is the automation doing it's job.
I discovered this focus and flash disable dll project thanks to this superuser post about applications stealing focus.
My automation application is in Java. So while I can open a DLL injection application and disable flashing manually, I'd like to integrate it in the java applicaton - for example as a setting option. When user selects to disable flashing and stealing focus, the dll will be injected.
Of course, this requires Java to be able to inject my .dll file. I've found this project: dotnet-dll-injector but it only deals with .NET dll files.
Q: Is there a way, in Java, to pick a .dll file and inject it into process? Which libraries would lead to this if the solution isn't straightforward?
I've noticed CreateRemoteThread is somehow related to the DLL injection. Maybe JNA library supports that?
I don't know of a way to do it natively in Java.
Since you've found a working solution in .NET and your task is Windows-specific, I suggest the following:
Build dotnet-dll-injector as a DLL and call it from your Java app. How to call into .NET dll from Java
Edit: easier to build it as a console app and use Runtime.getRuntime().exec("...");
Or if you're still early in the development process and you have no immediate plan to go cross-platform, just do everything in C# and save some headache.

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

How to write a Icon Handlers for explorer.exe in Java

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.

UAC and Java

Is it possible to ask for elevated permissions from within a Java Application? Suggestions I've seen seem to all be centered around running an external executable or setting up a manifest to request privileges on launch. These aren't available to, for instance, applets. Is there any way to request elevation from a running application?
UAC is not something that a running process can request (doesn't matter what language you are running in). You have to request elevation at launch time.
The way that most windows apps handle this (and make it look like they are requesting elevation) is to spawn an extra copy of themselves, requesting elevation, and passing sufficient command line arguments to the new process so that the appropriate dialog can be displayed.
This is why UAC elevation in a dialog is always initiated by a button click that opens a new dialog.
So, in the Java world, you just have to do exactly what everyone else has to do: launch your app again, requesting elevation. There are several ways to launch elevated, the 'run as' verb probably being the easiest.
Looks like Sun will have to handle that kind of situation in the JRE since there's no other way of doing elevated actions than by running an external process.
If JRE supported it, JVM would probably have to run a separate, elevated process for the java code requesting the elevation.
For now however, only the manifest or running an external application are the only solutions available as far as I know.
The question is, what do you need elevation for?
You have to use an external (native) application to do this. This post provides source code and a great explanation:
UAC Prompt From Java
You could use jna and do a ShellExec. For lpOperation use "runas" (this is not documented). Since you likely need the full path to the (current) JavaVM which is stored in the registrylook at registry access, part of JNA.
Goto the folder where java is installed.
open the properties of javaw.exe / java.exe and set "run as administator" option true.

Categories

Resources