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
Related
I am trying to run a jar file from windows command line like this:
java -jar myjar.jar
and i get the following error msg
-jar: illegal argument
usage: java [-options] class
where options include:
-help print out this message
-version print out the build version
-v -verbose turn on verbose mode
-debug enable remote JAVA debugging
-noasyncgc don't allow asynchronous garbage collection
-verbosegc print a message when garbage collection occurs
-noclassgc disable class garbage collection
-ss<number> set the maximum native stack size for any thread
-oss<number> set the maximum Java stack size for any thread
-ms<number> set the initial Java heap size
-mx<number> set the maximum Java heap size
-classpath <directories separated by semicolons>
list directories in which to look for classes
-prof[:<file>] output profiling data to .\java.prof or .\<file>
-verify verify all classes when read in
-verifyremote verify classes read in over the network [default]
-noverify do not verify any class
-nojit disable JIT compiler
anybody knows why that might be?
If you're using Windows Command Line, you can use javaw youJarFile.jar
Why do we need to prefix JVM arguments with -D e.g. when running a jar from the command line? E.g.
java -jar -DmyProp="Hello World" myProgram.jar
is used to run myProgram.jar with the system parameter myProp. So why the leading -D? Why couldn't the architects of Java let us simply do:
java -jar -myProp="Hello World" myProgram.jar
I'm hoping for an answer beyond just "Because that's the way it is".
Bonus Question: Why the letter -D as opposed to any other letter, does it stand for anything?
Note: This question asks why there was a need to use "D", or any other letter for that matter, in the first place. It is less concerned with the choice of specific letter "D" over any other letter, though that is asked as a bonus question.
The bonus question has an answer here: In java -D what does the D stand for?.
Why couldn't the architects of Java let us simply do:
java -jar -myProp="Hello World" myProgram.jar
It could work today but suppose that in next Java versions a -myProp argument is introduced as a JVM option.
How to distinguish your -myProp from the -myProp JVM option ? No way.
So it exists an obvious reason to use -D to define system properties.
As other example, instead of -myProp suppose you program relies on a -client system property.
It will not run :
java -jar -client="davidxxx" myProgram.jar
You would have a JVM error such as :
Unrecognized option: -client=davidxxx
as -client is a JVM standard option that expects no value.
But if you use -D-client, it is now fine as here -Dclient is defined as a system property that is distinct from the -client standard JVM option :
java -jar -D-client="davidxxx" myProgram.jar
Or by using both :
java -jar -client -D-client="davidxxx" myProgram.jar
To go further, not all JVM arguments start with -D. but most of them have a prefix (-D, -X, -XX) that allows in a someway to define namespaces.
You have distinct categories of JVM arguments :
1. Standard Options (-D but not only).
These are the most commonly used options that are supported by all implementations of the JVM.
You use -D to specify System properties but most of them don't have any prefix :-verbose, -showversion, and so for...
2. Non-Standard Options (prefixed with -X)
These options are general purpose options that are specific to the Java HotSpot Virtual Machine.
For example : -Xmssize, -Xmxsize
3. Advanced Runtime Options (prefixed with -XX)
These options control the runtime behavior of the Java HotSpot VM.
4. Advanced JIT Compiler Options (prefixed with -XX)
These options control the dynamic just-in-time (JIT) compilation performed by the Java HotSpot VM.
5. Advanced Serviceability Options (prefixed with -XX)
These options provide the ability to gather system information and perform extensive debugging.
6. Advanced Garbage Collection Options (prefixed with -XX)
These options control how garbage collection (GC) is performed by the Java HotSpot VM.
"Define". The meaning is similar to a preprocessor definition in C. The -D signifies that the definition is in the context of the application, and not in the Java interpreter context like any other option before the executable name.
The usage of the letter "D" isn't specifically explained in the documentation, but the only use is to "define" a key in the system properties map - except for this reference:
The System class maintains a Properties object that defines the configuration of the current working environment. For more about these properties, see System Properties. The remainder of this section explains how to use properties to manage application configuration.
If you do not specify anything like -myProp="XYZ" it means it is passed as an argument to main method of the program.
-D means you can use this value using System.getProperty
-X is used for extension arguments like -Xdebug -Xnoagent -Djava.compiler=NONE -Xrunjdwp:transport=dt_socket,server=y,suspend=y,address=8000
Yes, they could have interchanged.. the characters; but these characters are used to specify what type of parameter is passed and who is the consumer.
Without the -D the properties would conflict with normal JVM options. For example how would you set the property jar?
The -D was probably chosen (I can only speculate about that) because it is also used in the C preprocessor to define symbols and was therefore familiar to most people.
From Java, is it possible to get the complete commandline with all arguments that started the application?
System.getEnv() and System.getProperties() do not appear to contain the values.
Some of it is available from the RuntimeMXBean, obtained by calling ManagementFactory.getRuntimeMXBean()
You can then, for example call getInputArguments()
The javadocs for which say:
Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method. This method returns an empty list if there is no input argument to the Java virtual machine.
Some Java virtual machine implementations may take input arguments from multiple different sources: for examples, arguments passed from the application that launches the Java virtual machine such as the 'java' command, environment variables, configuration files, etc.
Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. Thus, the returned input arguments may not include all command-line options.
In Linux that should be possible when you get the output of that command (run in a shell)
cat /proc/$PPID/cmdline
But that is not portable at all and should therefore not be used in Java...
The following links may help you get there:
How to get command line arguments for a running process
get command-line of running processes
How to get a list of current open windows/process with Java?
Just as a note:
In Windows you have Process Explorer by Sysinternals that shows you the command line used to open the process. Right click the process and select Properties... You'll see Command Line in the window that is opened.
You might want to look into how jps does this. It's a Java program that is able to get the full command line for all Java processes, including full class name of main class and JVM options.
There is a environment variable %~dp0 which returns the complete path
Have a look at YAJSW (Yet Another Java Service Wrapper) - it has JNA-based implementations for various OSes (including win32 and linux) that do exactly this so it can grab the commandline for a running process and create a config that wraps it in a service. A bit more info here.
Since Java 9 you may use ProcessHandle to get the command line of the process:
ProcessHandle.current().info().commandLine()
One option I've used in the past to maintain the cross-platform-shine is to set the command line as an environment variable prior to issuing the command.
If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.
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.
From Java, is it possible to get the complete commandline with all arguments that started the application?
System.getEnv() and System.getProperties() do not appear to contain the values.
Some of it is available from the RuntimeMXBean, obtained by calling ManagementFactory.getRuntimeMXBean()
You can then, for example call getInputArguments()
The javadocs for which say:
Returns the input arguments passed to the Java virtual machine which does not include the arguments to the main method. This method returns an empty list if there is no input argument to the Java virtual machine.
Some Java virtual machine implementations may take input arguments from multiple different sources: for examples, arguments passed from the application that launches the Java virtual machine such as the 'java' command, environment variables, configuration files, etc.
Typically, not all command-line options to the 'java' command are passed to the Java virtual machine. Thus, the returned input arguments may not include all command-line options.
In Linux that should be possible when you get the output of that command (run in a shell)
cat /proc/$PPID/cmdline
But that is not portable at all and should therefore not be used in Java...
The following links may help you get there:
How to get command line arguments for a running process
get command-line of running processes
How to get a list of current open windows/process with Java?
Just as a note:
In Windows you have Process Explorer by Sysinternals that shows you the command line used to open the process. Right click the process and select Properties... You'll see Command Line in the window that is opened.
You might want to look into how jps does this. It's a Java program that is able to get the full command line for all Java processes, including full class name of main class and JVM options.
There is a environment variable %~dp0 which returns the complete path
Have a look at YAJSW (Yet Another Java Service Wrapper) - it has JNA-based implementations for various OSes (including win32 and linux) that do exactly this so it can grab the commandline for a running process and create a config that wraps it in a service. A bit more info here.
Since Java 9 you may use ProcessHandle to get the command line of the process:
ProcessHandle.current().info().commandLine()
One option I've used in the past to maintain the cross-platform-shine is to set the command line as an environment variable prior to issuing the command.
If you are using solaris as the OS, take a look at "pargs" utility. Prints all the info required.