All posts in category : java

ascii-art-java

ASCII Art Java example

A funny Java example to create an ASCII art graphic. The concept is simple, get the image’s rgb color in “integer mode”, later, replace the color’s integer with ascii text. P.S This example is credit to this post and this post. ASCIIArt.java package com.mkyong;   import java.awt.Font; import java.awt.Graphics; importContinue Reading

java xml tutorials

How to count the depth of xml document (DOM example)

Note This question is from JNP forum, asking how to calculate the depth of the entire XML document. To get the depth of a XML, just loop the node recursively, and compare the level, that’s all.Here is a DOM parser example to count and show the deepest level of anContinue Reading

cvs-file

How to read and parse CSV file in Java

A comma-separated values (CSV) file is just a normal plain-text file, store data in column by column, and split it by a separator (e.g comma “,”). For example : US,United States MY,Malaysia In this tutorial, we show you how to read, parse and print out the values from a csvContinue Reading

world-country

Display a list of countries in Java

In this article, we show you how to use the Locale class to play around the list of countries. P.S Tested with JDK 1.6 1. List of Countries The Locale.getISOCountries() will return a list of all 2-letter country codes defined in ISO 3166. ListCountry.java package com.webmitta.model;   import java.util.Locale;  Continue Reading

301-redirect

Java HttpURLConnection follow redirect example

The HttpURLConnection‘s follow redirect is just an indicator, in fact it won’t help you to do the “real” http redirection, you still need to handle it manually. URL obj = new URL(url); HttpURLConnection conn = (HttpURLConnection) obj.openConnection(); conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully. HttpURLConnection.setFollowRedirects(true); 1. Java Http RedirectContinue Reading

java_logo_100

How to get Http Response Header in Java

This example shows you how to get the Http response header values in Java. 1. Get response header from url “mkyong.com” URL obj = new URL("http://mkyong.com"); URLConnection conn = obj.openConnection(); Map<String, List<String>> map = conn.getHeaderFields(); 2. Get key’s (“Server”) value from the headers. Map<String, List<String>> map = conn.getHeaderFields(); List<String> serverContinue Reading

apple-logo

Maven, JAVA_HOME is not defined correctly on Mac OSX

Apple recommends to set JAVA_HOME to “/usr/libexec/java_home“, for example : $ vim .bash_profile   export JAVA_HOME=/usr/libexec/java_home   $ source .bash_profile But, when execute the Maven command, it prompts following error messages : $ mvn -version Error: JAVA_HOME is not defined correctly. We cannot execute /usr/libexec/java_home/bin/java Solution To fixed it, updateContinue Reading

mongoDB-logo

Due to limitations of the BasicDBObject, you can’t add a second ‘$and’

Problem Using Spring data and Mongodb, below is a function to find data within a date range. public List<RequestAudit> findByIpAndDate(String ip, Date startDate, Date endDate) {   Query query = new Query( Criteria.where("ip").is(ip) .andOperator(Criteria.where("createdDate").gte(startDate)) .andOperator(Criteria.where("createdDate").lt(endDate)) );   return mongoOperation.find(query, RequestAudit.class);   } It hits following error message : org.springframework.data.mongodb.InvalidMongoDbApiUsageException: DueContinue Reading

apple-logo

How to Set $JAVA_HOME environment variable on Mac OS X

In this tutorial, we show you how to set $JAVA_HOME environment variable on latest or older Mac OSX. 1. Mac OSX 10.5 or later In Mac OSX 10.5 or later, Apple recommends to set the $JAVA_HOME variable to /usr/libexec/java_home, just export $JAVA_HOME in file ~/. bash_profile or ~/.profile. $ vimContinue Reading

default thumbnail

How to get client Ip Address in Java

Normally, you can use servletRequest.getRemoteAddr() to get the client’s IP address that’s accessing your Java web application. String ipAddress = request.getRemoteAddr(); But, if user is behind a proxy server or access your web server through a load balancer (for example, in cloud hosting), the above code will get the IPContinue Reading

eclipse-access-rules-error

Access restriction: The type BASE64Encoder is not accessible due to restriction

Problem Downloaded a Java sample from Alexa API on Amazon service, imports it into Eclipse, but unable to compile and hits following “Access restriction” errors : Access restriction: The type BASE64Encoder is not accessible due to restriction on required library /System/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Classes/classes.jar P.S Using JDK 1.6 and Eclipse IDE 4.2 importContinue Reading

time-date-different-in-Java

How to calculate date and time difference in Java

In this tutorial, we show you 2 examples to calculate date / time difference in Java : Manual time calculation. Joda time library. 1. Manual time calculation Converts Date in milliseconds (ms) and calculate the differences between two dates, with following rules : 1000 milliseconds = 1 second 60 secondsContinue Reading

alexa ranking

How to get Alexa Ranking In Java

In this example, we show you how to use Java and DOM XML parser to get the Alexa ranking from below the undocumented API : http://data.alexa.com/data?cli=10&url=domainName 1. Alexa API For example, type following URL in your browser : http://data.alexa.com/data?cli=10&url=mkyong.com Alexa will return back following XML result : <ALEXA VER="0.9" URL="mkyong.com/"Continue Reading

google pagerank

How to get Google PageRank (PR) in Java

In this example, we will show you how to get Google PageRank (PR) in Java. To request a PageRank for “mkyong.com”, you just need to send following HTTP request : http://toolbarqueries.google.com/tbr?client=navclient-auto&hl=en&ch=6236440745 &ie=UTF-8&oe=UTF-8&features=Rank&q=info:mkyong.com P.S Above URL is used by Google toolbar plugin. The tricky part is following hashing value : ch=6236440745Continue Reading

jsoup

jsoup HTML parser hello world examples

Jsoup, a HTML parser, its “jquery-like” and “regex” selector syntax is very easy to use and flexible enough to get whatever you want. Below are three examples to show you how to use Jsoup to get links, images, page title and “div” element content from a HTML page. Download jsoupContinue Reading

Page 1 of 1512345...10...Last »