<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3892323058569646272</id><updated>2024-09-06T14:14:47.627-07:00</updated><category term="JAVA"/><category term="BigDecimal"/><category term="Date"/><category term="STARTX"/><category term="STARTXWIN"/><category term="Swing"/><category term="Utilities"/><category term="XHOST"/><category term="XLIB"/><category term="XWINDOWS"/><category term="cronjob"/><category term="crontab"/><category term="custom string length comparator"/><category term="free"/><category term="graph"/><category term="java find matching part string"/><category term="jfreechart"/><category term="longest matching substring in java"/><category term="observer design pattern java rmi"/><category term="unix"/><title type='text'>Digg into JAVA</title><subtitle type='html'>Misc Java related problems I faced and solutions I found. Hope this helps other developers.</subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>16</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-8200800366312300266</id><published>2011-02-21T16:19:00.000-08:00</published><updated>2011-02-21T16:22:09.058-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="observer design pattern java rmi"/><title type='text'>Java Observer Design Pattern using Rmi</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Recently I had a requirement in my work, where I had to PUSH data from server to client, when new data arrives.&lt;br /&gt;
Now obviously I considered using the &lt;a href=&quot;http://en.wikipedia.org/wiki/Observer_pattern&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;OBSERVER&lt;/span&gt; &lt;/a&gt;design pattern to solve my problem.&lt;br /&gt;
The clients were remote but in the same company network. And there was no messaging service available.&lt;br /&gt;
&lt;br /&gt;
I could not find a way to use observer pattern over &lt;a href=&quot;http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;HTTP&lt;/span&gt;&lt;/a&gt;, so I chose to use &lt;a href=&quot;http://en.wikipedia.org/wiki/Java_remote_method_invocation&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;RMI&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
Implementing Observer design pattern over RMI is a bit tricky. After searching on the internet,&lt;br /&gt;
I found out that a good samaritan has already done the job.&lt;br /&gt;
Here is the link, where I took the basic codebase from&lt;br /&gt;
&lt;a href=&quot;http://sites.google.com/site/jamespandavan/Home/java/sample-remote-observer-based-on-rmi&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;http://sites.google.com/site/jamespandavan/Home/java/sample-remote-observer-based-on-rmi&lt;/span&gt;&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;Now there is a problem in this design approach&lt;/b&gt;. Once you start the server and the client, the client perfectly receives the messages pushed by the server.&lt;br /&gt;
But if the server goes down for some reason, then there is no way for the client to know it. &lt;b&gt;Yes, there is no &lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;&lt;a href=&quot;http://download.oracle.com/javase/1.4.2/docs/api/java/rmi/RemoteException.html&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;RemoteException&lt;/span&gt;&lt;/a&gt; &lt;/span&gt;or &lt;a href=&quot;http://download.oracle.com/javase/1.4.2/docs/api/java/rmi/ConnectException.html&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;ConnectException&lt;/span&gt;&lt;/a&gt; thrown in this case&lt;/b&gt;.&lt;br /&gt;
The client keeps itself running, wondering, why it is not getting any data from server.&lt;br /&gt;
The existing client will not receive any data, even when the server is back up.&lt;br /&gt;
&lt;br /&gt;
To solve this problem, I used the last timestamp, when the client received PUSH data from server. If the time is more than a specified time, client considers the connection dead and tries to reconnect to the server.&lt;br /&gt;
This trying to reconnect goes on forever, until the server goes up. Then everything works fine, just like before, as if nothing happened.&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #073763;&quot;&gt;&lt;b&gt;&lt;i&gt;Few words before sharing the codebase:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
1) Don&#39;t forget to make your data serializable.&lt;br /&gt;
2) Generate the rmi stub class using the rmic command. Details about this command can be found in the Oracle website &lt;a href=&quot;http://download.oracle.com/javase/1.3/docs/tooldocs/win32/rmic.html&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: blue;&quot;&gt;here&lt;/span&gt;&lt;/a&gt;.&lt;br /&gt;
2) After the server code is written, create a jar file, containing the RMIService interface, the Model class and the rmi stub.&lt;br /&gt;
3) Clients on separate JVM, have to put this jar in their classpath.&lt;br /&gt;
&lt;br /&gt;
Here&#39;s the code, more or less the same code found in the above link, with slight modifications:&lt;br /&gt;
&lt;br /&gt;
&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;&lt;i&gt;Running the Server/Client:&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
java -Djava.security.licy=security.policy RmiServer
java -Djava.security.licy=security.policy RmiClient
&lt;/code&gt;&lt;/pre&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;The Security Policy file:&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
grant
{
    Permission java.security.AllPermission;
};
&lt;/code&gt;&lt;/pre&gt;&lt;i&gt;&lt;b&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;The POJO DTO (Don&#39;t forget the serializable part)&lt;/span&gt;&lt;/b&gt;&lt;/i&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.io.Serializable;

public class Model implements Serializable{

 /**
  * 
  */
 private static final long serialVersionUID = 1L;
 private String name;
 private int id;
 private long lastTimsStamp;
 public Model(String name, int id,long lastTimsStamp) {
  super();
  this.name = name;
  this.id = id;
  this.lastTimsStamp = lastTimsStamp;
 }
 public String getName() {
  return name;
 }
 public int getId() {
  return id;
 }
 public long getLastTimsStamp() {
  return lastTimsStamp;
 }
 
 
}
&lt;/code&gt;&lt;/pre&gt;&lt;b&gt;&lt;i&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;Remote observer interface&lt;/span&gt;&lt;/i&gt;&lt;/b&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.rmi.Remote;
import java.rmi.RemoteException;

public interface RemoteObserver extends Remote {

    void update(Object observable, Object updateMsg) throws RemoteException;

}
&lt;/code&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;&lt;i&gt;RMI client program implementing the interface&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.net.MalformedURLException;
import java.rmi.ConnectException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import rmisubject.Model;
import rmisubject.RemoteObserver;
import rmisubject.RmiSubjectService;

public class RmiObserverClient extends UnicastRemoteObject implements RemoteObserver {
    
 /**
  * If the client hasn&#39;t received any data for the span of connectionCheckInterval, it will try to reconnect the server.
  * It will keep trying until the server is not back up.
  * @param connectionCheckInterval
  * @throws RemoteException
  */
 public RmiObserverClient(long connectionCheckInterval) throws RemoteException{
        super();
        this.connectionCheckInterval = connectionCheckInterval;
    }
    private static long lastTimeStamp;
    private static final long serialVersionUID = 1L;
    private static long connectionCheckInterval = 10*1000;
    private static RmiSubjectService remoteService;
    Thread checkConnectionAlive = new Thread() {
       @Override
       public void run() {
           while (true) {
               try {
                   Thread.sleep(connectionCheckInterval);
               } catch (InterruptedException e) {
                   // ignore
               }
               long currentTimeStamp = System.currentTimeMillis();
               
               System.out.println(&quot;Inside client checkConnectionAlive : &quot; + currentTimeStamp);
               
               if (currentTimeStamp - lastTimeStamp&amp;gt;connectionCheckInterval )subscribe();
           }
       };
   };
   
    public static void main(String[] args) {
     try {
      RmiObserverClient rc = new RmiObserverClient(10 * 1000);
   rc.subscribe();
   rc.checkConnectionAlive.start();
  } catch (RemoteException e) {
   e.printStackTrace();
  }
     
    }
    public void subscribe(){
     if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        try {
            remoteService = (RmiSubjectService) Naming.lookup(&quot;//localhost:9999/RmiService&quot;);
            remoteService.addObserver(this);
        } catch (ConnectException ex) {
            ex.printStackTrace();
        }catch (RemoteException e) {
          e.printStackTrace();
  }catch (MalformedURLException e) {
   e.printStackTrace();
  }catch (NotBoundException e) {
   e.printStackTrace();
  }
    }
    @Override
    public void update(Object observable, Object updateMsg)
            throws RemoteException {
        System.out.println(&quot;===============start message=============&quot;);
        Model model = (Model)updateMsg;
        System.out.println(model.getName());
        System.out.println(model.getId());
        lastTimeStamp = model.getLastTimsStamp();
        System.out.println(lastTimeStamp);
        System.out.println(&quot;===============end message=============&quot;);
    }
}
&lt;/code&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;&lt;b&gt;&lt;i&gt;RMI service interface&lt;/i&gt;&lt;/b&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
public interface RmiSubjectService extends Remote {

   void addObserver(RemoteObserver o) throws RemoteException;
}
&lt;/code&gt;&lt;/pre&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;color: #351c75;&quot;&gt;&lt;i&gt;&lt;b&gt;RMI server (the observable)&lt;/b&gt;&lt;/i&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.io.Serializable;
import java.rmi.RMISecurityManager;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;
import java.util.Observable;
import java.util.Observer;

public class RmiSubjectImpl extends Observable implements RmiSubjectService {

    private class WrappedObserver implements Observer, Serializable {

        private static final long serialVersionUID = 1L;
       
        private RemoteObserver ro = null;

        public WrappedObserver(RemoteObserver ro) {
            this.ro = ro;
        }

        @Override
        public void update(Observable o, Object arg) {
            try {
                ro.update(o.toString(), arg);
            } catch (RemoteException e) {
                System.out
                        .println(&quot;Remote exception removing observer:&quot; + this);
                o.deleteObserver(this);
            }
        }

    }

    @Override
    public void addObserver(RemoteObserver o) throws RemoteException {
        WrappedObserver mo = new WrappedObserver(o);
        addObserver(mo);
        System.out.println(&quot;Added observer:&quot; + mo);
    }

    Thread thread = new Thread() {
      private int i=0;
        @Override
        public void run() {
            while (true) {
                try {
                    Thread.sleep(5 * 1000);
                } catch (InterruptedException e) {
                    // ignore
                }
                setChanged();
                notifyObservers(new Model(&quot;model&quot;, i++,System.currentTimeMillis()));
                //notifyObservers(new Date());
            }
        };
    };

    public RmiSubjectImpl() {
        thread.start();
    }

    private static final long serialVersionUID = 1L;

    public static void main(String[] args) {
        if (System.getSecurityManager() == null)
            System.setSecurityManager(new RMISecurityManager());
        try {
            Registry rmiRegistry = LocateRegistry.createRegistry(9999);
            RmiSubjectService rmiService = (RmiSubjectService) UnicastRemoteObject
                    .exportObject(new RmiSubjectImpl(), 9999);
            rmiRegistry.bind(&quot;RmiService&quot;, rmiService);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Hope this helps. Let me know if it doesn&#39;t.&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/8200800366312300266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/8200800366312300266' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/8200800366312300266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/8200800366312300266'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2011/02/java-observer-pattern-using-rmi.html' title='Java Observer Design Pattern using Rmi'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-8017773110903287228</id><published>2011-02-16T20:54:00.000-08:00</published><updated>2011-02-24T17:28:14.182-08:00</updated><title type='text'>How to Convert XML attributes to elements using XSLT</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Recently I was using &lt;a href=&quot;http://commons.apache.org/digester/&quot;&gt;Apache Digester&lt;/a&gt; to convert( Transform) a XML content into List of Objects.&lt;br /&gt;
Out of all the APIs I tried, I found digester was the best solution for my of requirement.&lt;br /&gt;
For people who didnot use Digester before, I would recommend giving it a try. It will make your life&lt;br /&gt;
easier. You can go home from work early and spend some quality time with quality people.&lt;br /&gt;
I will probably post some code snippets on how to use Digester, but this post is not about that.&lt;br /&gt;
&lt;br /&gt;
Coming back to point.&lt;br /&gt;
Just like any other XML, the XML content I was trying to parse, contained few attributes, alongwith the elements.&lt;br /&gt;
Now digester might be having some features to take care of attributes, but I found it hard to follow.&lt;br /&gt;
I created the following XSLT instead. Which, when applied to a XML, transforms all it&#39;s attributes into TAGS.&lt;br /&gt;
The XSLT is generic enough to be used on any xml content.&lt;br /&gt;
If you are not sure, how to apply this xslt to your XML, keep reading. I have some sample code too.&lt;br /&gt;
&lt;br /&gt;
Firstly, the XSLT content:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;

&amp;lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&amp;gt;
&amp;lt;xsl:stylesheet 
    version=&quot;2.0&quot;
    xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;&amp;gt;
  &amp;lt;xsl:output method=&quot;html&quot; encoding=&quot;UTF-8&quot;/&amp;gt;
  &amp;lt;xsl:strip-space elements=&quot;*&quot; /&amp;gt;
  &amp;lt;xsl:template match=&quot;*&quot;&amp;gt;
  &amp;lt;xsl:copy &amp;gt;
      &amp;lt;xsl:if test=&quot;@*&quot;&amp;gt;
        &amp;lt;xsl:for-each select=&quot;@*&quot;&amp;gt;
          &amp;lt;xsl:element name=&quot;{name()}&quot; &amp;gt;
            &amp;lt;xsl:value-of select=&quot;.&quot; /&amp;gt;
          &amp;lt;/xsl:element&amp;gt;
        &amp;lt;/xsl:for-each&amp;gt;
      &amp;lt;/xsl:if&amp;gt;
      &amp;lt;xsl:apply-templates /&amp;gt;
    &amp;lt;/xsl:copy&amp;gt;
    &amp;lt;/xsl:template&amp;gt;
&amp;lt;/xsl:stylesheet&amp;gt; 

&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
It is a small XSLT and should be easy to understand. In case you have problem understanding the logic, ask me.&lt;br /&gt;
&lt;br /&gt;
Sample code to transform any XML using any XSLT:&lt;br /&gt;
&lt;br /&gt;
Prerequisite:-&lt;br /&gt;
A JAVA environment offcourse. Lets not get into that deep. Amongst all the libraries that come with the distribution, the one you actively need is&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
rt.jar
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Here is some sample code. The method names are self-explanatory.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
 public static String convertAllAttributesToElements(String xmlContent) throws TransformerException{
   javax.xml.transform.Source xmlSource =new javax.xml.transform.stream.StreamSource(new StringReader(xmlContent));
   javax.xml.transform.Source xsltSource =new javax.xml.transform.stream.StreamSource(xsltFile);
   return transformContent(xmlSource,xsltSource);
 }
 
 public static String convertAllAttributesToElements(File xmlFile) throws TransformerException{
   javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(xmlFile);
   javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile);
   return transformContent(xmlSource,xsltSource);
 }
 
 private static String transformContent(javax.xml.transform.Source xmlSource,javax.xml.transform.Source xsltSource) throws TransformerException{
  String modifiedContent = null;
  javax.xml.transform.TransformerFactory transFact = javax.xml.transform.TransformerFactory.newInstance();
  ByteArrayOutputStream outStream = new ByteArrayOutputStream();
  StreamResult result = new javax.xml.transform.stream.StreamResult(outStream);
  javax.xml.transform.Transformer trans = transFact.newTransformer(xsltSource);
  trans.transform(xmlSource, result);
  byte[] b = ((ByteArrayOutputStream)result.getOutputStream()).toByteArray();  
  modifiedContent = new String (b);
  //removing any line feed or spaces to save bytes.
  modifiedContent = modifiedContent.replaceAll(&quot;[\\n\\r\\s]+&quot;,&quot;&quot;);
  return modifiedContent;
 }
 
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
I hope this helps guys. Let me know if there are any issues.&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/8017773110903287228/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/8017773110903287228' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/8017773110903287228'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/8017773110903287228'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2011/02/converttransform-xml-attributes-to.html' title='How to Convert XML attributes to elements using XSLT'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-7087862347154331652</id><published>2011-01-10T15:33:00.000-08:00</published><updated>2011-02-16T20:32:29.665-08:00</updated><title type='text'>ResultSet to XML converter</title><content type='html'>&lt;div dir=&quot;ltr&quot; style=&quot;text-align: left;&quot; trbidi=&quot;on&quot;&gt;Sometimes there is a need to convert a resultset into a xml file. The xml file can be shared across webservices.&lt;br /&gt;
I had a similar requirement but found no example online.&lt;br /&gt;
So I created this utility to convert resultsets into xml. The language of development is Java.&lt;br /&gt;
The code has provision to accept multiple resultsets. All the resultsets will be appended in the xml file.&lt;br /&gt;
The rows of tables can be indentified by row identifiers. Which is user supplied.&lt;br /&gt;
&lt;br /&gt;
Understanding that the resultsets can be very large, I have used stax based parser to write the xml.&lt;br /&gt;
Amongst STAX based parsers, I have choosen WOODSTOX as my parser. Because it has very good performance ratings in various benchmark tests.&lt;br /&gt;
Details of Woodstox can be found &lt;a href=&quot;http://woodstox.codehaus.org/&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
I have uploaded my source files in sourceforge. And can be found at the following location.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;https://sourceforge.net/projects/rstoxmlwriter/&quot;&gt;https://sourceforge.net/projects/rstoxmlwriter/&lt;/a&gt;&lt;br /&gt;
&lt;br /&gt;
A typical usage might be:&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for (String tableName : tableNames) {
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;String rowId = &quot;&lt;/span&gt;row&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;&quot;;&lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;String query = &quot;Select &quot;+selectClause+&quot; from &quot;+table+restrictions;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;ResultSet tableData = getResult(query);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;XmlElementInfo xmlElementInfo = new XmlElementInfo(tableData,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;rowId.toUpperCase(),table.toUpperCase());
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;xmlElementInfos.add(xmlElementInfo);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;try {
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;new XMLWriter().writeXML(xmlElementInfos,parentTag, outputLocation+xmlFileName);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;} catch (XMLWriterException e) {
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;e.printStackTrace();
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;closeDB();
&lt;/pre&gt;&lt;/code&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;I would be happy, if anybody finds this useful.&amp;nbsp;&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;Also please put your comments. Let me know if there are any bugs. Let me know if there is any further enhancement requests.&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div style=&quot;margin-bottom: 0px; margin-left: 0px; margin-right: 0px; margin-top: 0px;&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/7087862347154331652/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/7087862347154331652' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7087862347154331652'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7087862347154331652'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2011/01/resultset-to-xml-converter.html' title='ResultSet to XML converter'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-4615521448198579706</id><published>2010-02-22T15:55:00.000-08:00</published><updated>2010-02-22T15:57:54.443-08:00</updated><title type='text'>Update Twitter status using this java code</title><content type='html'>&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import sun.misc.BASE64Encoder;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStreamWriter;
import java.net.URLConnection;
import java.net.URL;
import java.net.URLEncoder;

public class Twitter {

public static void main(String[] args) throws Exception {


URL url = new URL(&quot;https://twitter.com/statuses/update.xml&quot;);
URLConnection connection = url.openConnection();

String crendetials = &quot;username&quot; + &quot;:&quot; + &quot;password&quot;;

BASE64Encoder encoder = new BASE64Encoder();

String encodedCredentials = new String(encoder.encodeBuffer(crendetials.getBytes())).trim();

connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestProperty(&quot;Authorization&quot;, &quot;Basic &quot; + encodedCredentials);

OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write(&quot;status=&quot; + URLEncoder.encode(&quot;It was really simple to write simple status message updation code using Java. This message also goes thought my Java code. It&#39;s fun&quot;, &quot;UTF-8&quot;));
out.close();

BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String response;
while ((response = in.readLine()) != null) {
System.out.println(response);
}
in.close();
}

}&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/4615521448198579706/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/4615521448198579706' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/4615521448198579706'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/4615521448198579706'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2010/02/update-twitter-status-using-this-java.html' title='Update Twitter status using this java code'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-7687783669732445453</id><published>2009-12-30T16:43:00.001-08:00</published><updated>2010-01-09T23:57:49.808-08:00</updated><title type='text'>Java String array - Single and Multidimensional</title><content type='html'>In java arrays can be single as well as multi dimentional. This is true not only for Strings, but also for int, double, char etc all datatypes.&lt;br /&gt;
Arrays can be declared in two ways. Today i will show you how to declare arrays in Java.&lt;br /&gt;
This post will cover both the ways array can be declared and also how to access the elements.&lt;br /&gt;
Please comment if this post helps you or if there is something I missed.&lt;br /&gt;
Happy Coding.&lt;br /&gt;
&lt;br /&gt;
Lets start with Single Dimension Arrays:&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;


&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String [] OneDArray1 = new String[10];
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;OneDArray1[0] = &quot;1&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;OneDArray1[2] = &quot;2&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;OneDArray1[5] = &quot;3&quot;;

&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(int i=0;i&lt;onedarray1.length;i++) {=&quot;&quot;&gt;&lt;/onedarray1.length;i++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;System.out.println(OneDArray1[i]);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
Output of this code snippet:

&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;1
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;2
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;3
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;null
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
Another way of declaring single dimension arrays:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String [] OneDArray2 = {&quot;1&quot;,&quot;2&quot;,&quot;3&quot;};
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(int i=0;i&lt;onedarray2.length;i++) {=&quot;&quot;&gt;&lt;/onedarray2.length;i++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;System.out.println(OneDArray2[i]);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
Output of this code snippet:

&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;1
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;2&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;3
&lt;/code&gt;&lt;code&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
Multi Dimensional Arrays&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;String TwoDArray1[][] = new String[2][3];
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[0][0] = &quot;1&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[0][1] = &quot;2&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[0][2] = &quot;3&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[1][0] = &quot;4&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[1][1] = &quot;5&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;TwoDArray1[1][2] = &quot;6&quot;;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(int i=0;i&lt;twodarray1.length;i++) {=&quot;&quot;&gt;&lt;/twodarray1.length;i++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;for(int j=0;j&lt;twodarray1.length+1;j++) {=&quot;&quot;&gt;&lt;/twodarray1.length+1;j++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;System.out.println(TwoDArray1[i][j]);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}

Output of this code snippet:

&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;1
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;2
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;3
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;4
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;5
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;6
&lt;/code&gt;&lt;code&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
Another way of declaring multi dimensional arrays:&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;String TwoDArray2[][] = { {&quot;1&quot;,&quot;2&quot;},{&quot;3&quot;,&quot;4&quot;} };
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;for(int i=0;i&lt;twodarray2.length;i++) {=&quot;&quot;&gt;&lt;/twodarray2.length;i++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;for(int j=0;j&lt;twodarray2.length;j++) {=&quot;&quot;&gt;&lt;/twodarray2.length;j++)&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;System.out.println(TwoDArray2[i][j]);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;
Output of this code snippet:
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;1
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;2
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;3
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;4
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/7687783669732445453/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/7687783669732445453' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7687783669732445453'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7687783669732445453'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/12/java-string-array-single-and-multi.html' title='Java String array - Single and Multidimensional'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-1800949259946211124</id><published>2009-12-18T19:55:00.000-08:00</published><updated>2010-01-06T17:53:09.866-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="java find matching part string"/><category scheme="http://www.blogger.com/atom/ns#" term="longest matching substring in java"/><title type='text'>Java find longest matching part or substring of string</title><content type='html'>Finding largest common sub string from an array of strings is often very useful. Recently I had this requirement, where I was trying to exclude a list of strings by using regex patterns. For this I had to find the greatest matching substring among the strings, so that I put that substring as a regex, and save a lot of effort.&lt;br /&gt;
Unfortunately I didn&#39;t find any ready made code on the net, that does the same thing. So I wrote my own.&lt;br /&gt;
&lt;br /&gt;
I tried to make the logic as optimum as possible.&lt;br /&gt;
If anyone has a better algorithm in mind, please share it with me.&lt;br /&gt;
&lt;br /&gt;
I have used a custom string length comparator, so optimise the string traversing. Readers can write their own implementation of the comparator, or copy my custom string length comparator class from &lt;a href=&quot;http://diggintojava.blogspot.com/2009/12/custom-string-length-comparator-java.html&quot;&gt;here&lt;/a&gt;.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
 import java.util.Arrays;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;import java.util.HashMap;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;import java.util.Map;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;import java.util.Set;
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;import java.util.TreeSet;

&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;public class SubString {

&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;public static void main(String[] args) {
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;String [] strs = {
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;&quot;this is a very big first string which should be processed at last&quot;,
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;&quot;this is a very small string&quot; ,
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;&quot;this is a very boring string nothing special&quot;,
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;&quot;this is a very simple yet bigger string, second large&quot;
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;};

&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;//Arrays.sort(strs, new CustomStringLengthComparator());
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;String sub = strs[0];
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;Map&lt;integer,string&gt; subStrings = new HashMap&lt;integer, string=&quot;&quot;&gt;();&lt;/integer,&gt;&lt;/integer,string&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;int j=strs[0].length();

&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;String comparingString =strs[1];
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;while (j&amp;gt;=0) {
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;for(int i=0;i&amp;lt; j;i++) {
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;sub = strs[0].substring(i, j);
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;if (comparingString.contains(sub)) {
&lt;span style=&quot;white-space: pre;&quot;&gt;      &lt;/span&gt;subStrings.put(sub.length(),sub);
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;}
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;}
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;j--;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;}
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;boolean flag =false;
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;if(!subStrings.isEmpty()) {
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;Set&lt;integer&gt; set = subStrings.keySet();&lt;/integer&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;TreeSet treeSet = new TreeSet(set); &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;String matchedString = subStrings.get(treeSet.last()); &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;for(int k=2;k&lt;strs.length;k++) {=&quot;&quot;&gt;&lt;/strs.length;k++)&gt;
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;if (strs[k].contains(matchedString)) {
&lt;span style=&quot;white-space: pre;&quot;&gt;      &lt;/span&gt;flag=true; &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;}else {
&lt;span style=&quot;white-space: pre;&quot;&gt;      &lt;/span&gt;flag=false; &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;      &lt;/span&gt;break; &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;} &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;} &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;if (flag) { 
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;System.out.println(&quot;Matched : &quot; + subStrings.get(treeSet.last()));
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;}else { &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;     &lt;/span&gt;System.out.println(&quot;No Match&quot;); &amp;nbsp; &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;    &lt;/span&gt;} &amp;nbsp; 
&lt;span style=&quot;white-space: pre;&quot;&gt;   &lt;/span&gt;} 
&lt;span style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;} 
&lt;span style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}
&lt;/code&gt;
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/1800949259946211124/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/1800949259946211124' title='2 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/1800949259946211124'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/1800949259946211124'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/12/longest-matching-substring-in-java.html' title='Java find longest matching part or substring of string'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>2</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-9207068773894852312</id><published>2009-12-18T19:38:00.001-08:00</published><updated>2009-12-18T19:56:37.029-08:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="custom string length comparator"/><title type='text'>Custom String Length Comparator Java</title><content type='html'>&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 13px; white-space: pre-wrap;&quot;&gt;The default sort method for strings is Java is it&#39;s character values. For example, If we have a string array:  &lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: small;&quot;&gt;&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
String [] strs = {&quot;abc&quot;, &quot;xyz&quot;, &quot;mnop123&quot;};
&lt;/code&gt;&lt;/pre&gt;And we use Arrays.sort(strs), the output will be in the following order: &lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
abc
mnop123
xyz
&lt;/code&gt;&lt;/pre&gt;But there are times, when we want to sort strings, by their length. The following custom string length comparator will serve the purpose:  &lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.util.Comparator;

public class CustomStringLengthComparator implements Comparator&lt;string&gt;{

 public int compare(String o1, String o2) {
  if (o1.length() &amp;lt; o2.length()) {
        return -1;
      } else if (o1.length() &amp;gt; o2.length()) {
        return 1;
      } else {
        return 0;
      }
 }

}

&lt;/string&gt;&lt;/code&gt;
&lt;/pre&gt;Instead of using  &lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
Arrays.sort(strs)
&lt;/code&gt;&lt;/pre&gt;we have to use    &lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
Arrays.sort(strs, new CustomStringLengthComparator());
&lt;/code&gt;&lt;/pre&gt;&lt;/span&gt;&lt;/span&gt;&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 13px; white-space: pre-wrap;&quot;&gt; &lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;div&gt;&lt;span style=&quot;font-family: &#39;Lucida Grande&#39;, helvetica, sans-serif; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 13px; white-space: pre-wrap;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/9207068773894852312/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/9207068773894852312' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/9207068773894852312'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/9207068773894852312'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/12/custom-string-length-comparator-java.html' title='Custom String Length Comparator Java'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-6108811529667845247</id><published>2009-12-09T15:22:00.000-08:00</published><updated>2009-12-30T16:48:26.566-08:00</updated><title type='text'>Currency format</title><content type='html'>As we know the way of formatting currency are different in many country. Main there is a big difference in European and British way. This code snippet converts the currency to appropriate formats and also does the reverse parsing.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;
public static void main(String args[]){
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt;  &lt;/span&gt;// Format
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;Locale locale = Locale.FRANCE;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;String string1 = NumberFormat.getCurrencyInstance(locale).format(123456789.12);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;System.out.println(string1);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; 
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;locale = Locale.US;
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;String string = NumberFormat.getCurrencyInstance(locale).format(123456789.12);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;System.out.println(string);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; 
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;// Parse
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;try {
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Number number = NumberFormat.getCurrencyInstance(locale).parse(string1);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(number.toString());
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;number = NumberFormat.getCurrencyInstance(locale).parse(string);
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(number.toString());
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;} catch (ParseException e) {
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt; &amp;nbsp; &amp;nbsp;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;}
&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;
&lt;/code&gt;&lt;code&gt;&lt;span class=&quot;Apple-tab-span&quot; style=&quot;white-space: pre;&quot;&gt; &lt;/span&gt;&lt;/code&gt;
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/6108811529667845247/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/6108811529667845247' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/6108811529667845247'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/6108811529667845247'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/12/currency-format.html' title='Currency format'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-330003261990001516</id><published>2009-12-09T15:07:00.001-08:00</published><updated>2009-12-30T16:49:46.598-08:00</updated><title type='text'>List The Contents of a ZIP File</title><content type='html'>This is a utility program to view the contents of a zip file&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.io.IOException;
import java.util.Enumeration;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
&amp;nbsp;*
&amp;nbsp;* @author Rakesh P
&amp;nbsp;* ZipFile Function Accept the Path of the Zip File 
&amp;nbsp;*/
public class ZipContents{
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;public void listContentsOfZipFile() {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ZipFile zipFile = new ZipFile(&quot;d:/FeedtoInterfaces.zip&quot;);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Enumeration zipEntries = zipFile.entries();
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (zipEntries.hasMoreElements()) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//Process the name, here we just print it out
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(((ZipEntry)zipEntries.nextElement()).getName());
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} catch (IOException ex) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;ex.printStackTrace();
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;/**
&amp;nbsp;&amp;nbsp; &amp;nbsp; * @param args the command line arguments
&amp;nbsp;&amp;nbsp; &amp;nbsp; */
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static void main(String[] args) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;new &amp;nbsp;ZipContents().listContentsOfZipFile();
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
}
&lt;/code&gt;&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/330003261990001516/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/330003261990001516' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/330003261990001516'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/330003261990001516'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/12/list-contents-of-zip-file.html' title='List The Contents of a ZIP File'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-5532163737204379794</id><published>2009-10-25T19:11:00.001-07:00</published><updated>2009-12-30T16:50:50.066-08:00</updated><title type='text'>Java Swing Example: Image dimension from url</title><content type='html'>This code can be used to get the dimension of any image using its url. Minor tweaks can be made to get localized image dimensions too.Hope this helps.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;font-family: monospace; font-size: 12px; line-height: 15px; white-space: pre-wrap;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;import java.awt.Image;
import java.awt.MediaTracker;
import java.awt.Toolkit;
import java.net.MalformedURLException;
import java.net.URL;
import javax.swing.JFrame;

public class ImageDimensions extends JFrame {
 
 protected static Toolkit tool = Toolkit.getDefaultToolkit();
 
 public ImageDimensions() {
 }

 protected void calculateHeightWidth()
   throws InterruptedException {
  Image image = null;
  try {
   image = tool.createImage(new URL(&quot;Image URL&quot;));
   
  } catch (MalformedURLException e) {
   e.printStackTrace();
  }catch(Exception e1){
   e1.printStackTrace();
  }
  
  MediaTracker mTracker = new MediaTracker(this);
  mTracker.addImage(image, 1);
  int i = 0;
  do {
   mTracker.waitForID(1);
   //added counter and break for the 404 or bad url 
   i++;
   if (i==5) break;
  } while (mTracker.statusID(1, true) != MediaTracker.COMPLETE);

System.out.println(image.getHeight(null));
System.out.println(image.getWidth(null));
 }
 
 public static void main(String[] args) {
 try {
  new ImageDimensions().calculateHeightWidth();
 } catch (InterruptedException e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
 } 
 }
 
}

&lt;span style=&quot;font-family: monospace; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 15px; white-space: pre-wrap;&quot;&gt;
&lt;/span&gt;&lt;/span&gt;
&lt;/code&gt;&lt;code&gt;&lt;span style=&quot;font-family: monospace; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 15px; white-space: pre-wrap;&quot;&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;span style=&quot;font-family: monospace; font-size: small;&quot;&gt;&lt;/span&gt;
&lt;span style=&quot;font-family: monospace; font-size: small;&quot;&gt;&lt;span style=&quot;font-size: 12px; line-height: 15px; white-space: pre-wrap;&quot;&gt;&lt;/span&gt;&lt;/span&gt;
&lt;/pre&gt;&lt;span style=&quot;font-family: monospace; font-size: small;&quot;&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/5532163737204379794/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/5532163737204379794' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/5532163737204379794'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/5532163737204379794'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/java-swing-examples-image-dimension.html' title='Java Swing Example: Image dimension from url'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-40220991344287740</id><published>2009-10-21T12:15:00.000-07:00</published><updated>2009-12-30T16:49:08.283-08:00</updated><title type='text'>Date to calendar,date utils... a simple example</title><content type='html'>Sometimes in you application you will require the calendar logic. In java the Date class methods are mostly deprecated and usage of Calendar class is encouraged. This  utility class will do the operations using the Calendar api. This also can be use as Date to calendar converter or calendar to date converter. The methods are synchronized, so are threadsafe. I didn&#39;t make the calendar static, you can do that is required.&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.util.Calendar;
import java.util.Date;

&lt;span style=&quot;color: #6aa84f;&quot;&gt;/**
 * This class will operate as a calendar. First set a date.
 * Then change the day,month or year as require. 
 * Then get the date back. There is also a feature of getting the current date, if needed,
 * which you can set as the base date again and do operations. 
 * Only date operations are permitted. 
 * Add logic, if you want the time related operations too.
 */&lt;/span&gt;
public class CustomCalendar {

private Calendar date;
private static final int BASEYEAR = 1900;
&lt;span style=&quot;color: #6aa84f;&quot;&gt;//Set the date before operation. Converts date to calendar for easier operation&lt;/span&gt;
public CustomCalendar(Date date) {
 super();
  this.date = Calendar.getInstance();
  this.date.setTime(date);
}

public CustomCalendar(Calendar date) {
 super();
 this.date = date;
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the prev year relative to the date already set(In constructor).&lt;/span&gt;
public  synchronized void prevYear() {
 int year = date.get(Calendar.YEAR)-BASEYEAR;
 date.set(Calendar.YEAR, year + BASEYEAR - 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the next year relative to the date already set(In constructor).&lt;/span&gt;
public synchronized void nextYear() {
 int year = date.get(Calendar.YEAR)-BASEYEAR;
 date.set(Calendar.YEAR, year + BASEYEAR + 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the prev month relative to the date already set(In constructor).&lt;/span&gt;
public synchronized void prevMon() {
int month = date.get(Calendar.MONTH);
date.set(Calendar.MONTH, month - 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the next month relative to the date already set(In constructor).&lt;/span&gt;
public synchronized void nextMon() {
 int month = date.get(Calendar.MONTH);
 date.set(Calendar.MONTH, month + 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the prev day relative to the date already set(In constructor).&lt;/span&gt;
public synchronized void prevDay() {
 int day = date.get(Calendar.DAY_OF_MONTH);
 date.set(Calendar.DAY_OF_MONTH, day - 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the next day relative to the date already set(In constructor).&lt;/span&gt;
public synchronized void nextDay() {
 int day = date.get(Calendar.DAY_OF_MONTH);
 date.set(Calendar.DAY_OF_MONTH, day + 1);
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//sets the current system date, in case it is needed.&lt;/span&gt;
public static synchronized Date currentSystemDate() {
 return new Date();
}

&lt;span style=&quot;color: #6aa84f;&quot;&gt;//returns the date, after all the year,month,day operation.&lt;/span&gt;
public  synchronized Date getDate() {
 return date.getTime();
}
}
&lt;/code&gt;
&lt;/pre&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/40220991344287740/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/40220991344287740' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/40220991344287740'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/40220991344287740'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/java-custom-calendar-date-utils.html' title='Date to calendar,date utils... a simple example'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-6725864763446099645</id><published>2009-10-13T13:58:00.000-07:00</published><updated>2009-10-14T15:43:35.358-07:00</updated><title type='text'>Log4j Custom Performance Logger.</title><content type='html'>If you are a java coder, logging is indispensable part of your life. Often in your life you may have been in a situation, where you need a special purpose logging.&lt;br /&gt;
&lt;br /&gt;
For example performance logging. If you have performance bottleneck in your application and if you suspect a few classes to be the culprit (or methods for that matter), then you may need a separate logger, that will log the performance of those classes. It will be in addition to the existing logging you have in your application.&lt;br /&gt;
&lt;br /&gt;
Performance logging is just an example, but in these special situations, you feel the need of a custom logger.&lt;br /&gt;
&lt;br /&gt;
Writing a custom logger is very simple, yet very helpful. &lt;br /&gt;
&lt;br /&gt;
I a hereby giving the example of a performance logger. It will note the START time, END time and the time difference in the form of a CSV. As you dont want this to mess with your main application logs, you will need a separate log appender for this(TimingStats in this example).&lt;br /&gt;
&lt;br /&gt;
Though it talks about performance logging, this code snippet can be modified to implement any kind of custom logger.&lt;br /&gt;
&lt;br /&gt;
PerformanceLogger.java&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;

public class PerformanceLogger {
 
 
 private static Logger logger = Logger.getLogger(PerformanceLogger.class);
 String methodName; 
 long startTime;
 long endTime;
 
 public PerformanceLogger() {
  super();
  logger.setLevel(Level.ERROR);
 }
 public PerformanceLogger(Level level) {
  super();
  
  logger.setLevel(level);
 }
public static boolean debug(){
 return logger.getLevel()==Level.DEBUG?true:false;
}

 public void writeTime(String operation){
  logger.debug(operation);
 }
 public void startMethod(String methodName){
  this.methodName = methodName;
  startTime = System.currentTimeMillis();
  writeTime(methodName+&quot;:START: ,&quot;+startTime+&quot;, milliseconds&quot;);
 }
 public void endMethod(String methodName) {
  this.methodName = methodName;
  endTime = System.currentTimeMillis();
  writeTime(methodName+&quot;:END: ,&quot;+endTime+&quot;, milliseconds&quot;);
  writeTime(methodName+&quot;:TIME TAKEN: ,&quot;+((endTime-startTime)/1000)+&quot;, seconds&quot;);
 }
 
}
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
log4j.xml&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
&amp;lt;appender name=&amp;quot;Your_Default_appender&amp;quot; /&amp;gt;
    &amp;lt;!-- this appender will be used by performance logger class --&amp;gt;
 &amp;lt;appender name=&amp;quot;TimingStats&amp;quot; class=&amp;quot;org.apache.log4j.RollingFileAppender&amp;quot;&amp;gt; 
    &amp;lt;param name=&amp;quot;File&amp;quot; value=&amp;quot;./logs/TimingStats.csv&amp;quot;/&amp;gt; 
    &amp;lt;param name=&amp;quot;Append&amp;quot; value=&amp;quot;false&amp;quot; /&amp;gt;
    &amp;lt;param name=&amp;quot;MaxFileSize&amp;quot; value=&amp;quot;10MB&amp;quot;/&amp;gt;
    &amp;lt;param name=&amp;quot;MaxBackupIndex&amp;quot; value=&amp;quot;1&amp;quot;/&amp;gt;
    &amp;lt;layout class=&amp;quot;org.apache.log4j.PatternLayout&amp;quot;&amp;gt; 
      &amp;lt;param name=&amp;quot;ConversionPattern&amp;quot; value=&amp;quot;%d , [%t] , %x , %-5p , (%F) , %m%n&amp;quot;/&amp;gt; 
    &amp;lt;/layout&amp;gt; 
   &amp;lt;/appender&amp;gt;
 
 
 &amp;lt;!-- All your loggin levels for classes would be here. --&amp;gt;
 
 &amp;lt;logger name=&amp;quot;some class&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;
    &amp;lt;level value=&amp;quot;DEBUG&amp;quot; /&amp;gt;
    &amp;lt;appender-ref ref=&amp;quot;Your_Default_appender&amp;quot; /&amp;gt;
  &amp;lt;/logger&amp;gt;

&amp;lt;!-- There will be a special declaration of the PerformanceLogger class. Note that it is not using the default appender. --&amp;gt;
  &amp;lt;logger name=&amp;quot;PerformanceLogger&amp;quot; additivity=&amp;quot;false&amp;quot;&amp;gt;
    &amp;lt;level value=&amp;quot;DEBUG&amp;quot; /&amp;gt;
    &amp;lt;appender-ref ref=&amp;quot;TimingStats&amp;quot; /&amp;gt;
  &amp;lt;/logger&amp;gt;
&lt;/code&gt;&lt;/pre&gt;&lt;br /&gt;
Put the following lines in the class you want to log the performance.&lt;br /&gt;
&lt;br /&gt;
&lt;b&gt;private static PerformanceLogger log = new PerformanceLogger(Level.DEBUG);&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
You can use a singleton pattern if you want. But that is another topic.&lt;br /&gt;
&lt;br /&gt;
At the start of any methos put&lt;br /&gt;
&lt;b&gt;log.startMethod(methodName);&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
At the end of any method put&lt;br /&gt;
&lt;b&gt;log.endMethod(methodName)&lt;/b&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
That should serve your purpose. If you see any issue with my code, please comment and help me improve</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/6725864763446099645/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/6725864763446099645' title='5 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/6725864763446099645'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/6725864763446099645'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/log4j-custom-performance-logger.html' title='Log4j Custom Performance Logger.'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>5</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-3064987376294926950</id><published>2009-10-12T17:13:00.000-07:00</published><updated>2009-10-12T17:13:47.561-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="cronjob"/><category scheme="http://www.blogger.com/atom/ns#" term="crontab"/><category scheme="http://www.blogger.com/atom/ns#" term="unix"/><title type='text'>Setting up a cron job in unix/linux</title><content type='html'>&lt;div&gt;&lt;b&gt;What is CRON?&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Cron is a time-based job scheduler in Unix-like computer operating systems. &#39;cron&#39; is short for Chronograph. It is a long running process that enables users to schedule jobs (commands or shell scripts) to run automatically at a certain time or date.&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;What are the common uses?&lt;/b&gt;&lt;/div&gt;&lt;div&gt;It is commonly used to perform system maintenance or administration. Though it can be used for all other practical purposes&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;What is CRONTAB?&lt;/b&gt;&lt;/div&gt;&lt;div&gt;Cron is driven by a crontab, a configuration file that specifies shell commands to run periodically on a given schedule.&lt;/div&gt;&lt;div&gt;&lt;b&gt;crontab -e&lt;/b&gt; (Edit your crontab file).&lt;/div&gt;&lt;div&gt;&lt;b&gt;crontab -l &lt;/b&gt;Show your crontab file.&lt;/div&gt;&lt;div&gt;&lt;b&gt;crontab -r&lt;/b&gt; Remove your crontab file.&lt;/div&gt;&lt;div&gt;&lt;b&gt;MAILTO&lt;/b&gt;=user@domain.com Emails the output to the specified address.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Each entry in a crontab file consists of six fields:&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;minute(s) hour(s) day(s) month(s) weekday(s) command(s)&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;The fields can be separated by spaces or tabs.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Field &amp;nbsp;Value &amp;nbsp;Description&amp;nbsp;&lt;/div&gt;&lt;div&gt;&lt;b&gt;minute&lt;/b&gt;(0-59) - The exact minute that the command sequence executes.&lt;/div&gt;&lt;div&gt;&lt;b&gt;hour&lt;/b&gt;(0-23) - The hour of the day that the command sequence executes.&lt;/div&gt;&lt;div&gt;&lt;b&gt;day&lt;/b&gt;(1-31) - The day of the month that the command sequence executes.&lt;/div&gt;&lt;div&gt;&lt;b&gt;month&lt;/b&gt;(1-12) - The month of the year that the command sequence executes.&lt;/div&gt;&lt;div&gt;&lt;b&gt;weekday&lt;/b&gt;(0-6) - The day of the week that the command sequence executes. Sunday=0, Monday = 1 and so on.&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;Example&lt;/b&gt;: Print &quot;Hello&quot; after every hour&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Sample cron job command:- &amp;nbsp;&lt;i&gt;&lt;span style=&quot;color: blue;&quot;&gt;* 1 * * * echo &quot;Hello&quot;&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;Sample cron job script:- &amp;nbsp;&lt;i&gt;&lt;span style=&quot;color: blue;&quot;&gt;* 1 * * * whatever_you_want.sh&lt;/span&gt;&lt;/i&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;b&gt;Steps to setup the above cron job:-&lt;/b&gt;&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;In you unix prompt fire &quot;crontab -e&quot; . It will open the crontab file in the default editor (Most commonly vi editor)&lt;/li&gt;
&lt;li&gt;Press &quot;i&quot; to change the mode to INSERT.&lt;/li&gt;
&lt;li&gt;At the end of the file, type in the above sample command and/or script.&lt;/li&gt;
&lt;li&gt;Wait for the stipulated time mentioned in the cronjob and then check your mail, you should be able to see the output (if any) there.&lt;/li&gt;
&lt;/ol&gt;&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Happy Scheduling&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;span style=&quot;font-family: Arial; font-size: small;&quot;&gt;&lt;span class=&quot;Apple-style-span&quot; style=&quot;font-size: 13px;&quot;&gt;&lt;/span&gt;&lt;/span&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/3064987376294926950/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/3064987376294926950' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/3064987376294926950'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/3064987376294926950'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/setting-up-cron-job-in-unixlinux.html' title='Setting up a cron job in unix/linux'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-2023681493756524266</id><published>2009-10-09T16:38:00.000-07:00</published><updated>2009-10-09T20:52:48.070-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="BigDecimal"/><category scheme="http://www.blogger.com/atom/ns#" term="Date"/><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="Utilities"/><title type='text'>Date and Big Decimal utilities</title><content type='html'>&lt;tr&gt; &lt;td valign=&quot;top&quot;&gt;&lt;div class=&quot;searchResultHeading&quot; style=&quot;color: #125a95;&quot;&gt;Problem  Description&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;freeText1 description&quot; height=&quot;100%&quot; id=&quot;problemDescription&quot;&gt;We need  various small utilities regardign numbers and dates. &lt;br /&gt;
This code snippet has  included some of the common conversions for convenience. &lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;
&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;&lt;div class=&quot;searchResultHeading&quot; style=&quot;color: #125a95;&quot;&gt;&lt;br /&gt;
Solution  Description&lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;div class=&quot;freeText1 description&quot; height=&quot;100%&quot; id=&quot;solutionDescription&quot;&gt;There  are two separate classes &quot;DateUtils&quot; and &quot;NumberUtils&quot;. MyMain class shows the  usage. &lt;br /&gt;
&lt;/div&gt;&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;
&lt;tr&gt;&lt;td style=&quot;width: 700px;&quot; valign=&quot;top&quot;&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;Code Snippet&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
NumberUtils.java&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.util.Locale;


public class NumberUtils {

&amp;nbsp;&amp;nbsp; &amp;nbsp;/**
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Method takes Object as parameter and returns decimal number.
&amp;nbsp;&amp;nbsp; &amp;nbsp; * if argument is float or double and contains tailing zeros
&amp;nbsp;&amp;nbsp; &amp;nbsp; * it removes them. If argument is float or double then no change in return type.
&amp;nbsp;&amp;nbsp; &amp;nbsp; * Change the Format of the Number by changing the String Pattern
&amp;nbsp;&amp;nbsp; &amp;nbsp; */
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static String changeToDecimalFormat(Object number) {

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BigDecimal bdNumber = new BigDecimal(number.toString());
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bdNumber = bdNumber.stripTrailingZeros(); &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; //Returns a BigDecimal with any trailing zero&#39;s removed
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;String pattern = &quot;###,##0.0###########&quot;; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;//To apply formatting when the number of digits in input equals the pattern
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;DecimalFormat newFormat = new DecimalFormat(pattern, new DecimalFormatSymbols(Locale.US));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return newFormat.format(bdNumber);

&amp;nbsp;&amp;nbsp; &amp;nbsp;}

&amp;nbsp;&amp;nbsp; &amp;nbsp;/* Method takes Object as parameter and removes commas from the parameter */
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static double removeCommasFromNumber(Object number) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;StringBuffer inputNo = new StringBuffer(number.toString());
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (inputNo.length() &amp;gt; 0) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;while (inputNo.indexOf(&quot;,&quot;) != -1) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;inputNo.deleteCharAt(inputNo.indexOf(&quot;,&quot;));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 0.0;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return Double.parseDouble(inputNo.toString());

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} catch (NumberFormatException e) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return 0.0;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;}

&amp;nbsp;&amp;nbsp; &amp;nbsp;/* Some times its required to have a fixed set of decimal places for a
&amp;nbsp;&amp;nbsp; &amp;nbsp; * number. We can set that by changing the precision number for a particular
&amp;nbsp;&amp;nbsp; &amp;nbsp; * input BigDecimal Input String
&amp;nbsp;&amp;nbsp; &amp;nbsp; */
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static String changeToRequiredDecimals(String bigDecimalString,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int precision) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;String newFormattedString = null;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;String afterDecimal = null;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (bigDecimalString == null || bigDecimalString.length() == 0) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return &quot;0.0&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (bigDecimalString.contains(&quot;.&quot;)) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;afterDecimal = bigDecimalString.substring(bigDecimalString
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;.indexOf(&quot;.&quot;) + 1);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int length = Math.abs((afterDecimal.length() - precision));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (afterDecimal.length() &amp;lt; precision) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = bigDecimalString;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; length; i++) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = newFormattedString + &quot;0&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else if (afterDecimal.length() &amp;gt; precision) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = bigDecimalString.substring(0,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;bigDecimalString.length() - length);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (precision == 0) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = newFormattedString.substring(0,
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString.indexOf(&quot;.&quot;));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = bigDecimalString;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} else {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;if (precision &amp;gt; 0)
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = bigDecimalString + &quot;.&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;else
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = bigDecimalString;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;for (int i = 0; i &amp;lt; precision; i++) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;newFormattedString = newFormattedString + &quot;0&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return newFormattedString;
&amp;nbsp;&amp;nbsp; &amp;nbsp;}

}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
DateUtils.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;


public class DateUtils {

&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Date getNthFromCurrentDate(int field, int nValue) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;Calendar current = Calendar.getInstance();
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;current.setLenient(true);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;current.add(field, nValue);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return current.getTime();
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Date convertStringToDate(String dateString, String pattern) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;SimpleDateFormat sdf = new SimpleDateFormat(pattern);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;try {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return sdf.parse(dateString);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;} catch (ParseException e) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return null;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static Date convertStringToTimestamp(String dateString, String pattern) {
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;return new Timestamp(convertStringToDate(dateString, pattern).getTime());
&amp;nbsp;&amp;nbsp; &amp;nbsp;}
}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
MyMain.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
import java.math.BigDecimal;


/**
&amp;nbsp;*
&amp;nbsp;*/
public class MyMain {

&amp;nbsp;&amp;nbsp; &amp;nbsp;/**
&amp;nbsp;&amp;nbsp; &amp;nbsp; *Method
&amp;nbsp;&amp;nbsp; &amp;nbsp; * @param args
&amp;nbsp;&amp;nbsp; &amp;nbsp; */
&amp;nbsp;&amp;nbsp; &amp;nbsp;public static void main(String[] args) {

&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(DateUtils.convertStringToDate(&quot;11.10.2010&quot;, &quot;dd.MM.yyyy&quot;));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(DateUtils.convertStringToTimestamp(&quot;11.10.2010&quot;, &quot;dd.MM.yyyy&quot;));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(DateUtils.getNthFromCurrentDate(1,2));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;int intVar = 10;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;double doubleVar = 10.504000;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;float floatVar = 343534534348.5687654F;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;String commaString = &quot;343,534,535,000.0&quot;;
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;BigDecimal bdNumber = new BigDecimal(&quot;1234.8765&quot;);
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(NumberUtils.changeToDecimalFormat(new Integer(intVar)));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(NumberUtils.changeToDecimalFormat(new Double(doubleVar)));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(NumberUtils.changeToDecimalFormat(new Float(floatVar)));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(NumberUtils.removeCommasFromNumber(commaString));
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; 
&amp;nbsp;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;System.out.println(NumberUtils.changeToRequiredDecimals(bdNumber.toString(), 8));

&amp;nbsp;&amp;nbsp; &amp;nbsp;}


}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/2023681493756524266/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/2023681493756524266' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/2023681493756524266'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/2023681493756524266'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/date-and-big-decimal-utilities.html' title='Date and Big Decimal utilities'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-7781392868226830537</id><published>2009-10-08T12:20:00.000-07:00</published><updated>2009-10-09T21:40:27.157-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="free"/><category scheme="http://www.blogger.com/atom/ns#" term="graph"/><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="jfreechart"/><category scheme="http://www.blogger.com/atom/ns#" term="Swing"/><title type='text'>Drawing multiple XY graphs using JFreechart</title><content type='html'>&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;&lt;div class=&quot;searchResultHeading&quot; style=&quot;color: #125a95;&quot;&gt;Problem  Description&lt;br /&gt;
&lt;/div&gt;There are  sometimes requirement to draw multiple plots against time in any java  application. JFreechart provides an excellent API for this purpose. But knowing  and remembering all the methods in this API is painfull and time consuming&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;tr&gt;&lt;td valign=&quot;top&quot;&gt;&lt;div class=&quot;searchResultHeading&quot; style=&quot;color: #125a95;&quot;&gt;&lt;br /&gt;
Solution  Description&lt;br /&gt;
&lt;/div&gt;My code  draws multiple X-Y charts using the data provided. The source comes with all the  jar files necessary for this purpose. It does two things.&lt;br /&gt;
&lt;div class=&quot;freeText1 description&quot; height=&quot;100%&quot; id=&quot;solutionDescription&quot;&gt;1) Saves the chart  in some specified location of local machine as JPG image. &lt;br /&gt;
2) Draws the chart  on the screen using Swing API.&lt;br /&gt;
&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;Dependencies&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;jfreechart-1.0.4.jar&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;span style=&quot;color: black;&quot;&gt;jcommon-1.0.9.jar&lt;/span&gt;&lt;/span&gt;&lt;br /&gt;
&lt;/div&gt;&lt;/td&gt;&lt;/tr&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;tr&gt;&lt;td style=&quot;width: 700px;&quot; valign=&quot;top&quot;&gt;&lt;span style=&quot;color: #125a95;&quot;&gt;&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;color: #125a95;&quot;&gt;Sample Code&lt;br /&gt;
&lt;/span&gt;&lt;br /&gt;
&lt;span style=&quot;background-color: #d9ead3;&quot;&gt;&lt;code&gt;&lt;/code&gt;&lt;/span&gt;&lt;code&gt;&lt;/code&gt;&lt;br /&gt;
MyMain.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;


package graph;


/**
* Helper class. Used to testing purpose
*
*/
public class Mymain {


/**
*
* @param args
*/
public static void main(String[] args) {
System.out.println(&quot;MAIN : START&quot;);
Graph graph = new Graph();
Value value;
Value localvalues[]= new Value[names.length];
graph.setNames(names);
for (int i=0;i
value = new Value();
value.setI(values[i]);
localvalues[i] = value;
}
graph.setValues(localvalues);
new MyGraphs().createChart(graph,GraphConstants.PERIOD_TYPE_MONTHLY);


System.out.println(&quot;MAIN : END&quot;);


}

protected final static Integer values[][] = {
{12,1,123,10,200,12,45,15},
{1,12,10,112,0,122,15},
{121,34,21,13,90,6,78},
{5,67,15,104,2,123,4}
};
protected final static String names[] = {
&quot;Report1&quot;,
&quot;Report2&quot;,
&quot;Report3&quot;,
&quot;Report4&quot;
};


}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
Graph.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;

package graph;


/**
*
*
*/
public class Graph {
String names[];
Value values[];
/**
*
* @return names
*/
public String[] getNames() {
return names;
}
/**
*
* @param names
*/
public void setNames(String[] names) {
this.names = names;
}
/**
*
* @return values
*/
public Value[] getValues() {
return values;
}
/**
*
* @param values
*/
public void setValues(Value[] values) {
this.values = values;
}


}

&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
GraphConstants.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
package graph;


/**


* Constant file


*


*/


public interface GraphConstants {





/**period type if weekly*/


public final int PERIOD_TYPE_WEEKLY = 0;


/**period type if monthly*/


public final int PERIOD_TYPE_MONTHLY = 1;


/**period type if monthly*/


public final int PERIOD_TYPE_QUATERLY = 2;






/** the location of the file to be stored*/


public static final String FILE_PATH = &quot;d:\\chart.jpg&quot;;





/**plotting details : width of x-axis*/


public static final int X_AXIS_WIDTH = 500;


/**plotting details : width of y-axis*/


public static final int Y_AXIS_WIDTH = 300;





/**graph name : This part should be moved to property file*/


public static final String GRAPH_NAME = &quot;Graph Name&quot;;


/**x-axis label : This part should be moved to property file*/


public static final String X_AXIS = &quot;X Axis&quot;;


/**y-axis label : This part should be moved to property file*/


public static final String Y_AXIS = &quot;Y Axis&quot;;


/** Sample data error. This part should be moved to property file*/


public static final String ERROR_MESSAGE = &quot;Name and values must be equal in number&quot;;





/** Null string*/


public static final String NULL = null;






}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
MyGraphs.java&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;pre style=&quot;background-color: #eeeeee; border: 1px dashed #999999; color: black; font-family: Andale Mono, Lucida Console, Monaco, fixed, monospace; font-size: 12px; line-height: 14px; overflow: auto; padding: 5px; width: 100%;&quot;&gt;&lt;code&gt;
package graph;


import java.awt.BorderLayout;


import java.io.File;


import java.io.IOException;


import javax.swing.JPanel;


import org.jfree.chart.ChartFactory;


import org.jfree.chart.ChartPanel;


import org.jfree.chart.ChartUtilities;


import org.jfree.chart.JFreeChart;


import org.jfree.chart.labels.XYToolTipGenerator;


import org.jfree.chart.plot.XYPlot;


import org.jfree.chart.renderer.xy.XYItemRenderer;


import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;


import org.jfree.data.time.Month;


import org.jfree.data.time.Quarter;


import org.jfree.data.time.RegularTimePeriod;


import org.jfree.data.time.TimeSeries;


import org.jfree.data.time.TimeSeriesCollection;


import org.jfree.data.time.Week;


import org.jfree.data.xy.XYDataset;


import org.jfree.ui.ApplicationFrame;


import org.jfree.ui.RefineryUtilities;










/**


* This class is responsible for creating the graph using the data provided.


*


*/


public class MyGraphs extends ApplicationFrame implements XYToolTipGenerator{








/**


*


*/


private static final long serialVersionUID = -4019769545419857920L;






/** JFreeChart */


private JFreeChart chart;





/** Time series collection */


private TimeSeriesCollection dataset;


/**


* no-arg constructor


*/


public MyGraphs(){


this(GraphConstants.GRAPH_NAME);


}


/**


*


* @param title


*/


public MyGraphs(String title){


super(title);


}


/**


* This method takes care of all the aspects of drawing the graph


* @param graph


* @param periodType


*/


protected void createChart(Graph graph,int periodType) {


dataset = new TimeSeriesCollection();






// Add series data from table model to chart


try{


addSeriesToChart(dataset, graph,periodType);






// Create chart


chart = ChartFactory.createTimeSeriesChart(GraphConstants.GRAPH_NAME,GraphConstants.X_AXIS,GraphConstants.Y_AXIS,


dataset, true, true, false);






XYPlot plot = (XYPlot) chart.getPlot();





//Configured the axis of the graph


configureAxis(plot);






//Configures the rendered


configureRenderer(plot);






//Saves the graph image at the mentioned file path


saveChart();






//Draws the chart on screen using java swing


drawChart();


}catch (IOException ioe){


ioe.printStackTrace();


}catch(Exception e){


e.printStackTrace();


}






}





/**


* saves the chart as a jpg image at specified location.


*/


private void saveChart()throws IOException{


ChartUtilities.saveChartAsJPEG(new File(GraphConstants.FILE_PATH), chart, GraphConstants.X_AXIS_WIDTH, GraphConstants.Y_AXIS_WIDTH);


}





/**


* creates the graph on screen, with the aid of the class XYChartDemo.


*/


private void drawChart(){


this.setSize(new java.awt.Dimension(GraphConstants.X_AXIS_WIDTH,GraphConstants.Y_AXIS_WIDTH));


ChartPanel chartPanel = new ChartPanel(chart);








// size


chartPanel.setPreferredSize(new java.awt.Dimension(GraphConstants.X_AXIS_WIDTH,GraphConstants.Y_AXIS_WIDTH));






final JPanel main = new JPanel(new BorderLayout());


final JPanel optionsPanel = new JPanel();






main.add(optionsPanel, BorderLayout.SOUTH);


main.add(chartPanel);


setContentPane(main);


RefineryUtilities.centerFrameOnScreen(this);


setVisible(true);






}


/**


*


* @param dataset


* @param graph


* @param periodType


* @throws Exception


*/


protected void addSeriesToChart(TimeSeriesCollection dataset,Graph graph,int periodType)throws Exception{





int count_from = 0;


int count_to = graph.getNames().length;


if (graph.getNames().length!= graph.getValues().length){


throw new Exception(GraphConstants.ERROR_MESSAGE);


}


for (int j = count_from;j


TimeSeries g = new TimeSeries(graph.getNames()[j], getTimePeriod(periodType).getClass());


RegularTimePeriod start = getTimePeriod(periodType);


for (int i = 0; i


start = start.previous();


double value = (graph.getValues()[j].getI()[i]).doubleValue();


g.add(start,value);


}


dataset.addSeries(g);


}


}


/**


* @param xyPlot


*/


protected void configureAxis(XYPlot plot){


// Make crosshair visible for Range and Domain axis.


plot.setDomainCrosshairVisible(true);


plot.setRangeCrosshairVisible(true);






// Set upper margin of Domain axis for proper display of Item label.


plot.getDomainAxis().setUpperMargin(.05);






// Set margin of range axis for proper display of Item label.


plot.getRangeAxis().setUpperMargin(0.15);


plot.getRangeAxis().setLowerMargin(0.15);


//plot.getRangeAxis().setAutoRange(true);


}





/**


*


* @param plot


*/


protected void configureRenderer(XYPlot plot){


XYItemRenderer r = plot.getRenderer();






if (r instanceof XYLineAndShapeRenderer) {


XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;


renderer.setBaseShapesVisible(true);


renderer.setBaseShapesFilled(true);


}


}


/**


*


* @param periodType


* @return


*/


protected RegularTimePeriod getTimePeriod(int periodType){


switch(periodType){


case GraphConstants.PERIOD_TYPE_WEEKLY:


return new Week();


case GraphConstants.PERIOD_TYPE_MONTHLY:


return new Month();


case GraphConstants.PERIOD_TYPE_QUATERLY:


return new Quarter();


default:


return new Month();


}


}





/**


* @param dataset


* @param series


* @param item


* @return tooltip


*/


public String generateToolTip(XYDataset dataset, int series, int item) {


String tooltip;


if(item == 0){


tooltip = GraphConstants.NULL;


}else{


tooltip = &quot; &quot;;


}


return tooltip;


}






}
&lt;/code&gt;
&lt;/pre&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;/td&gt;&lt;/tr&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/7781392868226830537/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/7781392868226830537' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7781392868226830537'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/7781392868226830537'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/drawing-multiple-xy-graphs-using.html' title='Drawing multiple XY graphs using JFreechart'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry><entry><id>tag:blogger.com,1999:blog-3892323058569646272.post-4724472159466381860</id><published>2009-10-07T20:40:00.000-07:00</published><updated>2009-10-12T00:07:02.341-07:00</updated><category scheme="http://www.blogger.com/atom/ns#" term="JAVA"/><category scheme="http://www.blogger.com/atom/ns#" term="STARTX"/><category scheme="http://www.blogger.com/atom/ns#" term="STARTXWIN"/><category scheme="http://www.blogger.com/atom/ns#" term="XHOST"/><category scheme="http://www.blogger.com/atom/ns#" term="XLIB"/><category scheme="http://www.blogger.com/atom/ns#" term="XWINDOWS"/><title type='text'>XWindows and Java</title><content type='html'>Often in our java code, we use AWT or SWING packages. The application runs fine in windows machines(usually development environments are in windows machines). But when we try to run those java applications in Linux/Unix box, we face problem in running. Because It uses the native graphics toolkits for rendering, but Linux/Unix doesn&#39;t have graphics toolkits installed.&lt;br /&gt;
&lt;div&gt;XWindows is a package, when installed, gives the Linux/Unix the graphics toolkit ability.&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;But in most practical cases, you are not the ROOT user of the box. So you have to raise a request to infrastructure team for installing the XWindows package. And they will ask you hundreds of questions like:&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;why you need it?&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;are you sure you need it? &lt;br /&gt;
&lt;/div&gt;&lt;div&gt;prove us, that XWindows will solve your problem etc etc...&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;You want to be very sure that installing XWindows will solve your problem. For this, you may follow these steps:&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;&lt;ol&gt;&lt;li&gt;CYGWIN is an application, using which you can simulate Linux like facilities in your windows machine. Install CYGWIN in your system from &lt;a href=&quot;http://www.cygwin.com/&quot;&gt;here&lt;/a&gt;. While installing, it will give you options regarding which packages you want to install. Make sure the X11 packages are all selected.&lt;/li&gt;
&lt;li&gt;Put your application jar into the remote machine, where your application is supposed to run.&lt;/li&gt;
&lt;li&gt;Open CYGWIN window and run &quot;&lt;b&gt;startxwin.bat&lt;/b&gt;&quot;. It will start the xserver and open a new window. Or you can directly open XWin Server from the start-menu under Cygwin-x.&lt;/li&gt;
&lt;li&gt;In the new window add the remote machine ip in the access control list. Using the command &quot;&lt;b&gt;xhost &lt;space&gt; &lt;ip&gt;&lt;/ip&gt;&lt;/space&gt;&lt;/b&gt;&quot;. &quot;&lt;b&gt;xhost &amp;nbsp;/remote machine ip/&amp;nbsp;&lt;remote ip=&quot;&quot; machine=&quot;&quot;&gt;&lt;/remote&gt;&lt;/b&gt;&quot;&lt;/li&gt;
&lt;li&gt;In the remote machine set the &lt;b&gt;DISPLAY &lt;/b&gt;property as: &quot;&lt;b&gt;export DISPLAY=/local machine ip/&lt;local ip=&quot;&quot; machine=&quot;&quot;&gt;&lt;your&gt;:0.0&lt;/your&gt;&lt;/local&gt;&lt;/b&gt;&quot; . By doing this, you are directing the remote machine, to use the display of you local machine for rendering.&lt;/li&gt;
&lt;li&gt;Now run the application in you remote machine. It should run fine.&lt;/li&gt;
&lt;/ol&gt;&lt;div&gt;Now that you are able to run your application using XWINDOWS, you can confidently go and command Infra team, that this is exactly what you want.&lt;br /&gt;
&lt;/div&gt;&lt;div&gt;If you face any issues, please post it here, I will try to help you out. &lt;br /&gt;
&lt;/div&gt;&lt;div&gt;Hope this makes your life easier. Because I had a hard time convincing my Infra team.&lt;br /&gt;
&lt;/div&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://diggintojava.blogspot.com/feeds/4724472159466381860/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment/fullpage/post/3892323058569646272/4724472159466381860' title='0 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/4724472159466381860'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3892323058569646272/posts/default/4724472159466381860'/><link rel='alternate' type='text/html' href='http://diggintojava.blogspot.com/2009/10/xwindows-and-java.html' title='XWindows and Java'/><author><name>hirak</name><uri>http://www.blogger.com/profile/17821010440803576743</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='32' height='24' src='//blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEg6vTCVfNsxkOWFLZXKNfDqDq6EPpsMfcrmjSp16kP4RG0hB3lWcp3B-xVxq8msPraUYmypqZ8OtXfALT7fqs6HM2bWBuwckzqnc0I3up-XxdlrR8z07b-_ivkl_vnwdg/s220/DSC00060.JPG'/></author><thr:total>0</thr:total></entry></feed>