In a shell script, I have set the JAVA_OPTS environment variable (to enable remote debugging and increase memory), and then I execute the jar file as follows:
export JAVA_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=8001,server=y,suspend=n -Xms512m -Xmx512m"
java -jar analyse.jar $*
But it seems there is no effect of the JAVA_OPTS env variable as I cannot connect to remote-debugging and I see no change in memory for the JVM.
What could be the problem?
PS: I cannot use those settings in the java -jar analyse.jar $* command because I process command line arguments in the application.
You can setup _JAVA_OPTIONS instead of JAVA_OPTS. This should work without $_JAVA_OPTIONS.
I don't know of any JVM that actually checks the JAVA_OPTS environment variable. Usually this is used in scripts which launch the JVM and they usually just add it to the java command-line.
The key thing to understand here is that arguments to java that come before the -jar analyse.jar bit will only affect the JVM and won't be passed along to your program. So, modifying the java line in your script to:
java $JAVA_OPTS -jar analyse.jar $*
Should "just work".
In the past 12 years some changes were made.
Environment variable JAVA_OPTS was and is NOT a standardized option. It is evaluated by some shell script wrappers for Java based tools, an example of how this works is in the answer from ZoogieZork.
The environment variable _JAVA_OPTIONS mentioned by HEX is nowadays deprecated/undocumented.
Starting with Java 9, the recommended way to do what you wanted is the variable JDK_JAVA_OPTIONS, see Using the JDK_JAVA_OPTIONS Launcher Environment Variable in the Oracle Java 9 documentation, and this comprehensive answer What is the difference between JDK_JAVA_OPTIONS and JAVA_TOOL_OPTIONS when using Java 11?.
Related
I'm running in an environment where I can pass parameters but not set environment variables using a normal commandline.
I would like to set environment variables nevertheless. Is there an alternative way to "fool" java that an environment variable is set, e.g. using -D parameters?
(I'm running spark in oozie through hue; all in the Cloudera stack).
The java command itself doesn't seem to allow that. It has a -D parameter, but that sets Java 'system properties':
$ java -help 2>&1 | grep -A1 '\-D'
-D<name>=<value>
set a system property
Java system properties are a sort of Java properties. Like environment variables, java properties are key-value pairs, but aren't the same thing as environment variables: If your Java application reacts to a specific environment variable, setting a system property of the same name won't have any effect unless the application explicitly reacts to that property, too.
If your environment allows you to run arbitrary Java applications and if it allows your Java applications to execute other processes, you can write a little wrapper that sets the environment variables on a ProcessBuilder (see the question Arnon linked in his comment: How do I set environment variables from Java?) to then invoke java with your actual JAR from it. You could either hard-code the environment variables to set, or set them according to system properties the wrapper receives. (Or you could even implement your own shell in Java and pass a script to it.)
Though, if you can modify the source of that actual JAR, a much more idiomatic solution would be to make it itself react to properties instead of (or additionally to) environment variables. (Unless you have to control environment variables that the java command / the JVM reacts to rather than the JAR you'd like to run. Then this approach would not be applicable, of course.)
If you can run arbitrary Java code, you can create and run a Process using ProcessBuilder, including an environment of your choice.
Hence, write a java program that parses a command-line like
java -cp .... your.Prog FOO=BAR BAZ=BOOM command arguments ...
and starts command with the environment extended by FOO and BAZ
(Note that command could be java ...)
I have declared variable -Xmx 2 times in JAVA_OPTS in run.sh of JBoss server.
e.g. -Xmx512m at say line no 15
and -Xmx1024 at say line no 50
So which -Xmx will be actually used by server? And how to check it?
Environment :
Application Server : JBoss 4.2
OS : Linux
It's shell script after all (run.sh). Most recent value is picked up. You can try it for yourself.
Create a file run.sh with following content
#!/bin/bash
JAVA_OPTS="Before"
JAVA_OPTS="After"
echo $JAVA_OPTS
and run it ./run.sh and you should get "After" in the console. Nothing JBoss specific.
So to answer your question -Xmx1024 will be picked up. And to answer your question so as to how to check you can use programs like jconsole to view your java process and check your maximum memory allocated (as you have provided in -Xmx option). You can also do something like ps -ax | grep java to see the Java process and the JAVA_OPTS it used.
I am tweaking the JVM for the tomcat server. I know one can use CATALINA_OPTS to pass on java options. However, one downside of that is that I will have to export that environment variable every time I make a change, so I am wondering if there is an easier way. For example, will something like below work?
$ catalina.sh start -server -Xmx512M -XX:MaxPermSize=256m
You don't need to export this variable just run it that way:
CATALINA_OPTS='-server -Xmx512M -XX:MaxPermSize' catalina.sh start
Very similar to what you posted. The environment variable is visible only to the started process.
Or you can simply define the variable in catalina.sh file.
My app is deployed on Tomcat and I've configured the JAVA_OPTS environment variable in /etc/default/tomcat7.
There seem to be a million different places where these variables can be provided to Tomcat, so I want to check that the values I'm providing are what's actually being used. Is there something I can inspect at runtime to determine the value of this variable. I checked System.getProperties(), but it doesn't seem to be there.
If you're looking for just the property overrides and JVM arguments, you can use RuntimeMXBean:
RuntimeMXBean mxBean = ManagementFactory.getRuntimeMXBean();
System.out.println(mxBean.getInputArguments());
For example, running with the following command-line:
java -Xms512m -Xmx1024m -Dtest.prop=foo com.example.sandbox.RuntimeMXBeanExample
I get the following output:
[-Xms512m, -Xmx1024m, -Dtest.prop=foo]
Note that this does not include arguments passed to the main() method.
You can use System.getenv("JAVA_OPTS") as suggested.
If you don't want to modify code than you can use some of those methods
Java Tools
jps -v displays Java processes with arguments
jvisualvm connects to Java process and let's you inspect number of properties including MXBeans
GNU/Linux tools
ps e displays environment variables passed to processes
One important thing, although post is old. Whatever variable you are passing it should be before the class name you are executing else it will be ignored.
Below will work:
Example: java -classpath . -Dformat=xls -DTabname=\"Base data new\" com.cg.bench.GenerateReport
Below will NOT work:
WRONG Example: java -classpath . com.cg.bench.GenerateReport -Dformat=xls -DTabname=\"Base data new\"
Is there a way that I can set the default heap size for the jvm on my own computer? I want to set it to 1g, because I'm always running custom programs that always hit the overage point in the default jvm size.
I just dont want to have to remember to type -XmX1g everytime I run my java app from the command line...
There has to be an admin way to do this right?
Apparently, _JAVA_OPTIONS works on Linux, too:
$ export _JAVA_OPTIONS="-Xmx1g"
$ java -jar jconsole.jar &
Picked up _JAVA_OPTIONS: -Xmx1g
For Windows users, you can add a system environment variable named _JAVA_OPTIONS, and set the heap size values there. The JVM should be able to grab the virtual machine options from _JAVA_OPTIONS.
This worked for me:
export _JAVA_OPTIONS="-Xmx1g"
It's important that you have no spaces because for me it did not work. I would suggest just copying and pasting. Then I ran:
java -XshowSettings:vm
and it will tell you:
Picked up _JAVA_OPTIONS: -Xmx1g
what platform are you running?..
if its unix, maybe adding
alias java='java -Xmx1g'
to .bashrc (or similar) work
edit: Changing XmX to Xmx
if the platform is Linux, then adding an entry in bash_profile will help.
vim ~/.bash_profile
then add
export _JAVA_OPTIONS="-Xmx4g"