How to connect LDAP server using Spring LDAP framework (fully tested code)

Although Java JNDI provides API for LDAP operations, but using JNDI normally need a lot of low-level programming work. Spring LDAP framework is a good way to do LDAP in Java. In this step-by-step guide, you will see how to connect to LDAP server and then search a user’s name using user’s ID. Before you … Read more

Java connect MS SQL Server using windows authentication

To connect MS SQL Server using windows authentication, the first step is to setup ODBC. You can go to Control panel -> Administrative tools -> ODBC. Add a new DSN to connect MS SQL Server using windows authentication account following wizard setup. The second step is the similar with using SQL Server authentication. The only … Read more

An Entry Example of Log4j

The log4j can be configured both programmatically and externally using special configuration files. External configuration is most preferred, because to take effect it doesn’t require change in application code, recompilation, or redeployment. Configuration files can be XML files or Java property files that can be created and edited using any text editor or XML editor, respectively.

Read more

How to use java properties file?

For configuration purposes, using properties file is a good way of reusing. In this way, when the code is packaged to a jar file, other users can just put the different configurations in the config.properties file. The following is a simple example of using properties file. 1. create the file hierarchy like the following. Mainly … Read more

Convert java jar file to exe

By using eclipse export wizard, you can get executable jar files. But the jar file can be only launched by using command lines. Apparently, this is not a good option for most regular not-java-programmer users, especially if the program requires user’s input to proceed. We need a program which can be started by double clicking. … Read more

Java code: Open IE browser and close it

I want to refresh my blog on Sohu to increase the visitor count. The following is the code for open IE browser and then close it. This will be done every 5 seconds, and in one day the count should be able to increase 3600*24/5 = 17280. So if you open your computer 10 days,  the count will become 172800. That will be a nice number.

Read more

Using Java to read web page

The following is the code for reading html content of a web page. Using java to read web page is different from using Java to call IE to open web pages. The visitor count for a general blog can not increase in this way. To increase visitors count, you need a program that can call … Read more

2 Examples to Show How Java Exception Handling Works

There are 2 examples below. One shows all caller methods also need to handle exceptions thrown by the callee method. The other one shows the super class can be used to catch or handle subclass exceptions. Caller method must handle exceptions thrown by the callee method Here is a program which handles exceptions. Just test … Read more

Requirement Analysis for Centralized File Management System

The following is the requirement summary for building a new centralized file management system (FMS): 1. After merging the central FMS, each individual system will use web service to connect to it. And all files and related information will be put in a central place. 2. The central should have the following functions: Authenticate Upload … Read more

Using Alfresco PHP Web Service to upload files

The following is the PHP code for uploading files using Alfresco Web Service. I put some comments inside the code. If there is anything confuses you, please leave your comment and I hope I can help. if (isset($_SERVER["ALF_AVAILABLE"]) == false) { require_once "./alfresco_sdk/remote/Alfresco/Service/Repository.php"; require_once "./alfresco_sdk/remote/Alfresco/Service/Session.php"; require_once "./alfresco_sdk/remote/Alfresco/Service/SpacesStore.php"; require_once "./alfresco_sdk/remote/Alfresco/Service/ContentData.php"; } $repositoryUrl = "http://localhost:8080/alfresco/api"; $userName = … Read more

Declaration, Initialization and Scoping for Java

The following is a summary for showing Java declaration, initialization and scoping. Variables defined in try block are not visible in catch block. You cannot pass parameters when you implement an interface by an anonymous class. Constructors cannot return anything. Not even void. Every enum has a values() method that returns an array of all … Read more

Java Thread: an overriding example code

class A implements Runnable { public void run() { System.out.println(Thread.currentThread().getName()); } }   class B implements Runnable {   public void run() { new A().run(); new Thread(new A(), "name_thread2").run(); new Thread(new A(), "name_thread3").start(); } }   public class Main {   public static void main(String[] args) { new Thread(new B(), "name_thread1").start(); } }class A implements … Read more

Overriding vs. Overloading in Java

Overriding and Overloading are two very important concepts in Java. They are confusing for Java novice programmers. This post illustrates their differences by using two simple examples. 1. Definitions Overloading occurs when two or more methods in one class have the same method name but different parameters. Overriding means having two methods with the same … Read more

Diagram of Exception Hierarchy

In Java, exception can be checked or unchecked. They both fit into a class hierarchy. The following diagram shows Java Exception classes hierarchy. Red colored are checked exceptions. Any checked exceptions that may be thrown in a method must either be caught or declared in the method’s throws clause. Checked exceptions must be caught at … Read more