Category Archives: Java

java httpurlconnection 403 but not on web browser

Today i face with a issue when i use java HttpUrlConnection to connect to a site i alway get http code 403. I finally over come that issue after loose a couple of hours. So i write down here  a post and hope can help much people on this prolem. When you search and come to this post i think you can quickly find out solution here.


Some sites will enable to reject when connect to by none browser tool and our connect using HttpUrlConnection violate this rule so the site do not allow to connect.


To solve this issue we can bypass the rule by setting agent header for our connect to those sites

URL url= new URL("site you need connect here");

HttpURLConnection hConnection = (HttpURLConnection) url.openConnection();

hConnection.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.11 (KHTML, like Gecko) Chrome/23.0.1271.95 Safari/537.11");



Maven to download dependencies

When you use maven to build and manage dependencies on a project and you want supply that project to other project without maven, i am sure you will get a question how i can copy all libraries this project depend on? In this post i will show you how to use maven to download dependencies.
To do that i using maven plugin Copying project dependencies. I add following lines to my pom.xml file

        <!--other plugin--->

With this config when we do maven -install command it will  copy all libraries that project is depended to target/lib folder.

org.hibernate.hql.ast.QuerySyntaxException: Class-name is not mapped

In this post i will guide you through how to solve below hibernate exception when using HQL

org.hibernate.hql.internal.ast.QuerySyntaxException: Class-name is not mapped

In my experience has many reasons for that exception. I will guide you step by step to check the issue

1. Check your hibernate config file hibernate.cgf.xml, do you have define that class yet or not in that file.  If you already define please go to next step

2. If you using hibernate mapping file, please naming for that file same as Class name.

3. If your persistence class define with annotation @Entity(name=’entity_name’) so please use that in HQL in case that you use unqualified name for example if your class is SystemParameter but you annotation @Entity(name=’param’) please use param name. Cause hibernate will use this alias name instead of class name.

4. If all above not work for you so please use full qualified name for example com.devjav.SystemParameter instead SystemParameter

Convert CIDR notation to a list of IP addresses

In this post i will show you how to get all IPs that valid base on CIDR notation.

I use open source Apache Commons Net to write sample to list all IPs.

 * devjav []
 * Copyright (C) 2014-2014 Pham Thai Thinh
package com.flocash;


 * @author Pham Thai Thinh
public class IPgenerate {

   * @param args
  public static void main(String[] args) {
    // TODO Auto-generated method stub
    String subnet = "";
    SubnetUtils utils = new SubnetUtils(subnet);
    String[] addresses = utils.getInfo().getAllAddresses();
    for (String ip : addresses) {




Spring security with custom authentication

This tutorial will show you how to use custom authentication with Spring security. When you do some real application you maybe need authentication application from other resource such as integration with other application, using REST Service, SOAP web service… In this post i will help you deal with that kind of authentication in general. Continue reading Spring security with custom authentication

Eclipse Web Application and Maven Dependency Issues

When development web application in eclipse and use maven to manage dependency we usually face with some problem as missing library at runtime cause maven dependency issues. In this post i show you how to dianostic that issue.
Step 1. Enable/disable Maven feature
On Eclipse IDE, right click on the project, choose maven->Disable Maven Nature
After that right click on project choose Configure-> Convert to maven project

If you already do this step but no luck, move to step 2
Step 2. Clean and update project
Some time you get exception cause library download by maven get corrupt cause network issue. Check log on console tab carefully to find what library have issue and go to maven local repository to delete that library(you can also delete all). Normal maven local repository lay on %USERPROFILE%/.m2
Next, In Eclipse IDE right click choose maven->update project


If your project still not work when run, go to next step.

Step 3. Checking Deployment Assembly
Right click on project in Eclipse IDE, choose properties and choose Deployment Assembly. Please make sure your project have minimal as below if you don’t using any other library in WEB-INF/lib. If you using more you can manuall add to Deployment Assembly by click Add button.


how to import certificate into java trust store

When using Java application to access server that using invalid certificate or self-signed certificate, you maybe will get below exception: PKIX path building failed: unable to find valid certification path to requested target

To make Java trust those certificate we must manual import that cert into java trust store. In this post i will guide you how to import certificate into trust store. In default Java lookup trust store in $JAVA_HOME/jre/lib/security/cacerts. So you must import certificate that you need to trust to that keystrore using keytool utility that ship with Java using below syntax

#move currency directory to $JAVA_HOME/jre/lib/security/cacerts
# default password of cacerts file is changeit
# in linux maybe you should not use -storepass option and enter direct on console
keytool -import -alias ca -file path_and_cert_name.cer -keystore cacerts –storepass changeit