utility

Usefull *nix Commands

This is my personal note for my most used *nix commands,
who know perhaps someday someone would find it usefull.

i use this to start apache tomcat, im using nohup command to keep my application running after i logged out.

nohup ./startup.sh 

i use this to kill an application using it’s process id

kill -9 <pid>

but how to find an application’s process id? Im using ps command for it. In this example im searching for “java” process id.

ps -ef | grep java

this is the command i use for checking whether a particular port is open or not. Im using port 8080 for example

netstat -aon | grep 8080

command for reading a rotating log file,

tail -f edw/modules/system.out

check my command history

history | grep java

im using these syntax to see my AIX peformance

topas –P

or

nmon

Setup Apache Tomcat and Ubuntu VM on Windows Azure

Recently i got a project which need me to deploy on a cloud infrastructure. After spending some time finding the right cloud provider, finally i choose Microsoft’s product Windows Azure. According to Wikipedia, Windows Azure is a Microsoft cloud computing platform used to build, host and scale web applications through Microsoft data centers.

Actually, i was a little suprised because Microsoft provided Ubuntu Server images for its VM OS selection. I tought the only OS available for Mcrosoft’s cloud platform would be from windows server family.

Okay, first i start by picking New Virtual Machine,and pick Ubuntu from list of Virtual Machines.

Next is selecting your VM name, username and password, and your virtual machine’s size, For this example, im trying a small size server.

Select your DNS name and region and your availability set. Finish your configuration, and start creating your Ubuntu VM on Azure.

Next is connect to your Ubuntu via SSH, and start downloading apache tomcat using this command.

sudo apt-get install tomcat7

It will install Apache Tomcat 7, and start your tomcat using this command,

sudo services tomcat7 start

check whether your Tomcat’s port (8080) has opened yet or not,

netstat -aon | grep 8080

Next is adding your tomcat port to your VM’s endpoints.

After youve add your endpoint, it’s time to test your tomcat server. Try accessing your public virtual ip address via browser and see whether your Tomcat Server is ready. 😉

Weird “Unparseable Date” Exception When Using Java’s SimpleDateFormat Class

I found this weird error at my production server, an error which somehow never show up at my testing environment.

java.text.ParseException: Unparseable date: "Thu Oct 11 00:00:00 ICT 2012"
        at java.text.DateFormat.parse(DateFormat.java:337)
        at DateClass.main(DateClass.java:7)

It seems that my application is unable to parse the string i provided. But, what makes me confuse is, i try with the same class on my laptop over and over again but this error is never show up. I tought it has something to do with the server’s JDK version or even the server’s operating system.

This is my java class that i use for testing purpose, it runs very well on both my laptop and development server, but it shows error on my server.

import java.text.SimpleDateFormat;
import java.util.Date;

public class DateClass {
    public static void main(String[] args) throws Exception {
        String date = "Thu Oct 11 00:00:00 ICT 2012";
        Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(date);
        System.out.println(tanggalku);
    }
}

After spend several minutes googling, i found out that it happen because of Java’s Date Localization. This is my workaround,

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DateClass {
    public static void main(String[] args) throws Exception {
        String date = "Thu Oct 11 00:00:00 ICT 2012";
        Date tanggalku = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy", Locale.US).parse(date);
        System.out.println(tanggalku);
    }
}

Another workaround, if you are using Apache Tomcat, is adding this configuration on your Tomcat’s server parameter.

-Duser.language=en 
-Duser.region=US

Hope it helps others 😉

Calling Java Serialized Object from JSP

This article is an answer for my simple assignment i created several weeks ago for my friend, Zainul Iman. Its about creating a simple web application that can persist and retrieve values by putting it values on a java object and serialized it on a plain text file.

Okay, so basically i only create 4 files. Only 1 java files, 2 jsp and 1 servlet. And only adding several lines on web.xml file.
First is a simple java bean to store my inserted value,

package com.edw.bean;

import java.io.Serializable;

public class Student implements Serializable{    
    private String name;
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }    
}

And a simple jsp file to serve as view layer, in here user can insert value which will be persist on java bean.

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Insert Value</title>
    </head>
    <body>
        <form method="post" action="StudentServlet">
        <table>
            <tr>
                <td>Insert Name : </td>
                <td><input type="text" name="name" /> </td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit" /></td>                
            </tr>
        </table>
        </form>
    </body>
</html>

This is my servlet class, in this class i handle the serialization of the inserted value into java object

package com.edw.servlet;

import com.edw.bean.Student;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class StudentServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String name = request.getParameter("name");
        Student student = new Student();
        student.setName(name);
		
        FileOutputStream fileOutputStream2 = new FileOutputStream(new File("D:\\test12.txt"), false);
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream2);
        objectOutputStream.writeObject(student);
        response.getWriter().println("Success....!!");
        response.getWriter().println("list of student ---> <a href=\"\">daftar.jsp</a>");
    }
}

And the serialized object will be read from “daftar.jsp” file,

<%@page import="com.edw.bean.Student"%>
<%@page import="java.io.ObjectInputStream"%>
<%@page import="java.io.File"%>
<%@page import="java.io.FileInputStream"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Daftar Nama</title>
    </head>
    <body>
        <h1>Registered Name is : </h1>
        <%
            try{
                FileInputStream fileInputStream = new FileInputStream(new File("D:\\test12.txt"));
                ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
                Student student = (Student)objectInputStream.readObject();
                out.print("<h1>"+student.getName()+"</h1>");
            }catch(Exception ex){                
                out.print(ex);
            }            
        %>
    </body>
</html>

Dont forget to register you servlet to web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>StudentServlet</servlet-name>
        <servlet-class>com.edw.servlet.StudentServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>StudentServlet</servlet-name>
        <url-pattern>/StudentServlet</url-pattern>
    </servlet-mapping>
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

Well it’s not too hard isnt it 😉