Skip to main content


Showing posts from March, 2007

Putting a Java pogram to sleep

For a good night's sleep wear comfortable loose clothes, do not consume coffee or tea after 6:00 PM, and read something relaxing before going to bed. ok that's cool, but what if you want to put your Java Thread to sleep? Before Java SE 5, the most common way to put threads to sleep was by calling the sleep() method. System.out.println ( "Sleeping for 150 ms" ) ; Thread.sleep ( 150 ) ; System.out.println ( "Sleeping for 2 seconds" ) ; Thread.sleep ( 2  *  1000 ) ; System.out.println ( "Sleeping for 50 nanoseconds" ) ; Thread.sleep ( 0 ,  50 ) ; Java2html     The sleep() method takes a long parameter that  represents the milliseconds we want the thread to sleep. If the time we want our thread to sleep can be easily represented in ms, then this method works out well, but if we want the thread to sleep for some seconds then we have to perform the conversion manually. Even worse,

Security and the Java classloader

The Java classloader plays an important part in the overall security of a Java application. The classloader works along with the SecurityManager and the Access Controller to make a Java system secure. The classloader is important because it is the entity that first loads the classes. The classloader knows the codebase from where the code was loaded, and if the code was signed. The classloader works in three ways to help make a Java system secure: It helps the JVM to maintain the integrity of namespaces. It maps permissions with each class. The permissions associated with every class are also known as the protection domain of the class. This mapping helps the access controller determine which classes have which permissions. It ensures that code which accesses or defines classes has the appropriate permissions to do so. The classloader works in conjunction with the SecurityManager to enforce this. Each of these topics are fairly long, and I will not go into intricate details. I will howe

Loading classes

Now that we have understood how the Classloader locates classes, what exactly does loading a class mean? What happens when a class is loaded? Loading refers to the process of finding the binary form of a class or interface type with a particular name, perhaps by computing it on the fly, but more typically by retrieving a binary representation previously computed from source code by a compiler, and constructing, from that binary form, a Class object to represent the class or interface. The binary format of a class or interface is normally the class file format described in The Java Virtual Machine Specification, but other formats are possible, provided they meet the specified requirements. The method defineClass of class ClassLoader may be used to construct Class objects from binary representations in the class file format. The loading process is implemented by the class ClassLoader and its subclasses. Different subclasses of ClassLoader may implement different loading policies. In part

The Java Classloader #2

Now that we understand the importance of dynamic linking, assume that you are running a dynamically linked C++ program which needs to use a class that is in an external library. The runtime locates the external library using the PATH environment variable. Java programs do not use the PATH environment variable to locate classes, they use the CLASSPATH variable instead. CLASSPATH contains a list of directories, zip files or jar files. You are probably thinking, why Java does not use PATH? Why did it invent another environment variable, the CLASSPATH? One reason why Java did ot choose to use the PATH variable, is because PATH already contains entries needed by other executables. Using PATH may slow down the system, because it will have to process extra entries. OK, so if that convinces you, let's have a look at what the CLASSPATH contains. The CLASSPATH contains a list of directories, which contain class files that will be needed by the Java runtime (Note: Core Java classes like the

The Java classloader #1

This week I am starting a series on the Java classloader . Before we start talking about the classloader here's a little foundation. If you have programmed with C++, you will remember there are two ways of compiling and linking programs. Static Dynamic Any large software consists of many classes and dependencies on external libraries. Statically compiling a program means creating one large executable file containing all the classes and dependencies. Since all that it needs is bundled in one file which is loaded when the program starts, such a program does not need to locate any files when running. However, because statically linked programs have a very large footprint, this approach is not favored, and an approach known as dynamic linking is used. The output of a dynamically compiled program is an executable file containing the core program, and many libraries files. When the program starts running only the main executable is loaded in memory. Dependencies are loaded as a