IntelliJ idea: Show process id - java

I'm starting a java program from IntelliJ Idea which uses a .dll which is written by me in C++. After the startup of the application I can attach to the process using Microsoft Visual Studio (Debug / Attach to Process...) which allows me to debug the C++ part of the running application.
The name of the process is simply java. It is always a pain to select the right one from all the java processes. The simple Task Manager is not sufficient. The Process Explorer is good, but I still need to inspect multiple processes until I find the right one. It would be much easier for me if Idea would just tell the PID of the application it started.
Does Idea have such a feature?
(Win 7 64bit fresh # 2017-04-19, Idea 2017.1.1)

You can list all java processes with the jps utility (Can be executed from Idea terminal window) which makes it extremely easy to identify your process. Example scenario:
D:\projects\git\CENSORED>jps
12084
5476 WorkerMain
8772 WebSocketProxyMain
9444 Launcher
12920 RemoteMavenServer
13400 Bootstrap
7752 Jps

If you have a profiling application like "JProfiler" you can also very easily find the pid in the "quick attach" dialog.

Related

java.exe is faster than javaw.exe

I've made several java software's using hibernate + Mysql/Sqlite and I have noticed a significant difference in fetch time (from database) when using both launchers.
When I start my application using java.exe, of course a console is displayed but the application is faster than when I use javaw.exe .
My application is a windows based application, and I don't want java.exe to load the console when the application starts, so I've been using javaw.exe, but it lacks in fetch time.
What's the explanation of that issue? And how can I either start my application using java.exe without starting a console, Or start it using javaw.exe and have the same performances?
Thanks in advance.
Found the problem using VisualVM (profiler).
Found out that there was logging instructions which were taking much time.
Removed all logs as below:
LogManager.getLogManager().getLogger("").setLevel(Level.OFF);
Thanks to fl0w.

Find and kill a specific Java process from another Java App

I have several java processes running on a windows machine. I have a Java process which is supposed to monitor the other processes and periodically kill or restart new ones.
If I have a java process running com.foo.Main1 and one running com.foo.Main2 - how can my monitoring process find and kill just the Main2 process?
Update: I have some code that can execute a command line tasklist.exe and parse it, but no matter what I do, I only see the java.exe process, not which class is executing
Update 2: I do not have the ability to install non-java programs.
It's probably going to be a lot simpler using OS-specific tools and using Runtime.exec() to run them, but I'll try and give a platform independent answer:
It might be possible to do this platform independently using the Attach API. This comes with the JDK, so to use it just include tools.jar from your JDK on your program's classpath.
To get a list of virtual machines on the system, use VirtualMachine.list(). You can get/parse arguments from the virtual machine descriptor objects that are returned from this.
The attach API also allows you to load agents into already-running Java processes. Since you want to kill a Java process, you can write a Java agent that simply runs System.exit() (or if you really want it dead use Runtime.halt() instead) when the agent loads.
Once you identify the one you want to kill, attach to it and load the killer agent (the agent has to be built as a JAR file, accessible to the Java process it needs to be loaded into). Shortly after the agent is attached that process should die.
These links might help also:
An Oracle blog on the attach API
Package documentation for java.lang.instrument (has detailed instructions on how to build an agent JAR)
This is specific to Windows.
I was facing the same issue where I have to kill the specific java program using taskkill. When I run the java program, tasklist was showing the same program with Image name set as java.exe. But killing it using taskkill /F java.exe will stop all other java applications other than intended one which is not required.
So I run the same java program using:
start "MyProgramName" java java-program..
Here start command will open a new window and run the java program with window's title set to MyProgramName.
Now to kil this java-program use the following taskkill command:
taskkill /fi "MyProgramName"
Your Java program will be killed only. Rest will be unaffected.

windows discriminate java processids

I'm developing a web application with Eclipse and Tomcat on windows. When testing my efforts I sometimes crash Tomcat, and the only option left is to kill the jvm hosting Tomcat, but that can only be done with windows' task manager.
The process to kill is a java process but eclipse is also on a java process and basically the only thing I can do determine which java process to kill is toss a coin and hope for the best. It seems that I choose the wrong (eclipse) java process more often than the tomcat java process. Of course I can and should write down the id of the only java before starting Tomcat but that is sometimes forgotten
Is there a way to determine which java process is for eclipse and which for Tomcat? when eclipse is up an running for a long time I can discriminate on the cpu time, but for short running instances this is no candidate for heurstics.
I use Process Explorer which is free and I can easily see in its GUI (in process tree view) that eclipse is a super node of Tomcat's JVM.
Use the Process Explorer from Sysinternals. It shows the hirarchy of processes, and since the Tomcat got started by Eclipse, you can see it as a “subprocess”.
You could start jvisualvm from the bin of your JDK directory. There each MainClass is listed with the corresponding pid.

The shutdown hook isn't executed when the application is launched using javaw.exe

If I'm using javaw.exe to launch a Java application, the shutdown hook isn't executed when users log off from their Windows account. The application is actually launched using a launch4j generated .exe file but I know it uses javaw.exe to start it.
This seems to be a known bug:
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4302814
Has anyone else had this problem? If so, how did you fix it?
Thanks!
The application is actually launched using a launch4j generated .exe file but I know it uses javaw.exe to start it.
I haven't used launch4j in a while, but I'd be fairly suprised if it directly did this. Most of these apps start the jvm themselves, rather than shelling out to javaw.
The only difference between java.exe and javaw.exe is that java.exe automatically attaches a console. The bug that you reference (and the others referenced by it) were closed as Sun doesn't appear to be interested in making the handling of Windows logoff events better. From what I can tell, it only works with java.exe, because the console itself handles the WM_MESSAGES in such a way that it slows down the shutdown process.
It appears that the only easy workaround at the moment is to change the "headerType" in your launch4j config to "console". Obviously, this brings with it an ugly console.
I think the other alternative would be to use some sort of native wrapper of your on that handles windows messages in a cleaner way.

How can I give my Java application a unique process name?

I've noticed that when I start Netbeans it shows up in the task manager as netbeans.exe as all my own Java applications show up as java.exe or javaw.exe.
How can I change that so my process names shows up as myapp.exe?
The process name is the name of the JVM. So if you rename the jvm you have an other process name. There are some tools which can do that for you. For example Launch4J
IMO the best option is to choose one of the many open source launchers. They all provide a nicer deployment container than java.exe and a batch file.
I've compiled a list of them after a brief search on google (in no particular order and may not be exhaustive):
NSIS
Janel (dead link)
JSmooth
Launch4J
WinRun4J
(full disclosure: i work on winrun4j)
Not easily. The easiest way (but not nice!) would be to simply copy the java.exe (only 68k on my system, so perhaps practical!)
If you're worried about identifying which java process is which (e.g. is one consuming memory/CPU etc.), use the standard tool jps to identify the Java processes
Netbeans and Eclipse both ship with an .exe file that in turns launches a JVM. The exe itaself probably does nothing after launching the VM. You see the NetBeans javaw.exe in the Task Manager also, I suspect.
So you'll need to write a native exe (using some windows tool) that does a similar thing.
Just answered this a second ago here: Get JVM to grow memory demand as needed up to size of VM limit?
It's actually a lot easier than folks are saying (but you do have to have a c/c++ compiler handy).
There are mainly 2 approaches: one is as already described: using tools like Launch4j, WinRun4J to create native Windows launchers.
Another approach that seems better is to use Apache Procrun to wrap the java application as a Windows service. During the install service process, we can give the process an meaningful name such as OurApp.exe.
All we need do is rename prunsrv.exe to OurApp.exe and replace every occurrence of prunsrv.exe in our install|start|stop|uninstall service scripts to MyApp.exe.
See more from Using Apache Procrun to Rename Process Name of a Java Program in Windows

Categories

Resources