Windows get pid of java process


















This is admittedly a hack, but it might help. What do you need the PID for anyway? This is an OS handle for the process. That page has details on how to do that. I think I have found out a solution, that looks quite bulletproof while working on most platforms. Here is the idea:. Since you check only for child processes, you cannot be wronged by some other process in the same machine. JVM-wide mutex than allows you to be sure, that the new process is the correct one.

Reading child process list is simpler than getting PID from process objects, because it does not require WIN API calls on windows, and, more importantly, it has been done already in several libs. Below is an implementation of the above idea using JavaSysMon library. If your UNIX implementation is like mine, you will see an implementation class that has a property named pid that contains the PID of the process.

Here is the logging code that I used. The code below works for me on my flavour of UNIX. However: Some programs, especially services and long-running programs, create or offer to create, optionally a "pid file". There isn't a simple solution. The way I've done it in the past is to start another process to run either the ps command on Unix-like systems, or the tasklist command on Windows, and then parse the output of that command for the PID I want. In reality, I ended up putting that code into a separate shell script for each platform which just returned the PID, so that I could keep the Java piece as platform independent as possible.

This doesn't work well for short-lived tasks, but that wasn't an issue for me. It is part of the java native runtime used by jruby and can be considered a prototype for a future java-FFI. I believe the only portable way to do this, is to run a child process through another parent Java process, which will inform me the actual PID of the parent process.

The child process could be anything. If portability is not a concern, and you just want to get the pid on Windows without a lot of hassle while using code that is tested and known to work on all modern versions of Windows, you can use kohsuke's winp library.

It is also available on Maven Central for easy consumption. It may be overkill just to get the PID, but if you want other things like CPU and thread id, and specifically thread affinity, it may be adequate for you. To get the current thread's PID, just call Affinity. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Learn more. Active 7 days ago. Viewed k times. How do I get the id of my Java process? I know there are several platform-dependent hacks, but I would prefer a more generic solution.

Improve this question. Dmitry Shechtman Dmitry Shechtman 6, 5 5 gold badges 24 24 silver badges 25 25 bronze badges. This is meant to be fixed in JDK9. Way to do it in Java SE10 — mrsrinivas. Add a comment. Active Oldest Votes. Beware though that according to the docs , there are no guarantees about this value: Returns the name representing the running Java virtual machine.

Improve this answer. Wouter Coekaerts Wouter Coekaerts 8, 3 3 gold badges 28 28 silver badges 34 34 bronze badges. This solution is really fragile. See an answer about Hyperic Sigar below. Luke Quinane Luke Quinane However, I get error: cannot find symbol for jdk9 — skytree. Here's a backdoor method which might not work with all VMs but should work on both linux and windows original example here : java. VMManagement jvm.

Brad Mace Brad Mace Nice workaround. I'm going to assume there is a good reason why this method and others in the class aren't public and easily accessible, and I'm curious to know what it is.

Oracle Java team have announced that they intend to hide all non-java packages i. If you have a similar implementation as the one above, you may want to figure out an alternative so that your code don't break. Doesn't work for me either as the sun. Due to the way reflection works, access to sun.

Just perform the getDeclaredMethod on the Object returned by jvm. Miss Chanandler Bong 3, 10 10 gold badges 24 24 silver badges 35 35 bronze badges. Ashwin Jayaprakash Ashwin Jayaprakash 2, 24 24 silver badges 28 28 bronze badges. Link is broken. You give an example of how to use this, but is there a maven dependency for it? Martin Martin 1, 1 1 gold badge 19 19 silver badges 26 26 bronze badges. Jvm ApplicationSupport createCurrentApplication. They are the experts, so it looks like dependable, cross platform solution.

This mean they can make assumptions accordingly. Notify me of new posts via email. Skip to content. Home About. I will describe three possible ways in the following: 1. The getName method of this class is described as: Returns the name representing the running Java virtual machine. Like this: Like Loading Tagged how-to Java pid process tutorial unix. Published by Max Rohde. Right click on the task bar and select Task Manager.

Under performance tab, you will see the uptime information. At times you will want to find out which domain groups you belong to. For example, if an Application is using Single Sign on with Active directory, you will need to know the group information to diagnose authentication issues. The output actually has several useful bits of information. You will want to scroll down and look at Local Group Memberships applies only to this machine and Global Group Memberships applies in the entire network.

If you application behaves weirdly and you are hunting for clues, one of the places you will look is to make sure the OS has all patches necessary by the way, if you are at this stage, you are really dealing with a nasty problem.

Nevertheless you must be able to determine the patches installed. Open a command prompt and just execute systeminfo command. Among the wealth of information this command outputs such as system model, memory and processor information , it shows the Hotfixes installed. However for larger directories say several giga bytes , especially If you are copying across network, you will need a more robust copy command.

Say hello to robocopy. This command is dangerous as it can delete files in the destination in order to make it look identical to source. Note that some applications behave erroneously when the file system it runs on fills up. And on top of that, you may lose valuable logging information as the application cannot write any log due to the space issue. One shortcut to find huge files like core dumps , is to use Windows Search feature.

Open Windows Explorer and navigate to the directory you think is the root cause. In the search window, type size:gigantic. In order to find the folders that are huge, the best way is to use the free tool space sniffer.



0コメント

  • 1000 / 1000