Get all java windows

Using Java, How can I get a list of all local users on a windows machine

How can I list all the local users configured on a windows machine (Win2000+) using java.
I would prefer doing this with ought using any java 2 com bridges, or any other third party library if possible.
Preferable some native method to Java.

4 Answers 4

Using a Java-COM Bridge , like Jacob. You then select an appropriate COM library, e.g. COM API for WMI to list local users, or any other Windows management information.

The Win32_SystemUsers association WMI class relates a computer system and a user account on that system.

The Win32_Account abstract WMI class contains information about user accounts and group accounts known to the computer system running Windows. User or group names recognized by a Windows NT domain are descendants (or members) of this class.

Working Example (jacob 1.17-M2, javaSE-1.6):

Using Java COM Object, i.e. Jacob:

There is a simpler solution for what I needed.
This implementation will use the «net user» command to get the list of all users on a machine. This command has some formatting which in my case I don’t care about, I only care if my user is in the list or not. If some one needs the actual user list, he can parse the output format of «net user» to extract the list without the junk headers and footers generated by «net use»

The method runProcessAndReturnOutput runs the process, collects the stdout and stderr of the process and returns it to the caller.

Windows: how to get a list of all visible windows?

(by all mean do re-tag with the relevant technology: I don’t know which ones they are 🙂

I’ll probably come later with more detailed questions, about specific details but for now I’m trying to grasp the «big picture»: I’m looking for a way to enumerate «real visible windows» on Windows. By «real visible window» I mean just that: what a user would call a «window». I need a way to get a list of all these visible windows, in Z-order.

Note that I do really need to do that. I’ve already done it on OS X (where it is a real headache to do, especially if you want to support OS X 10.4, because OS X doesn’t have convenient windows API) and now I need to do it under Windows.

Here’s an example, suppose there are three visible windows on the screen, like this:

Then I need to get back a list like this:

Then if the user (or the OS) brings the window A to the front, it becomes:

And I get (ideally) a callback giving me this:

Doing this under OS X requires the use of amazingly weird hacks (like mandating the user to turn on «Enable Access for assistive device»!) but I’ve done it under OS X and it works (under OS X, I didn’t manage to get a callback everytime some window changes occurs, so I’m polling, but I got it to work).

Now I want to do this under Windows (I really do, no question about it) and I’ve got a few questions:

can this be done?

are there well documented Windows APIs (and working as per their specs) allowing to do that?

is it easy to register a callback everytime a window changes? (if it is resized, moved, brought to back/front or if a new window pops-up, etc.)

Читайте также:  Acer veriton es2730g windows 10

what would the gotchas be?

I know this question is not specific, which is why I’ve tried to describe my problem as clearly as possible (including nice ASCII art for which you can upvote this): for now I’m looking at the «big picture». I want to know what doing such a thing involves under Windows.

Bonus question: imagine you’d need to write a tiny .exe writing the windows names/position/size to a temporary file everytime there’s a window change on screen, how long would such a program be approximately in your language of choice and how long would you need to write it?

(once again, I’m trying to get the «big picture» to understand what is at work here)

3 Answers 3

To enumerate the top-level windows, you should use EnumWindows rather than GetTopWindow/GetNextWindow, since EnumWindows returns a consistent view of the window state. You risk getting inconsistent information (such as reporting on deleted windows) or infinite loops using GetTopWindow/GetNextWindow, when windows change z-order during iteration.

The EnumWindows uses a callback. On each call of the callback you get a window handle. The screen co-ordinates of the window can be fetched by passing that handle to GetWindowRect. Your callback builds a list of the window positions in z-order.

You can use polling, and build the window list repeatedly. Or, you set up a CBTHook to receive notifications of window changes. Not all CBT notifications will result in changes to order,position or visibility of top level windows, so it’s wise to rerun EnmWindows to build a new list of window positions in z-order and compare this to the previous list before processing the list further, so that futher processing is done only when a real change has occurred.

Note that with hooking, you cannot mix 32- and 64-bit. If you are running a 32-bit app, then you will get notifications from 32-bit processes. Similarly for 64-bit. Thus, if you want to monitor the entire system on a 64-bit machine, it would seem that it’s necessary to run two apps. My reasoning comes from reading this:

SetWindowsHookEx can be used to inject a DLL into another process. A 32-bit DLL cannot be injected into a 64-bit process, and a 64-bit DLL cannot be injected into a 32-bit process. If an application requires the use of hooks in other processes, it is required that a 32-bit application call SetWindowsHookEx to inject a 32-bit DLL into 32-bit processes, and a 64-bit application call SetWindowsHookEx to inject a 64-bit DLL into 64-bit processes. The 32-bit and 64-bit DLLs must have different names. (From the SetWindowsHookEx api page.)

As you’re implementing this in Java, you might want to look at JNA — it makes writing access to native libraries much simpler (calling code in java) and removes the need for your own native JNI DLL.

EDIT: You asked how much code it is and how long to write. Here’s the code in java

I’ve made most of the related classes and interfaces inner classes to keep the example compact and pasteable for immediate compilation. In a real implementation, they would be regular top-level classes. The command line app prints out the visible windows and their position. I ran it on both 32-bit jvm and 64-bit, and got the same results for each.

EDIT2: Updated code to include z-order. It does use GetNextWindow. In a production application, you should probably call GetNextWindow twice for the next and previous values and check they are consistent and are valid window handles.

How to get a list of current open windows/process with Java?

Does any one know how do I get the current open windows or process of a local machine using Java?

Читайте также:  Dumping physical memory to disk windows синий экран

What I’m trying to do is: list the current open task, windows or process open, like in Windows Taskmanager, but using a multi-platform approach — using only Java if it’s possible.

14 Answers 14

This is another approach to parse the the process list from the command «ps -e«:

If you are using Windows, then you should change the line: «Process p = Runtime.getRun. » etc. (3rd line), for one that looks like this:

Hope the info helps!

Finally, with Java 9+ it is possible with ProcessHandle :

On Windows there is an alternative using JNA:

The only way I can think of doing it is by invoking a command line application that does the job for you and then screenscraping the output (like Linux’s ps and Window’s tasklist).

Unfortunately, that’ll mean you’ll have to write some parsing routines to read the data from both.

YAJSW (Yet Another Java Service Wrapper) looks like it has JNA-based implementations of its org.rzo.yajsw.os.TaskList interface for win32, linux, bsd and solaris and is under an LGPL license. I haven’t tried calling this code directly, but YAJSW works really well when I’ve used it in the past, so you shouldn’t have too many worries.

You can easily retrieve the list of running processes using jProcesses

There is no platform-neutral way of doing this. In the 1.6 release of Java, a «Desktop» class was added the allows portable ways of browsing, editing, mailing, opening, and printing URI’s. It is possible this class may someday be extended to support processes, but I doubt it.

If you are only curious in Java processes, you can use the java.lang.management api for getting thread/memory information on the JVM.

For windows I use following:

This might be useful for apps with a bundled JRE: I scan for the folder name that i’m running the application from: so if you’re application is executing from:

then you can find if it’s already running in J9, by:

Using code to parse ps aux for linux and tasklist for windows are your best options, until something more general comes along.

Linux can pipe the results of ps aux through grep too, which would make processing/searching quick and easy. I’m sure you can find something similar for windows too.

The below program will be compatible with Java 9+ version only.

To get the CurrentProcess information,

For all running processes,

We have to use process.getOutputStream.close() otherwise it will get locked in while loop.

TASKLIST /v /FI «STATUS eq running» /FO «CSV» /FI «Username eq LHPL002\soft» /FI «MEMUSAGE gt 10000» /FI «Windowtitle ne N/A» /NH

Getting the computer name in Java [duplicate]

I was wondering if there is a way to get the computer name in Java? I have seen several answers that feature java.net.InetAddress . However I was wondering if there is a way that does not use the network?

(As a side question, is the computer name only a network thing anyway, so therefore has to be done this way??)

3 Answers 3

The computer «name» is resolved from the IP address by the underlying DNS (Domain Name System) library of the OS. There’s no universal concept of a computer name across OSes, but DNS is generally available. If the computer name hasn’t been configured so DNS can resolve it, it isn’t available.

I agree with peterh’s answer, so for those of you who like to copy and paste instead of 60 more seconds of Googling:

I have tested this in Windows 7 and it works. If peterh was right the else if should take care of Mac and Linux. Maybe someone can test this? You could also implement Brian Roach’s answer inside the else if you wanted extra robustness.

Читайте также:  Как задать главный экран windows 10

I’m not so thrilled about the InetAddress.getLocalHost().getHostName() solution that you can find so many places on the Internet and indeed also here. That method will get you the hostname as seen from a network perspective. I can see two problems with this:

What if the host has multiple network interfaces ? The host may be known on the network by multiple names. The one returned by said method is indeterminate afaik.

What if the host is not connected to any network and has no network interfaces ?

All OS’es that I know of have the concept of naming a node/host irrespective of network. Sad that Java cannot return this in an easy way. This would be the environment variable COMPUTERNAME on all versions of Windows and the environment variable HOSTNAME on Unix/Linux/MacOS (or alternatively the output from host command hostname if the HOSTNAME environment variable is not available as is the case in old shells like Bourne and Korn).

I would write a method that would retrieve (depending on OS) those OS vars and only as a last resort use the InetAddress.getLocalHost().getHostName() method. But that’s just me.

UPDATE (Unices)

As others have pointed out the HOSTNAME environment variable is typically not available to a Java application on Unix/Linux as it is not exported by default. Hence not a reliable method unless you are in control of the clients. This really sucks. Why isn’t there a standard property with this information?

Alas, as far as I can see the only reliable way on Unix/Linux would be to make a JNI call to gethostname() or to use Runtime.exec() to capture the output from the hostname command. I don’t particularly like any of these ideas but if anyone has a better idea I’m all ears. (update: I recently came across gethostname4j which seems to be the answer to my prayers).

Long read

I’ve created a long explanation in another answer on another post. In particular you may want to read it because it attempts to establish some terminology, gives concrete examples of when the InetAddress.getLocalHost().getHostName() solution will fail, and points to the only safe solution that I know of currently, namely gethostname4j.

It’s sad that Java doesn’t provide a method for obtaining the computername. Vote for JDK-8169296 if you are able to.

How to get the Desktop path in java

I think this will work only on an English language Windows installation:

How can I make this work for non English Windows?

8 Answers 8

I use a french version of Windows and with it the instruction:

works fine for me.

I think this is the same question. but I’m not sure!:

Reading it I would expect that solution to return the user.home, but apparently not, and the link in the answer comments back that up. Haven’t tried it myself.

I guess by using JFileChooser the solution will require a non-headless JVM, but you are probably running one of them.

This is for Windows only. Launch REG.EXE and capture its output :

Seems not that easy.

But you could try to find an anwser browsing the code of some open-source projects, e.g. on Koders. I guess all the solutions boil down to checking the HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders\Desktop path in the Windows registry. And probably are Windows-specific.

If you need a more general solution I would try to find an open-source application you know is working properly on different platforms and puts some icons on the user’s Desktop.

there are 2 things.

  1. you are using the wrong slash. for windows it’s \ not / .
  2. i’m using RandomAccesFile and File to manage fles and folders, and it requires double slash ( \\ ) to separate the folders name.
Оцените статью