Hints and tips on java

These are some helpful hints on various topics.

 

Locating where class is loaded from


When debugging Java application which contains of multiple multiple jars there are a number of cases where one gets lost in all the jars. Then is vital to know where a third party class is loaded from to know its exact version. To get the load location of StrangeClass, just evaluate the following expression in your runtime:

URL location = StrangeClass.class.getProtectionDomain().getCodeSource().getLocation();

Generating serialVersionUID using serialver


In order to generate serialVersionUID for Serializablе classes one can use serialver tool from JDK bundle. The class however needs to be compiled in a jar or proper directory structure. Here are two examples:

1. Class org.example.MySerializable is compiled in folder project\bin\org\example\Myserializable.class. Then use the following command to generate serialVersionUID:

X:\JDK\bin\serialver -classpath project\bin org.example.MySerializable

2. Class org.example.MySerializable is included in jar file project/lib/myjar.jar. Then just type:

/path/to/JDK/bin/serialver -classpath project/lib/myjar.jar org.example.MySerializable

In both cases add the newly generated id in the class source file and recompile:

private static final long serialVersionUID = 1234572295622776147L;
 

Dynamic instanceOf() in Java

Usually when checking the class of an object we use similar construct:

...
if (myObject instanceof MyClass) {
...

However if the class is determined during runtime this code is not applicable - you can't do something like:

...
Class<?> myClass = Class.forName("MyClass");
if (myObject instanceof myClass) {
...


The correct way to do this is:

...
Class<?> myClass = Class.forName("MyClass");
if (myClass.isInstance(myObject)) {
...


Check if table is present in database using pure JDBC

The following function checks if a table is present in a database associated with JDBC connection. It uses only pure JDBC and no database specific tables or functions which makes it pretty portable.

public boolean isTablePresent(Connection connection, String tableNameToFind) throws SQLException {
    DatabaseMetaData dbm = connection.getMetaData();
    ResultSet rs = dbm.getTables(null, null, null, null);
    boolean result = false;
    while (rs.next()) {
        String tableName = rs.getString(3);
        if (tableNameToFind.equalsIgnoreCase(tableName)) {
            result = true;
            break;
        }
    }
    rs.close();
    return result;
}


Find the jar that contains given Java class

If you have a folder containing several jars and you want to quickly find which one contains given class, use the following script:

findjar.sh:

dir=$1
class=$2
if [ $# -ne 2 ]
then
  echo "Usage: `basename $0` <directory> <class>"
  exit 1
fi

for file in `find $dir -name *.jar`
do
unzip -t $file | grep $class && echo $file
done


Example: find the jar containing MyClass in the folder /my/jars/folder:

findjar.sh /my/jars/folder MyClass