All posts in category : java


Java : Unsupported major.minor version 51.0

1. Problem Run a “jar” on Debian 7.5, and hits the following error messages : $ java -jar ripecrawler.jar Exception in thread "main" java.lang.UnsupportedClassVersionError: com/mkyong/whois/job/RipeCrawlJob : Unsupported major.minor version 51.0   at java.lang.ClassLoader.defineClass1(Native Method) at java.lang.ClassLoader.defineClass( at at at$000( at$ at Method) at atContinue Reading


Java – Get nameservers of a website

In this tutorial, we will show you how to get the nameservers of a website using Java + dig command. 1. Using Dig Command 1.1 On Linux, we can use dig command to query a DNS lookup of a website, for example : $ dig any   //… Reading


Find out your Java heap memory size

In this article, we will show you how to use the -XX:+PrintFlagsFinal to find out your heap size detail. In Java, the default and maximum heap size are allocated based on this – ergonomics algorithm. Heap sizes Initial heap size of 1/64 of physical memory up to 1Gbyte Maximum heapContinue Reading


Java – Append values into an Object[] array

In this example, we will show you how to append values in Object[] and int[] array. Object[] obj = new Object[] { "a", "b", "c" }; ArrayList<Object> newObj = new ArrayList<Object>(Arrays.asList(obj)); newObj.add("new value"); newObj.add("new value 2"); 1. Object[] Array Example Example to append values with ArrayList : package com.mkyong.test;Continue Reading


Java – Convert int[] to Integer[] example

Examples show you how to convert between int[] and its’ wrapper class Integer[]. 1. Convert int[] to Integer[] public static Integer[] toObject(int[] intArray) {   Integer[] result = new Integer[intArray.length]; for (int i = 0; i < intArray.length; i++) { result[i] = Integer.valueOf(intArray[i]); } return result; } 2. Convert Integer[]Continue Reading


The type DefaultHttpClient is deprecated

Eclipse IDE prompts warning on new DefaultHttpClient, mark this class as deprecated. package com.mkyong.web.controller;   import org.apache.http.Header; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.DefaultHttpClient;   public class WebCrawler {   public static void main(String[] args) throws Exception {   HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet("");Continue Reading


Java – Check if web request is from Google crawler

If a web request is coming from Google crawler or Google bot, the requested “user agent” should look similar like this : Mozilla/5.0 (compatible; Googlebot/2.1; + or (rarely used): Googlebot/2.1 (+ Source : Google crawlers 1. Java Example In Java, you can get the “user agent” from HttpServletRequest. Example :Continue Reading


How To Get HTTP Request Header In Java

This example shows you how to get the HTTP request headers in Java. To get the HTTP request headers, you need this class HttpServletRequest : 1. HttpServletRequest Examples Example 1.1 – Loop over the request header’s name and print out its value. import javax.servlet.http.HttpServletRequest;   //… private HttpServletRequest request;  Continue Reading


Java – Write directly to memory

You have been told a lot that you can’t manage memory in Java. Well, it has changed since HotSpot release of the Java VM. There is a way to directly allocate and deallocate memory as well as write and read it… Of course we are talking about the JVM memoryContinue Reading


Java Custom Annotations Example

In this tutorial, we will show you how to create two custom annotations – @Test and @TestInfo, to simulate a simple unit test framework. P.S This unit test example is inspired by this official Java annotation article. 1. @Test Annotation This @interface tells Java this is a custom annotation. Later,Continue Reading


Java – Convert IP address to Decimal Number

In this tutorial, we show you how to convert an IP address to its decimal equivalent in Java, and vice versa. For examples : <-> 4294967295 <-> 3232235778 1. IP Address to Decimal We show you two ways to convert an IP address to a decimal number NormalContinue Reading


Java and “& 0xFF” example

Before you understand what is & 0xFF, make sure you know following stuffs : Bitwise AND operator, link. Converts hex to/from binary, and decimal to/from binary. In short, & 0xFF is used to make sure you always get the last 8 bits. Let’s see an example to convert an IPContinue Reading


Java Date and Calendar examples

This tutorial shows you how to work with java.util.Date and java.util.Calendar. 1. Java Date Examples Few examples to work with Date APIs. Example 1.1 – Convert Date to String. SimpleDateFormat sdf = new SimpleDateFormat("dd/M/yyyy"); String date = sdf.format(new Date()); System.out.println(date); //15/10/2013 Example 1.2 – Convert String to Date. SimpleDateFormat sdfContinue Reading


Where to download Java JDK source code ?

The JDK source code is inside the, this article shows you how to get it on Windows, Ubuntu (Linux) and Mac OSX. 1. Windows In Windows, visit the Oracle website and download the JDK (not JRE). Install the JDK and the is inside the JDK installed folder, forContinue Reading


Java – How to calculate leap year

A leap year is a year containing one additional day (366 days a year). Review the leap year algorithm : if year is divisible by 400 then is_leap_year else if year is divisible by 100 then not_leap_year else if year is divisible by 4 then is_leap_year else not_leap_year P.S AlgorithmContinue Reading

Page 1 of 1712345...10...Last »