Sun Java in KUbuntu

That was not an obvious one. Under KUbuntu I typically use the KPackageKit application to install and update applications.  It did not work well. I tried to install the sun-java-jdk package many times and every time it would list the dependencies and do the install but the “java -version” would tell me that I was still using the OpenJDK.

After reading a few different posts in forums I tested the idea to use the synaptic package manager. The installation worked much better.

To finalize the install I ran a few commands at the cli:

sudo update-java-alternatives -l

This listed the fact that I now have the OpenJDK and Sun Java installed on my workstation.

sudo update-alternatives –config java

This allowed me to specify to use the Sun Java as my default JVM.

java -version

This now reports the Sun Java JRE as my JVM.

I can now use Maven to compile some test projects.

I guess there are still a few issues with the OpenJDK to be fully compatible (might just be some configuration that I am not aware of).

Java Decompiler

This is not the first time I do this and it is on the edge of embarrassing except this time I knew I had tools to get me out: lose the source code to a java application.

I have this application that was done a year ago and I guess that in the rush to get it to production I forgot to import the code in SVN.

A year later we have changed a few things and now I have to change a few lines in the code and add a method to it.

I remembered using a command line tool under Linux to decompile but since I could not remember the name of it I had to search for it. In the first few results of my google search for “java decompiler” came this site:

http://java.decompiler.free.fr/

I tried to install the JD-Eclipse but I don’t see how to use it so I simply downloaded and used the JD-Gui for Linux and it works beautifully. I can see my source code in the .class file in 2 seconds and now I can easily change it. This is so convenient.

New programming Style

Saw this article from a twit and I am unsure how I can do all these:

http://codemonkeyism.com/generation-java-programming-style/

Interesting and if I do any code with this I will post an other entry to let you know how it went.

Java MVC

I have to read a bit more on the subject but this article was interesting since it shows how to do in Java what Apple Cacao does with the MVC model. I find this twist to be easier to understand.

http://java.sun.com/developer/technicalArticles/javase/mvc/

Java Anti-Pattern

http://odi.ch/prog/design/newbies.php

This explains a few things that should be done differently if you want your application to perform well. Very interesting!

Java keytool quick notes

I have been playing with SSL connections between a Java application and a server lately and understanding the keytool command has helped troubleshooting. So here are my quick notes:

To list the content of a keystore you simply use this command:

keytool -v -list -keystore keystoreFileName

It is useful to remember that the default keystore is in the $JAVA_HOME/ jre/lib/security/cacerts file and that the default password for it is “changeit”.

I also used the printcert command to list the content of some certificate:

keytool -printcert -v -file entrust.cer

Swing Explorer

I was reading an article today about this piece of software and I taught that it was quite interesting. You can see all the parts of a swing application in this swing debugger. For someone like me that is not always sure how all the parts are linked together it is a quick visual view. Worth a try for sure.

Ref:

My first Servlet

Servlet programming has always been something I was curious about. Starting this project was a bit ackward since there is plenty of documentation but nothing to hold your hand in what I was looking at. Too much struggling convinced me to write down how to do it for a very simple servlet so I could go from there.I used Eclipsei 3.1 as my Java IDE and I use Tomcat 5.5.x as my Servlet Application Server.

  1. Creating a new project in Eclipse
    This should be a basic task that is quite easy. I called my new project HelloWorldServlet (great craetive title). I then created a new class named “HelloWorldServlet”. This opens the HelloWordlServlet.java file in the editor and I entered this source code in it:

    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    public class HelloWorldServlet extends HttpServlet
    {
        static final long serialVersionUID = 1;
    
        public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws IOException, ServletException
            {
                response.setContentType("text/html");
                PrintWriter out = response.getWriter();
                out.println("");
                out.println("");
                out.println("Hello World through a servlet!");
                out.println("");
                out.println("");
            }
    
    }
  2. Importing the servlet librarySomething that took me a little while to figure out is that I needed to add the servlet library from Tomcat to be able to properly compile but to also get the proper dynamic help in the Eclipse IDE. Under my working space directory I have a lib directory where I copy the common libraries that I use. The Tomcat servlet-api.jar was copied over there. You then simply right-click on your project in Eclipse, go to “Build Path” and click on “Add External Archives”. You simply browse to where you have copied servlet-api.jar and you now have the library imported.
  3. Unknown tidbitAfter adding this external archive it highlighted the fact that I need to have a “static final long serialVersionUID”. This is for the serialization of the object but I am not sure what value, if unique, it should have. I only added it as I was told without looking for too many answers right away.
  4. Compiled and copiedI compiled (ctrl-b) my servlet and copied it to my Tomcat box. Under the Tomcat default directory there is a webapps directory where you have to install your application. I made a new directory simply called “HelloWorld”. Under that directory I created a “WEB-INF” directory. Under the WEB-INF directory I created a “classes” directory where I copied my .class.

    The directory structure currently looks like this:

    tomcat
      +-->webapps
             +-->HelloWorld
                       +-->WEB-INF
                              +-->classes
                                         HelloWorldServlet.class
  5. Application declaration In the WEB-INF directory I had to create a web.xml file containing this: <br><?xml version=”1.0″ encoding=”ISO-8859-1″?><br><!DOCTYPE web-app<br> PUBLIC “-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN”<br> “http://java.sun.com/dtd/web-app_2_3.dtd”><br><web-app><br> <servlet><br> <servlet-name>HelloWorldServlet</servlet-name><br> <servlet-class>HelloWorldServlet</servlet-class><br> </servlet><br> <servlet-mapping><br> <servlet-name>HelloWorldServlet</servlet-name><br> <url-pattern>/HelloWorldServlet</url-pattern><br> </servlet-mapping><br></web-app><br>

    This file declares my servlet name, class name and URI. Since everything is the same here it is quite simple.

  6. Calling the applicationThe fun part (when it works) is calling the application and seeing the end result. Since I have already declared in my web.xml that the URI is HelloWordlServlet calling it in my browser was as simple as entering “http://serverName/HelloWordlServlet/”.
  7. A few more notesBecause I was not perfect on the first try I had to reload my application after each change I made to the class file or to the web.xml. Important to reload otherwise the cached version is what you keep seeing.