Posts

Determine jar file of a class

We sometimes need to determine from which jar file some particular class comes from. Here I wrote a small method to find out the name of the jar file we are looking for : public static String getJarURL ( Class mclass ) { URL clsUrl = mclass. getResource ( mclass. getSimpleName ( ) + ".class" ) ; if ( clsUrl ! = null ) { try { URLConnection conn = clsUrl. openConnection ( ) ; if ( conn instanceof JarURLConnection ) { JarURLConnection connection = ( JarURLConnection ) conn ; return connection. getJarFileURL ( ) . toString ( ) ; } } catch ( IOException e ) { throw new RuntimeException ( e ) ; } } return null ; } public static void main ( String [ ] x ) { System . out . println ( getJarURL ( java. net . HttpURLConnection . class ) ) ; }...

Simple Java Exception Handling Tips

Exceptions are errors that occur in runtime , that is , when the application is running. A careful developer should estimate and care about possible runtime errors. Let us give an example : String s = args[0]; int i = Integer.parseInt(s); this piece of code can cause some exceptions. For example args is an array of string. We want to read the first element of this array. What happens if args is null ? What happens if args is empty ? so we need to ensure that the array is not null and not empty.         String s = null ; // initialize s         // check the array is not null or empty         if ( args ! = null && args. length > 0 )             s = args [ 0 ] ;         int i = 0 ;         if ( s ! = null ) // if s is not null we can parse             i = Integer . parseInt ( s ) ;   ...

Schedule windows to close a program

On windows If we need to close a program (maybe a java) at a certain time , use this command : at 13:29 taskkill /F /IM 6976 6976 is the process id (you can find it on task manager - processes tab. If not seen , than on the top menu choose View-Select Coloumns and mark PID (processID) ) to cancel all scheduled commands at /delete you need to open cmd window as administrator to run at command. (startMenu-accessories right click on command window and choose "run as admin" hop this helps.

Convert Java Date to Mysql Date

Normal 0 21 false false false MicrosoftInternetExplorer4 public static String toMysqlDateStr(Date date){ if (date== null ) return "NULL" ; SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss" ); return '\'' +sdf.format(date)+ '\'' ; }

How to measure distance on google maps ?

Use the page here , you just click on points and the distance will appear either in miles or in kilometers. You can also mark multiple points and calculate a real length of a path.

Check if a String contains letter

public static boolean containsLetter(String s) { if ( s == null ) return false; boolean letterFound = false; for (int i = 0; !letterFound && i letterFound = letterFound || Character.isLetter(s.charAt(i)); return letterFound; }

Get MimeType of A File

Use activation.jar to access javax.activation API. File file = new File("foo.jpg"); String mimeType = new MimetypesFileTypeMap().getContentType(file)); // image/jpeg alternatively , you can use filename to detech the mime type : String mimeType = new MimetypesFileTypeMap().getContentType("foo.jpg")); // image/jpeg