Thursday, December 13, 2007

Accessing the properties file outside the Jar file.

To access the .properties file when the file is outside the jar you just need to enter the classpath value in the file: Manifest-Version: 1.0 Main-Class: pack.HelloWorld Class-Path: . Multiple classpath requirements will be space separated and not ; separated i.e. You have to mention all the jar names. Class-Path: servlet.jar infobus.jar acme/beans.jar acme/mysql.jar For properties in a folder named config, you can put in class path as Class-Path: servlet.jar infobus.jar config/.

Programmatically compiling the Java program using Compiler API

Sample code to programmatically compile a Java program.
public class CompilerExample {
    public static void main(String[] args) {
        String fileToCompile = "c:" +
                + "";
        JavaCompiler compiler =      ToolProvider.getSystemJavaCompiler();
        int compilationResult =, null, null, fileToCompile);
        if (compilationResult == 0) {
            System.out.println("Compilation is successful");
        } else {
            System.out.println("Compilation Failed");

Creating an image file (jpg) using java

You can create the custom images using java with the following snippet. This can be productively used in sign up forms to make the user enter the text in the image manually (to avoid automated script).
BufferedImage image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB); 
 Graphics g = image.getGraphics(); 
 g.drawString("Hello World!!!", 10, 20); 
 try {  
   ImageIO.write(image, "jpg", new File("c:/CustomImage.jpg")); 
 } catch (IOException e) {  

Running multiple instances of tomcat from single installation

To install the tomcat once and use it to launch multiple instances follow the steps:
  1. Create a folder e.g. Alpha in the TOMCAT_HOME.
  2. Copy the conf, logs, temp, webapps and work folders to the Alpha folder.
  3. Modify the conf\server.xml to change the port i.e. make them different from the ports of the main tomcat e.g. change its Connector port to 8081 and so on.
  4. Copy startup.bat from TOMCAT_HOME\bin and paste to TOMCAT_HOME\Alpha\.
  5. Modify the startup.bat by adding the highlighted part to the end of the file.

Making an Executable Jar file

Steps to make a jar file executable jar file (i.e open on double click): Tips to develop an executable jar for a simple 1>Create a text file named and edit it with Manifest-Version: 1.0 Main-Class: Hello *It is important to end the above file with a blank line i.e after the Main-Class line two enters key presses. 2>Then create the archive by typing jar cvmf Hello.jar Hello.class or jar cmf Hello.jar * 3>Run it by typing the command below or simply double clicking the Jar file (For Swing/AWT based applications) java -jar Hello.jar

Signing a Jar

The steps to sign a jar are as follows:

#javac *.java
#jar -cvf cupidIM.jar *.*
#keytool -genkey -keyalg RSA -keystore test_store -alias rsatest
#keytool -export -alias rsatest -file rsatest.cer -keystore test_store
#keytool -import -alias rsatest -file rsatest.cer -keystore cacerts
#jarsigner -keystore test_store cupidIM.jar rsatest

Friday, April 20, 2007

Making a JTree transparent

To make a JTree completely transparent (i.e even after the tree.setOpaque(false) leaves the leaf nodes with white part.. The below code removes even that and makes it totally transparent).

DefaultTreeCellRenderer renderer = new DefaultTreeCellRenderer() {
    public Color getBackgroundNonSelectionColor() {

    public Color getBackground() {