how to write java program get pid [duplicate] - java

This question already has answers here:
Closed 13 years ago.
how to write java program get pid?

How a Java Application Can Discover its Process ID (PID)
Not very straightforward, but apparently there is no "official" way.

You can do this using JMX, but beware. The below is not an officially supported mechanism and could change. However, I've used this in the past and it works fine.
RuntimeMXBean rmxb = ManagementFactory.getRuntimeMXBean();
System.err.println("pid: " + rmxb.getName());
will print {pid}#hostname

I don't believe this is something Java supplies. For a start, it breaks the platform-independent nature. I can see two ways to approach it, both assuming you're running under a UNIX-type system.
provide a JNI which will call getpid() and return it.
use system or one of the Runtime methods to run an external program which will call getppid() to get the PID of its parent (i.e., Java), or worse case, walk up the process tree until you find Java itself.

Related

What exactly is the java executable, what does it do and where can I find the sources for it? [duplicate]

This question already has answers here:
Totally Confused with java.exe
(3 answers)
How is JNI_CreateJavaVM invoked when running a java app from the command line
(1 answer)
Closed 1 year ago.
My question is regarding the java executable, the one that you use to run a Java program and that on Linux it is found for example in /usr/bin/java.
I have been experimenting a bit as I want to look into how everything happens behind the scenes, like how does the bytecode gets loaded, how is the execution of the actual Java program starting and other details that may not be so straightforward.
Until now I have looked at the execution with strace and found that a new thread is created and that thread is the one on which the Java program actually gets executed (from another question that I posted). From what I understand the java executable is a launcher of some sort, but I do not understand all the operations that happen behind the scenes.
So, what exactly is the java executable and is there any place where I can find the source code for it (this would really help me)?
The primary source file for the launcher in the current development JDK can be found here: https://github.com/openjdk/jdk/blob/master/src/java.base/share/native/libjli/java.c
As you see, it's quite short and delegates most of the work to other pieces of code, but should be useful as a starting point.
If you want to see the source for other JDK versions (this is basically the main development repo for future Java versions), you need to look into the appropriate repository.

Using the Razer Chroma SDK from Java [duplicate]

This question already has answers here:
Is it possible to run C source code from Java?
(4 answers)
Closed 7 years ago.
I want to make a Java app that uses the Razer Chroma SDK, but the Chroma SDK is in c++
Is there a way I can run c++ code from Java?
I must use Java for what I want to make.
I have almost no experience in c++, but I understand enough to get doing what I need.
EDIT:
This question is slightly different then others, because it is about a specific SDK, not about general c++ libraries. For this library I was able to use a simpler approach then learning to use things like JNI
You probably want to look into using JNI
The easiest way I found, and then one that doesn't require learning stuff like JNI:
Create a console application with commands that fire off what you need
In Java, launch the console application and redirect it's input and output streams so you can send it commands, and log it's output (see 12013910)
Have a command in the console application that you can pass a PID, and have the console application watch for when it closes, and then it will close itself. (This fixes having the console application not being closed if the Java application crashes and doesn't call the closing method(s))

How to simulate File::Stat.ino in jruby or java?

I am currently trying to have Logstash work on Solaris with the File Input method. But I am encountering some bugs (see LOGSTASH-665). After digging a lot, it appears that native support for File.stat on my system (SunOS 5.10, JDK 1.6.0_21, 32 bit) is totally deficient, so I am looking for a way to properly handle it.
Specifically I want to access the inode information. Based on the metadata I can gather about the file (like its path and whatever is available on solaris), I want to calculate a number which is unique for that file, and which changes when the file is replaced by another file which has the same name. At first I thought about simply using a hash of the file path and used this function as a replacement, but indeed, when the file is rolled over the number does not change, so I need to also access the ctime information...
..Or make a system call to get the ls -li result to get the real inode number by another way.
Problem is that I never used ruby before, I am more used to java, so I am struggling to find a solution. Every suggestion will be appreciated.
The best solution I know of is to wrap the native call using JNI or JNA.
There do appear to be a couple of projects that have done this, although I haven't used either of them. See this question: Is there a Java library of Unix functions?

Input output communication between two programs

I have a third party java program called kgsgtp.jar which need to communicate with my own C++ (but mainly just C) program. The documentation for the java program states:
=====================
You just need to make sure that stdin for kgsGtp it connected to
the engine's output and stdout for kgsGtp is connected to the engine's
input. Usually, the easiest way to do this is by forking and execing
kgsGtp from within your engine.
=====================
Now I am a reasonably competent programmer and feel that I could probably arrange all that, given just a few more clues. I suspect that if the description was expanded to erm, 10? lines instead of three and a half then I'd have it sorted in no time.
I'm guessing that what the document means by forking, is using WinExec() or CreateProcess() in my program to execute the java program? I'm also guessing that perhaps when I use the right function, then the fact of one program's stdin corresponding to the other's stdout will happen automatically?
That description is for unixes, where a sequence of pipe(),dup2(), fork()/exec() calls would be use to do this.
Take a look at the code snippet in the answer from denis here: How do I get console output in C++ with a Windows program? , should get you started.
Edit: more complete example is here: http://support.microsoft.com/kb/190351
What you need is equivalent of POSIX dup() on windows may be this

How can I set the process name for a Java-program? [duplicate]

This question already has answers here:
how to change the name of a Java application process?
(10 answers)
Closed 8 years ago.
If a Java program is started, it get's in the system process-monitor the name java. Many Java-programs are that way hard to distinguish. So it would be nice, if a way exists, to set the name, that will be shown in the process-monitor. I'm aware that this may work different on different Operating Systems.
A simple way would be, if the java-interpreter would support a switch to set the name, like this:
java -processname MyProgram -jar MyProgram
But I couldn't find such a switch, so it is probably non-existant. An API in Java to set the process-name would be also fine.
So, so you have any suggestions?
I don't know if this is possible, but you could use a command line tool that comes with the JDK called 'jps'. It's like *nix ps, but just Java programs instead. jps -v shows all the arguments you have passed to java.
Also, I have seen people attach a "process name" to their java processes by adding an unused -Dmyprocessname to the args.
as #omerkudat said:
jps -v
prints out all java processes {processID, params list}
If the params list is not enough to recognize the applications you need,
try adding some dummy params when running them:
java -Dname=myApp -cp myApp.jar some.client.main.MainFrame
This will print like:
7780 MainFrame -Dname=myApp
and you can use the process ID to kill / monitor it.
You can do this with an LD_PRELOAD shim: https://github.com/airlift/procname
The shim simply calls the Linux-specific prctl() when the process starts:
static void __attribute__ ((constructor)) procname_init()
{
prctl(PR_SET_NAME, "myname");
}
The call has to happen on the main thread, so it isn't possible to do this from Java or even with a JVMTI agent, since those happen on a different thread.
When I first read this, the idea of changing the process name struck me as impossible. However, according to this ancient thread on the sun forum you can use C++ wrappers around the JVM executable to achieve this.
Though frankly, I wonder what your real problem is, as I'd guess there is a more standard solution then attempting to change the process name.
Your best option is something like launch4j
http://launch4j.sourceforge.net/
There is a bug logged in the sun bugtracker for this, but it's not high priority
http://bugs.sun.com/view_bug.do?bug_id=6299778
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
If you want to use a different process name you'll have to create your own binary to launch your Java application using something like JSmooth.
Look at this question for a discussion of creating such binaries.
That's because Java applications aren't actually executable they're ran by the Java virtual machine which is why java appears in the process monitor, it's the host of your application.
Things like LimeWire however do but I think that's more down to GCJ - http://gcc.gnu.org/java/

Categories