Wednesday, March 21, 2012

How to set environment variables in Windows 7 for Java


  1. Open the Control Panel -> System or Security –> System; the same thing can be done by right-clicking on MyComputer and choosing Properties

System window in Windows 7
System window in Windows 7

2.   Choose Advanced System Settings option
System properties window in Windows
System properties window in Windows

3.   Choose the Environment Variables option
Environment variables in Windows
Environment variables in Windows

4.   In the System variables section it is selected New 5.   Define the variable name, JAVA_HOME and its value C:\Program Files\Java\jdk1.6.0_16 (for this example JDK version 1.6.0 was installed in C:\Program Files\Java\jdk1.6.0_16 folder; if needed, modify this value to reflect the real situation)
6. Insert a new system variable named, CLASSPATH and its value %JAVA_HOME%\jre\lib

New system variable
New system variable

7. For PATH, if it already exists, select it and choose the Edit option; in the editor add the value;%JAVA_HOME%\bin (the new values are separated by a semicolon from the existing ones) Testing the system variables is done by opening a new command prompt window (Start -> cmd) and trying to launch the compiler with the command:
C:\Users\Catalin>javac
Usage: javac
where possible options include:
  -g                         Generate all debugging info
...
or, by using next commands
C:\Users\Catalin>echo %CLASSPATH%
C:\Program Files\Java\jdk1.6.0_16\jre\lib
 
C:\Users\Catalin>echo %JAVA_HOME%
C:\Program Files\Java\jdk1.6.0_16
 
C:\Users\Catalin>echo %PATH%
8. Restart the computer in order to make your system aware of these changes (thanks to Ike for reminding me)

Configuring Apache in front of JBoss Application Server Using mod_jk in windows Environment


Introduction

Apache HTTP server is a highly reliable and proven open source/free web server. It is under development for over 15 years and is used extensively to host web sites. Since Apache is very popular and the source code is available, security experts are always looking for any vulnerability. This makes it one of the most secure web servers.
JBoss AS is a popular open source J2EE application server widely used for deploying web applications. JBoss is powered by Tomcat servlet container which can also work as a Web server. However in many production deployments, Apache is configured as the web server in front of the JBoss server. This is due to the extensive set of features/deployment options offered by Apache HTTP server.
This article provides a step by step guide on configuring Apache HTTP server as the Web server in front of the JBoss application server. This guide is written for Microsoft Windows systems (Windows XP, Windows 7, Windows Server 2008 etc.). However the steps mentioned are similar on a Linux based system.
The following diagram depicts a typical production setup of a high availability Web application built on J2EE technologies.
Apache - JBoss high availability configuration
A hardware load balancer in front ensures highly reliable load balancing. Apache web servers configured behind a hardware load balancer ensures availability of the web application even when one of the servers are taken down for maintenance. Apache servers balances the load among the Jboss applications servers via the mod_jk connector. The Jboss servers are configured in a cluster and hence when a server goes down, the subsequent user requests can be forwarded to a different server.

Why to Configure Apache HTTP server in front of JBoss Application Server?

Application servers are good at hosting Web applications, but they are not as good when it comes to serving static content, providing load balancing, URL rewriting, security etc. Following are some of the advantages of using Apache HTTP web server in front of an application server such as JBoss.
  • Apache is a high performance and highly secure Web server with advanced features. It is a proven server and as of 2010, it is run on over 100 million servers.
  • Apache comes with a lot of extension modules providing essential hosting features such as logging, virtual hosting, URL rewriting etc.
  • Apache can be used as a load balancer distributing load across multiple JBoss instances. Mod_jk connector supports advanced load balancing configurations.
  • A single Apache server instance can serve multiple domains using virtual hosting and at the same time they can all be serviced by different JBoss servers. This ensures process isolation for web applications hosted in each domain.

Installing JBoss Application Server

Download JBoss Application Server from here. For this example, I have downloaded JBoss 6.0.
After downloading the file, extract the zip file to a folder of your choice. From the Windows command line, change to the bin directory inside the JBoss installation and run the following command. This will start JBoss on the default HTTP port of 8080. The –b option is used to bind JBoss to all network interfaces.
run.bat -b 0.0.0.0
Verify that JBoss sever is up and running by accessing the default console application at the URL – http://localhost:8080/. Create a sample application and host it under the App1 context. Verify that your application is running by accessing the URL – http://localhost:8080/App1.

Installing Apache HTTP Server

Download Apache HTTP server 2.x version from here. For Windows, best option is to download the MSI installer. Double click on the MSI file to install Apache. By default Apache is installed on port 80 and it is configured as a Windows service. If you have trouble running Apache, check whether there are any other applications listening on port 80 (Skype is a usual culprit).

Apache to JBoss Request Routing – How It Works?

There are two different ways of configuring Apache to JBoss routing. One is to use the Tomcat connector available for Apache called the mod_jk connector. This is an Apache module specifically written by Tomcat team. This module routes the Apache to JBoss request via the AJP (Apache Jserv Protocol).
Second method is to use an Apache module called mod_proxy. This proxy can either use HTTP or AJP for routing request from Apache to JBoss. The advantage of mod_proxy is that it is available as a built in module in Apache 2.x versions.
Apache JBoss integration using AJP protocolWhen it comes to load balancing and failure detection, mod_jk is better than mod_proxy. The only disadvantage is the need to separately deploy  mod_jk module.
In either case, if the Apache connector is using AJP protocol, the AJP protocol listener must be enabled on the JBoss server. By default JBoss binds AJP service to the port 8009. Whenever a request is received by Apache, it looks at the AJP module configuration file(workers.properties) and if the request is intended for the JBoss server, the request is routed through AJP protocol to the port 8009(Please see the diagram above).

Configuring Apache to JBoss HTTP Request Routing Using Mod_Jk

To configure mod_jk integration, either download mod_jk source code or mod_jk binary for your platform from here. Copy the compiled or downloaded module to the modules directory of your Apache installation.
Now create a new file named mod-jk.conf inside apache/conf folder. Add the following content to this file. Please ensure that the name of the module (mod_jk-1.2.31-httpd-2.2.3.so) is changed to the actual module file copied to the modules directory.
LoadModule jk_module modules/mod_jk-1.2.31-httpd-2.2.3.so
JkWorkersFile conf/workers.properties
JkShmFile logs/mod_jk.shm
JkLogFile logs/mod_jk.log
JkLogLevel info
JkMount /App1/* worker1
The directive JkMount here ensures that all HTTP requests with context /App1/ are forwarded to the worker1 application server configured in workers.properties (see below).
Open httpd.conf in apache/conf folder and add the following line at the end of the file. This enables the mod-jk.conf file as an extension to the default configuration file.
Include conf/mod-jk.conf
Create a new file workers.properties inside apache/conf folder. Add the following content to the file. Note that the name of the JBoss node (worker1) corresponds to the JkMount configuration in mod-jk.conf file. The host in this case is given as localhost since I have the JBoss running on the same machine. In actual deployments replace localhost with the ip address/host name of the JBoss machine.
worker.list=worker1
worker.worker1.type=ajp13
worker.worker1.host=localhost
worker.worker1.port=8009
The above configuration indicates that there is a single application server machine available for handling requests in the localhost and it is available through AJP protocol via port 8009.
Restart apache and JBoss and access your Web application through Apache server using this URL – http://localhost:80/App1.

Tuesday, February 1, 2011

Project creation and configuration steps on Eclipse editor


Project creation and configuration steps on Eclipse editor
  1. Download the Eclipse installation file (in zip format) from following URL ‘http://www.eclipse.org/downloads’ for MS windows version.
  2. Unzip the downloaded zip file and get the eclipse folder.
  1. Under eclipse folder you will get eclipse executable file ‘eclipse.exe’
  1. Click on ‘eclipse.exe’ it will open the Eclipse Workspace Launcher.
  1. Click on ‘Browse…’ button and select a workspace for your project and click on ‘OK’ button, it will open the Eclipse editor. Close the welcome window of editor.
  1. Right click on left pan of editor window (package Explorer pan) and select ‘NewJava Project’
  1. It will lead you to ‘New Java Project’ Window. Enter a project name into ‘Project Name’ input box. Choose ‘Create project from existing source’ radio option and click on ‘Browse’ button.
It will open the below window from there you select your project base (in my case it is D:\stars\implementation\software\testing) and click on ‘OK’ button.

It will populate ‘Directory’ input box with code base location path.

Now click on ‘Next’ button of ‘New Java Project’ window. Following window will get appeared.

Now you need to change the value of ‘Default output folder’ input box and for the same click on ‘Browse…’ button and select classes folder within your project base (in my case path is ‘ stars/web-inf/classes’) and click OK button.

It will populate ‘Default output folder’ input box with selected value (in my case it is ‘stars/web-inf/classes’).

Now click on ‘Finish’ button, it will create a project in Eclipse with existing code base. That you can see in the left pan (Package Explorer pan) of Eclipse editor.
  1. Next you need to configure build path for the created project and for the same right click on created project and select ‘Properties’.
  1. It will open a window with created project name (in my case it is ‘Properties for stars’). Now click on ‘Java Build Path’ and see right pan of the opened window. Three is four buttons (Sources, Projects, Libraries and Order and Export).
  1. Click on ‘Source’ button it will show your source code folder (in our case it is ‘java’ folder that contains all java classes). Click on ‘Source’ button will show you the other projects that are already created in the same workspace. ‘Order and Export’ pan there you can arrange the content into order and also you can export them.
Click on ‘Libraries’ button and add required jars in your application’s build path for compilation and execution of your source code. You can see right side of ‘Libraries’ pan there are two buttons one is ‘Add Jars…’ and other is ‘Add External Jars…’. Through add jars option you can add your application jars that is present into lib folder to project build path and through add external jars option you can add external jars that is not part of your application.


So click on ‘Add Jars…’ button and select all required jars from lib folder and click OK button.

It will populate the ‘Libraries’ pan with selected jars.

Now click on ‘Add External Jars…’ button and select required external jars for your project from local directory.

Basically some jars for application execution is already bundled with application server (JRun) and those jars we are not generally keeping into the lib folder of application and for compilation of the application we add them as external jars.
  1. Now you can see all added jars into ‘Libraries’ pan of the opened window.
Next you click ‘OK’ button. It will add all the added jars into the project build path and will close the ‘Properties for stars’ window.
  1. Now clean the java compiled classes and recompiles it. For the same select ‘Projectclean’ and also check ‘Build automatically’ option.
  1. If ‘Problems’ pan is showing any errors, it mean compilation has failed. Click on error, it will open the java source code where error occurred.
  1. For opening a source code file into editor, just double click on it. It will get opened.
  1. If there is any error in the source file then editor will show it with red mark. Put mouse over it and see the cause of error. After fixing the error, press ‘Ctrl+s’ on your keyboard to compile the same file.
  1. Double click on the file name. Editor will expand the source code window in full screen and again double click on same it will return back to previous window.
  1. To create a new java class go to ‘starsNewclass’

It will open the ‘New Java Class’ and there you should enter the class name as well as package name for new class and click ’Finish’ button. It will create a new java class in the mentioned package. For other file type apart from classes do the same and choose other suitable option from ‘New’.

  1. For searching a word into source code press ‘Ctrl+F’.
  2. For searching a word in whole project press ‘Ctrl+H’.
  3. Select a method in a class and press ‘Ctrl+Shift+G’, it will show you all the classes from where this method has called.
  4. Select object or its method of other class and press ’Ctrl+Enter’, it will open that object’s source file in Eclipse editor.
  5. To plug-in the HTML Editor with Eclipse, download following jar ‘tk.eclipse.plugin.htmleditor_2.0.6.1.jar’ and place it into your Eclipse ‘plugins’ folder (in my case it is ‘D:\Program Files\eclipse\plugins’).