Hints and tips
These are some helpful hints on various topics.
Auto generation of current year in copiright message of the website
Most websites have similar notice in their footer:
Copyright 1995-2010 example.com
The problem is that you have to change the current year every year. Here's how to do it with PHP:
Copyright 1995-<?php echo date('Y') ?> example.com
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;
PHP fragment for getting file extension
To get the file extesion in one line using PHP use similar construct:
$ext = end(explode(".", $filename));
Here $filename can be just the file name, absolute or relative path.
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;
}
Printing empty variables in DOS/cmd with echo+
When printing variables on the screen in cmd shell we use echo. However echo has another purpose - it toggles on and off typing output. This however causes strange behavior when printing empty variables. For example the following code does not produce an empty line, but rather "Echo is ON":
(set VAR=)
echo %VAR%
To work around this, put plus sign rigth after echo:
(set VAR=)
echo+%VAR%
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