Basically, given a jvm (the process named Java (TM) Platform SE binary) process id, can i find the underlying .jar file that the jvm is running? Can I do this from other languages (non-java)? I know that the jps tool in the JDK can list the e.g. the starting arguments for the jvm process, but it does not list down the .jar file path, most times it just lists down the package name.
given a jvm process id, can i find the underlying .jar file that the
jvm is running?
Yes. Use jcmd utility:
jcmd <PID> VM.command_line
Can I do this from other languages?
Yes. See jattach for the C implementation.
Related
I have written a compiler in java which can output llvm-ir. Now I would like to actually run the compiler somehow. I've read about GraalVM and how it should be possible to run other languages which use LLVM inside of java, however, I couldn't find out how to run llvm-ir itself with it. I don't have to use GraalVM though, any suggested approach would be fine as long as it enables me to somehow execute that code.
Basically, you need to get yourself a GraalVM, and you can execute your LLVM bitcode file with
lli [LLI options] [GraalVM options] [polyglot options] <bitcode file> [program args]
as per
https://www.graalvm.org/reference-manual/llvm/
The lli launcher lives in the bin folder of the graalvm distribution.
Regarding the format of the bitcode file the docs on https://www.graalvm.org/reference-manual/llvm/Compiling/ say
While the
GraalVM LLVM runtime can execute plain bitcode files, the preferred
format is a native executable with embedded bitcode. The executable
file formats differ on Linux and macOS. Linux by default uses ELF
files. The bitcode is stored in a section called .llvmbc. The macOS
platform uses Mach-O files. The bitcode is in the __bundle section of
the __LLVM segment.
I really can't figure out how to use jsp -Joption. I got description as followed,
OPTIONS
The jps command supports a number of options that modify the output of the command. These options are subject to change or removal in the
future.
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of
local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application's main class or the full path name to the application's JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the
-XX:Flags=<filename> argument).
-Joption Pass option to the java launcher called by javac. For example, -J-Xms48m sets the startup memory to 48 megabytes. It is a
common convention for -J to pass options to the underlying VM executing applications written in Java.
Actually, I don't know what is a java launcher called by javac, and when I run the example jps -J-Xms48m just as same using jps. So, what this option for? Thanks.
Java development tools like jps, jstat, jstack, jmap etc. are all written in Java. Just like regular Java programs they require Java Runtime Environment, i.e. they run under JVM.
-J options do not affect jps tool directly, but they rather affect the JVM which runs this tool. E.g. -J-Xms48M option means that jps will launch Java Virtual Machine with the initial heap size of 48 Megabytes.
For example, compare jps -J-XX:+PrintGCDetails and jps -J-Xms48M -J-XX:+PrintGCDetails
Let's assume that I have JDK/JRE 8 also I've added JAVA_OPTS= -Xmx8G -Xms1G.
I've stated without any other params (java -jar 1.jar; java -jar 2.jar) 2 simple jars. So now I have two java process.
1) Do they executed in one JVM (as two java process) or each process will be executed in separated JVM?
2) -Xmx8G -Xms1G heap size will be applied to separately each of two executed jars or this heap size will be something common for two executed jars?
3) What will be with memory allocation if start one jar ( java -jar 1.jar)
and another with java -jar 2.jar Xmx12G?
P.S. Would we nice if you have any good links.
P.S.S Don't Ask what I'm smoking :)
The best way is to analyze JVM instances in your system. Just open JMC(Java Mission Control). Whenever you execute any application it will show as an individual jvm there. ex : if you run jar1 and jar2 the JMC will show three JVMs- jar1, jar2 ,jmc. Then, you can use use MBean Browser there to analyze your all jvm.
Hope it helps.
I have a issue where a jar file runs on one machine , but not on another machine -
asked on stackoverflow.
It seems some dependencies are not found on the second computer. My question is, is there a way to know from where the libraries are loaded (as in - to simply System.out.println("from which directory the dependent jar is linked")) ?
This will output a lot of information, but it includes the location of all jar files:
You can start Java with the -verbose:class option to debug classloader issues.
You maybe could use jcmd (part of the JDK) and query the JVM which class path it was using at its startup.
jcmd ${pid_of_the_JVM} VM.system_properties | grep path
in the output have a look for the properties
java.class.path
sun.boot.class.path
jps.exe which found on JDK 1.5 and later could monitor all Java process but is there a way to detect the specify command line and terminate the correct pid?
What if the user have JRE, is there a similar code allow us to terminate any process easily?
Prefer to keep the topic on Windows which I am working on.
The jps command supports a number of options that modify the output of the command. These options are subject to change or removal in the future.
-q Suppress the output of the class name, JAR file name, and arguments passed to the main method, producing only a list of local VM identifiers.
-m Output the arguments passed to the main method. The output may be null for embedded JVMs.
-l Output the full package name for the application's main class or the full path name to the application's JAR file.
-v Output the arguments passed to the JVM.
-V Output the arguments passed to the JVM through the flags file (the .hotspotrc file or the file specified by the -XX:Flags= argument).
Pipe the output of jps to grep or sed or awk or perl or even another Java program for further matching, parsing and action. On Windows, the easiest way to get those utilities is through Cygwin.
Here are some Microsoft downloadable command line utilities which are useful for working with processes on Windows:
pskill
pslist
and the rest of the Sysinternals Suite
If the user don't have jps, you can use ps. The command line options for ps differs between platforms, see man ps on you system. I use ps -C java -o pid,time,cmd to list java processes on a CentOS system. Then kill to terminate.