Showing posts with label Java. Show all posts
Showing posts with label Java. Show all posts

How to find out which jar file a class is loaded from

There are ways to programmatically find out where the java classloader loads a certain class from. But what if you don't have access to the source code and are on a production environment.
In these cases JVM provides you with a nice option :
-verbose:class

So if you are working with a an app server like WebLogic or Tomcat, just modify the appropriate script that sets JVM arguments and you should be able to see output like this in your server log :

 [Loaded java.io.Serializable from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
[Loaded java.lang.Comparable from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
[Loaded java.lang.CharSequence from C:\Program Files\Java\jdk1.7.0_04\jre\lib\rt.jar]
..............................................................................
..............................................................................
..............................................................................

How to extract values from a java properties files in a bash/shell script

Let's say you have a java properties (sample.prop ) file that contains this :

ORACLE_HOST=myhost.us.oracle.com
MW_HOME=/scratch/codrguy/sandbox
DB_HOST=dbhost.us.oracle.com
Now, if we want to extract values from this properties file and use it in a shell/bash script for let's say an automation task, how do we go about that.

There are 3 parts to this task: 1) Getting the line that contains the value we want 2) extract the value from that line 3) store it in a variable to be used later in the script

To do part 1 we can simply grep for the value of the property we need :
For example :
grep ORACLE_HOST sample.prop
this returns
ORACLE_HOST=myhost.us.oracle.com

To do part 2 we pipe the output of part 1 into awk for extraction of the value we need
grep ORACLE_HOST sample.prop |  awk -F= '{print $2}'
this returns
myhost.us.oracle.com

the -F option of awk sets the delimiter to be '=' and then we print the second record which is what we need

Finally to store this value in a variable we do :

my_value=$(grep ORACLE_HOST sample.prop |  awk -F= '{print $2}')


How to take a Java thread dump from a shell/bash script

We already know how to get a Java thread dump manually. But what if we want to take a Java thread dump from a script to help us automate things.

To do this we need to come up with a ps and grep combination that will only return one result. so grep as many times as neccessary and/or use keywords that are only found in your process name so you only get one result.

Remember to add [] somewhere in your first grep so grep doesn't matches itself.
output=$(ps -ef | grep m[y]ProcesseName )
Then all you need to do is sending the QUIT signal to $2 which stores the PID of your process.
kill -QUIT $2
So just add the above two lines to your script after modifying the first line to fit your needs and you should be good to go.

How to create a Java Thread dump

To create a java thread dump in Linux you must send a QUIT signal to your java process. JVM then catches that and prints the thread dump to standard output. If you are running your program from a script that has it's output directed to a file the thread dump will be written to that file obviously .

So first do we need to find the PID of our java program.

ps -ef | grep java

for example this could return :

lapps     3243  3065 21 Nov12 ?        08:01:50 java -Xms2048m -Xmx10g -XX:MaxPermSize=256m -Djava.awt.headless=true -XX:+UseParallelGC -XX:-UseGCOverheadLimit -classpath .:./test.jar:./lib/:./lib/* mypackage.org.TestApp

Now that we have 3243 as the PID, we simply send a QUIT signal to it.

kill -QUIT 3243