So you checked your process is running in Linux operating system and it's running find, by using ps command. But now you want to know, from how long process is running, What is start date of that process etc. Unfortunately PS command in Linux or any UNIX based operating system doesn't provide that information. But as said, UNIX has commands for everything, there is a nice UNIX tip, which you can use to check from how long a process is running. It’s been a long time, I have posted any UNIX or Linux command tutorial, after sharing some UNIX productivity tips . So I thought to share this nice little tip about finding runtime of a process in UNIX based systems e.g. Linux and Solaris. In this UNIX command tutorial, we will see step by step guide to find, since when a particular process is running in a server.
Tuesday, April 30, 2013
Monday, April 29, 2013
One of my reader was installing Eclipse in his Windows 7 x86 machine and emailed me about this error "A java Runtime Environment (JRE) or Java Development kit (JDK) must be available in order to run Eclipse. No Java virtual machine was found". Before getting into details and trying to find root cause and solution of Eclipse Java Virtual Machine not found error, let's see some background about Eclipse. Eclipse is a popular Java IDE, which assist on coding, debugging and running Java program, but key point is, Eclipse itself need Java to launch and run. By default Eclipse scan your PATH and look for any JRE, if it founds suitable JRE than it runs otherwise it throws "A java Runtime Environment (JRE) or Java Development kit (JDK) must be available in order to run Eclipse. No Java virtual machine was found". Now, In order to install JRE, you can either download JDK or a JRE from Oracle's Java download site. For Java programmers, I suggest to install JDK because it comes with bundled JRE. Once you install JDK, its good to set your JAVA_HOME environment variable, and subsequently include JAVA_HOME/bin into your PATH. JAVA_HOME contains "java" command line utility which is what required by Eclipse to run. Alternatively you can install JRE on your windows 7 machine, and include its bin directory into PATH that contains javaw command, which can also run Eclipse or any Java program. By the way there is subtle difference between java and javaw, which is worth knowing. You can get "No Java virtual machine was found" error during fresh install or reinstalling Eclipse, but root cause always lies in PATH which is quite obvious with error itself.
Friday, April 26, 2013
java.library.path is a System property, which is used by Java programming language, mostly JVM, to search native libraries, required by project. Similar to PATH and Classpath environment variable, java.library.path also includes a list of directory. When Java code loads a native library (a library or executable written in language like C, C++ or native code) using System.loadLibrary("name of library") method, java.library.path is scanned for specified library. If JVM doesn't found requested native library, then it throw java.lang.UnsatisfiedLinkError: no native library in java.library.path. Now some one may argue that, why does Java program should depend on native library, doesn't this make a Java application platform dependent? Well, he would be right, but there are situations, where most of code is already written in native languages, and you are just writing some Java code on top of that. In those scenario, instead of rewriting whole stuff, you tend to use native libraries. Sometimes a third party JAR (written in Java) depends upon some native components as well. If you have been using Tibco RV messaging, then you might know that Java API for Tibco RV depends upon several dll files, and until those files are present in java.library.path, you can not run those Java program successfully. Since most of us, run Java program from Eclipse IDE, it's important to know How to set java.library.path in Eclipse. In this Java tutorial, I will show you couple of ways to set java.library.path in Eclipse IDE, you can follow similar steps in other IDE like Netbeans or IntelliJ to set java.library.path
Thursday, April 25, 2013
Both valueOf and parseInt methods are used to convert String to Integer in Java, but there are subtle difference between them. If you look at code of valueOf() method, you will find that internally it calls parseInt() method to convert String to Integer, but it also maintains a pool of Integers from -128 to 127 and if requested integer is in pool, it returns object from pool. Which means two integer objects returned using valueOf() method can be same by equality operator. This caching of Immutable object, does help in reducing garbage and help garbage collector. Another difference between parseInt() and valueOf() method is there return type. valueOf() of java.lang.Integer returns an Integer object, while parseInt() method returns an int primitive. Though, after introducing Autoboxing in Java 1.5, this doesn't count as a difference, but it's worth knowing.
Tuesday, April 23, 2013
Java is one of the best programming language created ever, and I am not saying this, because I am a passionate Java developer, but Java has proved it in last 20 years. Two decades is a big time for any Programming language, and Java has gained strength every passing day. Though there are times, when Java development slows down, but Java has responded well. Earlier with ground breaking changes in form of Enum, Generics, and Autoboxing in Java 5, performance improvement with Java 6, and Google's choice of language for Android apps development, keeps Java as front line programming language. Many graduate often ask me, which is the best programming language, which language should we learn, shall I learn Java etc. Well, it depends upon definition of your best programming language, if it's popularity then obviously Java outscore every one, even C, which is there for almost 50 years. If it in terms of Job opportunities, again Java outscore every one. You can get tons of Jobs opportunity by learning Java programming language, you can develop core Java based server side application, J2EE web and enterprise applications, and can even go for Android based mobile application development. So if you are not coming from C and C++ background, and want to learn your first programming language, I will suggest to choose Java. In this article, I will share my list of reason, and Why you should learn Java programming and Why I think Java is best programming language created ever.
Monday, April 22, 2013
What is String args is used for or Why we use String arg in main method is a common doubt among Java beginners. First thing newbies exposed while learning Java programming language is writing HelloWorldprogram in Java, Though HelloWorld is smallest sort of program, it contains lot of useful information and concept for newbies Java programmers, one of them is main method and it's different attribute. Though I covered bit of String args, while discussing Why main is public static in Java, I thought to write this article to explain String args array in more detail. Let's see a simple Java HelloWorld again to understand more about String args in Java.
Friday, April 19, 2013
Sometimes we need to convert InputStream to byte array in Java, or you can say reading InputStream as byte array, In order to pass output to a method which accept byte array rather than InputStream. One popular example of this, I have seen is older version of Apache commons codec, while converting byte array to hex string. Though, later version of same library do provide an overloaded method, to accept InputStream. Java File API provides excellent support to read files like image, text as InputStream in Java program, but as I said, sometime you need need String or byte array, instead of InputStream . Earlier we have seen 5 ways to convert InputStream to String in Java , we can use some of the techniques from there while getting byte array from InputStream in Java. If you like to use Apache commons library, which I think you should, there is a utility class called IOUtils, which can be used to easily convert InputStream to byte array in Java. If you don't like using open source library for such kind of thinks, and like to write your own method, you can easily do so by using standard Java File API. In this Java tutorial we will see examples of both ways to convert InputStream to byte array in Java.
Tuesday, April 16, 2013
Abstract class and interface is very popular in any object oriented programming language or Java interview, and there are always one or more questions from this. Interface is more common, because of its popularity among designers but questions from abstract class also pops up now and than. Interview questions from abstract class is more common on junior level or you say under 2 years experience of Java programmers, while interface related questions are mostly asked on senior level Java interview e.g. 4 or 6 years of experience. They are mostly asked along with other Java design pattern questions, e.g. Decorator pattern or Factory pattern . Any way, in this article we will see mix of these interview questions from abstract class and interface. All questions has been asked in various Java interviews and difficulty level for these question is easy for most of Java developer. It’s mostly fact based questions, but some questions like difference between abstract class and interface in Java, and when to prefer abstract class over interface can be really tricky.
Monday, April 15, 2013
JSON array is a ordered collection of values, which are enclosed within brackets e.g.  and separated by comma. In this Java tutorial we will convert JSON Array to String array in Java and subsequently create JSON from Java String Array. This tutorial is similar to our last article in JSON about How to convert JSON object to Java object, instead of JSON object, here we will convert JSON array to String array or List in Java. As I said earlier, there are lot's of open source library out there which can help to parse JSON data format and we have already seen Jackson library in our last example. In this tutorial we will use GSON to parse JSON data format and create Java String array or List from JSON array representation. Given the popularity of JSON as lightweight alternative of XML to transfer data from Server to client in web applications, it's becoming imperative to know about JSON data format and parsing JSON string, much like parsing XML documents and knowing about different XML parsers e.g. DOM or SAX. Since Java application development is more about reusing existing library, then coding yourself, its important to know what is available, and which library other programmers are using. So far we have seen Jackson library, and here we will explore another one called GSon.
Friday, April 12, 2013
Puzzles, riddles, logical questions, and lateral thinking questions are integral part of any programming job interviews. I missed to include some puzzles, when I shared my list of top 30 programming interview questions earlier, and couple of my friends requested to share some puzzles as well. These puzzles and lateral thinking questions are mostly collected from various Java programming interviews on Investment banks, but they are equally useful to any programming job interviews. I have seen more puzzles and logical questions at junior level i.e. 2 to 4 years. These kinds of puzzles, if asked at the beginning of interview, can make or break the interview. It’s not easy to solve them, especially when you see them first time, but what is surely not to do is give up without try, even if you can't think of answer, try to show rational approach. Remember it's OK, if you don't answer any puzzle question, but your approach towards unknown problem get noticed. At the same time lateral thinking questions offers several answers, and you can give your own explanation as well, but make sure it must look genuine and seems logical. Puzzles and riddles also forms a big chunk of Microsoft and Google Interview questions as well, and they are some really nasty and tough to answer. Anyway, I have not provided answers of these puzzles and logical questions, so that you can at least give it a try. Immediate answers kill the purpose of puzzles and riddles, and it's fun to solve them by yourself. By the way, you can easily find answers of these puzzles using Google, as they have asked numerous times on various programming interviews on software and technology companies.
Thursday, April 11, 2013
Write a Java program to find if a number is odd or even is one of the basic programming exercise, and anyone will be happy to see this in actual Java Interviews, wouldn't you? By the way did I said easy, well there is a little twist there, you need to check odd and even without using modulus (%) or remainder operator in Java. Since many programmers, especially freshers are familiar with % operator, this nice little trick does put them into thinking mode, which is what interviewer wants. This question is on similar level of checking if a number is palindrome or not, if you have practiced such questions, it would be easy to find solution. Any way, now, it's your turn to show off how good are you with different operators in Java and how quickly you can think of alternative solutions. Well, there are couple of ways to check if a number is even or odd without using modulus operator. First one is, by using division operator, and second method is by using bitwise operator in Java. An even number is an integer number is totally divisible by 2 i.e. when you divide even number by 2, remainder is zero. While an odd number is an integer number, which is not multiple of two, if we divide odd number by 2, result would be fraction. If we focus on this definition, we always think about remainder operator, but we can still use division operator to check if number is even and odd, how? let's see.
Tuesday, April 9, 2013
How do I compare two enum in Java? Should I use == operator or equals() method? What is difference between comparing enum with == and equals() method are some of the tricky Java questions. Until you have solid knowledge of Enum in Java, It can be difficult to answer these question with confidence. By the way unlike comparing String in Java, you can use both == and equals() method to compare Enum, they will produce same result because equals() method of Java.lang.Enum internally uses == to compare enum in Java. Since every Enum in Java implicitly extends java.lang.Enum ,and since equals() method is declared final, there is no chance of overriding equals method in user defined enum. If you are not just checking whether two enum are equal or not, and rather interested in order of different instance of Enum, than you can use compareTo() method of enum to compare two enums. Java.lang.Enum implements Comparable interface and implements compareTo() method. Natural order of enum is defined by the order they are declared in Java code and same order is returned by ordinal() method.
Monday, April 8, 2013
Stack trace is very useful while debugging or troubleshooting any issue in Java. Thankfully Java provides couple of ways to get current stack trace of a Thread in Java, which can be really handy in debugging. When I was new to Java programming, and didn’t know how to remote debug a Java application, I used to put debug code as patch release, which uses classpath to pick debug code for printing stack trace and shows how a particular method is get called and that some time provide vital clue on missing or incorrect argument. Now, When I have couple of Java debugging tips at my sleeve, I still think knowledge of how to get current stack trace in Java for any Thread, or simply print stack trace from any point in code worth knowing. For those who are not very familiar with stack trace and wondering what is stack trace in Java, here is quick revision. Thread executes code in Java, they call methods, and when they call, they keep them in there stack memory. You can print that stack trace to find out, from where a particular method is get called in execution flow. This is especially useful if you are using lot of open source libraries, and don’t have control on all the code. One of the most familiar face of stack trace in Java is printing stack trace using Exception.printStackTrace(), when an Exception or Error is thrown in Java. In this Java tutorial, we will look at couple of ways to print and get current stack trace of a Thread in Java.
Friday, April 5, 2013
Spring Framework Tutorial - How to call Stored Procedures from Java using IN and OUT parameter example
Spring Framework provides excellent support to call stored procedures from Java application. In fact there are multiple ways to call stored procedure in Spring Framework, e.g. you can use one of the query() method from JdbcTemplate to call stored procedures, or you can extend abstract class StoredProcedure to call stored procedures from Java. In this Java Spring tutorial, we will see second approach to call stored procedure. It's more object oriented, but same time requires more coding. StoredProcedure class allows you to declare IN and OUT parameters and call stored procedure using its various execute() method, which has protected access and can only be called from sub class. I personally prefer to implement StoredProcedure class as Inner class, if its tied up with one of DAO Object, e.g. in this case it nicely fit inside EmployeeDAO. Then you can provide convenient method to wrap stored procedure calls. In order to demonstrate, how to call stored procedures from spring based application, we will first create a simple stored proc using MySQL database, as shown below.
Thursday, April 4, 2013
How to add, substract days in current date in Java | Increment, decrement date by days with example, tutorial
While working in Java projects, we often needs to increment or decrement date e.g. adding one days to current date to get tomorrow's date, subtracting one day to get yesterday's date etc. Since date in Java is maintained as long millisecond value, Sometime, programmer tend to add 24 hours as one day, which could be wrong if day falls on a day light saving time zone, where a day could be either 23 or 25 hour long. When you add or subtract days from date, other components' of date e.g. month and year must roll. In this Java date tutorial, we will see two ways to increment and decrement date in Java. One approach uses java.util.Calendar from JDK and other uses DateUtils class from Apache commons lang library. DateUtils class provides convenient addDays(Date, int days) method, which accept a date and number of days to add, you can subtract days by passing negative value. Similarly java.util.Calendar provides Calendar.add() method, which accept a calendar field, for adding days, you need to use Calendar.DAY_OF_MONTH. Similar to DateUtils, you can pass positive number to increment date, and negative integer to decrement date in Java.
Wednesday, April 3, 2013
SVN or Subversion is one of the popular source control system used in Java world for source code management. There are lot of source control system available e.g. Git, Perforce, CVS, Clearcase, VSS, but SVN has it’s own place among Java developer and open source communities. Knowledge of source control system is must for any professional programmer. New users of subversion often confused between three things trunk, tags and branches. Questions like, what is difference between a tag and a branch in subversion is frequently asked in various Java forums and chat rooms. In this article, we will see what is trunk, tags or branches in SVN and understand difference between trunk, tag and branch. In short, when you first upload your project to SVN it creates a trunk, It's analogous to trunk of tree. this trunk forms the main development line. When multiple developer work on different functionality of project they usually create branch from trunk and after successfully completing that functionality, they usually merge there changes to trunk. On the other hand tag is usually used to create read only snapshot of either trunk or branch, which has been released, for future use. You can think tag as stable snapshot of code at any point, and can be used to as backup or restore. Let's see difference between branch, trunk and tag more detail in next section.
Tuesday, April 2, 2013
Maximum heap size for 32 bit or 64 bit JVM looks easy to determine by looking at addressable memory space like 2^32 (4GB) for 32 bit JVM and 2^64 for 64 bit JVM. Confusion starts here because you can not really set 4GB as maximum heap size for 32 bit JVM using -Xmx JVM heap options. You will get could not create the Java virtual machine Invalid maximum heap size: -Xmx error. There could be many different reason why maximum heap space for JVM is less than there theoretical limit and vary from one operating system to other e.g. different in Windows, Linux and Solaris. I have seen couple of comments on my post 10 points on Java Heap Space regarding what is maximum heap space for Java or 32 bit JVM or 64 bit JVM and why Windows allows only upto 1.6G memory as maximum heap space etc. In this Java article, I have collected some of the frequently asked questions around maximum heap space on both 32 and 64 bit JVM and tried to explain them.
Monday, April 1, 2013
One part of unit testing a Java method is checking exception thrown by that method. A Java unit test should verify correct exception thrown in exceptional case and no exception should be thrown in normal case. In JUnit 3.XX, there was no direct support to pass or fail a test based upon exception thrown by a Java method. JUnit4 address this issue and provides an easy, and readable way to test exception thrown by a Java method. There are many situations when you need to test exception thrown by any method in Java. Classical example of this is testing API methods which should throw IllegalArgumentException if arguments passed to method are not matching to pre-conditions. In order to test exception thrown by any method in JUnit 4 you need to use @Test(expected=IllegalArgumentException.class) annotation. You can replace IllegalArgumentException.class with any other exception e.g. NullPointerException.class or ArithmeticException.class etc. Bottom line is that JUnit 4 will execute that test and check if method actually throws expected exception or not. If method throws expected exception, specified as "expected", than JUnit 4 will pass that test, but if method throws any other exception, or doesn't throw any exception than JUnit4 will fail that particular test.