All posts in category : java

java_logo_100

Java – Cron job to run a jar file

Quartz is good, but often times we just need a simple scheduler system to run a jar file periodically. On *unix system, you can use the build-in cron to schedule a scheduler job easily. In this example, we will show you how to create a cron job on *nix toContinue Reading

java-random-number

Java : Return a random item from a List

Normally, we are using the following ways to generate a random number in Java. 1. ThreadLocalRandom (JDK 1.7) //Generate number between 0-9 int index = ThreadLocalRandom.current().nextInt(10); 2. Random() //Generate number between 0-9 Random random = new Random(); int index = random.nextInt(10); 3. Math.random() //Generate number between 0-9 int index =Continue Reading

jsoup

jsoup : Send search query to Google

This example shows you how to use jsoup to send a search query to Google. Document doc = Jsoup .connect("https://www.google.com/search?q=mario"); .userAgent("Mozilla/5.0") .timeout(5000).get(); Unusual traffic from your computer network Don’t use this example to spam Google, you will get above message from Google, read this Google answer. 1. jsoup example ExampleContinue Reading

java_logo_100

Java – Read a file from resources folder

In this tutorial, we will show you how to read a file from a resources folder, in both Java and Unit Test environment. In simple, put files in a resources folder, and read the file with following code snippets : ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("file/test.xml").getFile()); 1.Continue Reading

java_logo_100

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(ClassLoader.java:643) at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) at java.net.URLClassLoader.defineClass(URLClassLoader.java:277) at java.net.URLClassLoader.access$000(URLClassLoader.java:73) at java.net.URLClassLoader$1.run(URLClassLoader.java:212) at java.security.AccessController.doPrivileged(Native Method) at java.net.URLClassLoader.findClass(URLClassLoader.java:205) atContinue Reading

java_logo_100

servlet-api-2.5.jar – jar not loaded

Deployed a “war” file on Tomcat, and hits following error messages : Jul 17, 2014 7:59:55 PM org.apache.catalina.loader.WebappClassLoader validateJarFile INFO: validateJarFile(D:\apache-tomcat-7.0.53\webapps\hc\WEB-INF\lib\servlet-api-2.5.jar) – jar not loaded. See Servlet Spec 3.0, section 10.7.2. Offending class: javax/servlet/Servlet.class Tools used : JDK1.7 Maven 3 Tomcat 7 1. Reason The Tomcat’s container comes with ownContinue Reading

DNS

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 mkyong.com   //… mkyong.com.Continue Reading

java_logo_100

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_logo_100

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 : TestApp.java package com.mkyong.test;Continue Reading

java_logo_100

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

apache_logo

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("http://mkyong.com");Continue Reading

google-Bot

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; +http://www.google.com/bot.html) or (rarely used): Googlebot/2.1 (+http://www.google.com/bot.html) Source : Google crawlers 1. Java Example In Java, you can get the “user agent” from HttpServletRequest. Example :Continue Reading

java_logo_100

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_logo_100

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-annotation

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

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