tag:blogger.com,1999:blog-74740953473177301822024-03-13T20:06:06.338-07:00JavaVirtues.comJames Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.comBlogger9125tag:blogger.com,1999:blog-7474095347317730182.post-91192157679468359452016-12-05T10:18:00.001-08:002016-12-05T10:18:15.172-08:00Installing Python on Linux without disrupting existing one<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
Python is important package that comes installed on Linux systems. Corrupting the pre-installed version makes many of the packages non-usable. I could not <i>yum</i> after the pre-installed version of Python was corrupt.<br />
<br />
Here is how you can install a version of Python on Linux operating system, keeping the pre-installed one safe. Here, we use CentOS 6.4 version, which is a pretty old release.<br />
<br />
<h3 style="text-align: left;">
Issues solved by this document</h3>
<i><b>pip</b> </i>is an important module you need while working on Python. Following are the errors I have faced while trying to install<span style="font-family: "courier new" , "courier" , monospace;"> </span><i><b><span style="font-family: inherit;">pip</span> </b></i>module and this document helps you in solving them too:<br />
<ul style="display: block; text-align: left;">
<li>
<h4 style="text-align: left;">
zipimport.ZipImportError: can't decompress data; zlib not available</h4>
<pre>[root@localhost tmp]# python2.7 get-pip.py
Traceback (most recent call last):
File "get-pip.py", line 20061, in <module>
main()
File "get-pip.py", line 194, in main
bootstrap(tmpdir=tmpdir)
File "get-pip.py", line 82, in bootstrap
import pip
zipimport.ZipImportError: can't decompress
data; zlib not available
</module></pre>
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLErU0nK2xM5F5Oc-h4yzItm8GHCPvIZKNsYtnFYb-oIdwJE1kRxo65TeY7O9hqKL-PZwiORkPhSjEa7VWkZ66VGLTAa5SWnUDKVnC0lo_cWCMjsLSjJANvRmP2glbVRdJMZAoZqkWhEI/s1600/pythongetpippy.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiLErU0nK2xM5F5Oc-h4yzItm8GHCPvIZKNsYtnFYb-oIdwJE1kRxo65TeY7O9hqKL-PZwiORkPhSjEa7VWkZ66VGLTAa5SWnUDKVnC0lo_cWCMjsLSjJANvRmP2glbVRdJMZAoZqkWhEI/s400/pythongetpippy.png" width="400" /></a>
</li>
<li><h4 style="text-align: left;">
There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available.</h4>
<pre>[root@localhost tmp]# python2.7 get-pip.py
pip is configured with locations that require TLS/SSL,
however the ssl module in Python is not available.
Collecting pip
Could not fetch URL https://pypi.python.org/simple/p
ip/: There was a problem confirming the ssl certificat
e: Can't connect to HTTPS URL because the SSL module i
s not available. - skipping
Could not find a version that satisfies the requirem
ent pip (from versions: )
No matching distribution found for pip
[root@localhost tmp]#
</pre>
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglOkS_T5xKsHz_BSgmKDArbUZG41FormJPmuojXHfl-o_E_EIeSj3Ffi7uS9_x796yHBFgT4jIhIILcYd6SwLOpROb2FgjPUfRDoePIoevhUOKYl-Gz4AeLRH-zh8KhXTJD909ZLa63gQ/s1600/Screenshot-pipinstallurl.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="264" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEglOkS_T5xKsHz_BSgmKDArbUZG41FormJPmuojXHfl-o_E_EIeSj3Ffi7uS9_x796yHBFgT4jIhIILcYd6SwLOpROb2FgjPUfRDoePIoevhUOKYl-Gz4AeLRH-zh8KhXTJD909ZLa63gQ/s400/Screenshot-pipinstallurl.png" width="400" /></a>
</li>
</ul>
<h3 style="text-align: left;">
Verify the current version</h3>
You may use <span style="font-family: "courier new" , "courier" , monospace;"><b>python --version</b></span> command on the terminal to find the current version.
<br />
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu_JnL8qxOfe0JeNSSllrPBVXFkqgXON35KRlqgcrqbtJNGjM-5gXx8AZ_9RuDBkSiDAgr0PmaVjYa1EX5HN5MPq6oMf3Y79pRTLpNalSxuJ-ixtNEVH5CtM5S3qVVbJjkBK3BBGs_72A/s1600/python_version.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="60" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhu_JnL8qxOfe0JeNSSllrPBVXFkqgXON35KRlqgcrqbtJNGjM-5gXx8AZ_9RuDBkSiDAgr0PmaVjYa1EX5HN5MPq6oMf3Y79pRTLpNalSxuJ-ixtNEVH5CtM5S3qVVbJjkBK3BBGs_72A/s320/python_version.png" width="320" /></a></div>
<h3 style="text-align: left;">
Installing a different version</h3>
<ol style="text-align: left;">
<li>You may download a version you would need to install from <a href="https://www.python.org/downloads" target="_blank">Python Download</a>. By default, Python comes as tarball for Linux.</li>
<li>Once you have the tarball downloaded, copy it to a location where we extract and do the installation. Here, I copied the 2.7.12 version to <span style="font-family: "courier new" , "courier" , monospace;">/tmp</span><span style="font-family: inherit;">. Use the version which is available for your platform and remember to use the commands accordingly.</span></li>
<li><span style="font-family: inherit;">Extract tarball and change directory to the extracted folder.</span><blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">tar xvf Python-2.7.12.tar.xz<br />cd Python-2.7.12</span></blockquote>
</li>
<li><span style="font-family: inherit;">Install required Linux packages. <b><i>gcc</i></b> is a utility for compiling and installing Python from source, <b><i>zlib-devel</i></b> and <b><i>openssl-devel</i></b> are required to avoid errors while installing <b><i>pip</i></b> module at a later stage. You would need to be </span><span style="font-family: "courier new" , "courier" , monospace;">root</span><span style="font-family: inherit;"> or a </span><span style="font-family: "courier new" , "courier" , monospace;">sudo</span><span style="font-family: inherit;">er to install these packages:</span><blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">yum install gcc<br />yum install zlib-devel<br />yum install openssl-devel</span></blockquote>
</li>
<li>Run configure with option to export shared libraries. This is necessary if you would need to use <b><i>pyinstaller</i></b> module.<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">./configure --enable-shared</span></blockquote>
</li>
<li>Generate the build:<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">make</span></blockquote>
</li>
<li>Install Python without replacing the existing version. Using <b><span style="font-family: "courier new" , "courier" , monospace;">altinstall</span> </b>option will help you to use the new installation with version suffix during execution. e.g. <span style="font-family: "courier new" , "courier" , monospace;">python2.7 --version</span><span style="font-family: inherit;">:</span><br />
<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">make altinstall</span></blockquote>
</li>
<li>Make a link to Python library path:</li>
</ol>
<blockquote class="tr_bq">
<ul style="text-align: left;">
<li>On 32 bit OS<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib/libpython2.7.so.1.0</span></blockquote>
</li>
<li>On 64 bit OS<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">ln -s /usr/local/lib/libpython2.7.so.1.0 /usr/lib64/libpython2.7.so.1.0</span></blockquote>
</li>
</ul>
<h3 style="text-align: left;">
Installing pip module</h3>
<div>
Here is how you may get <span style="font-family: "courier new" , "courier" , monospace;"><b>pip</b></span> for the Python version you have just installed:</div>
<div>
<ol style="text-align: left;">
<li>Running <span style="font-family: "courier new" , "courier" , monospace;"><b>get-pip.py</b></span> is a better way to install <span style="font-family: "courier new" , "courier" , monospace;">pip</span>. <span style="font-family: "courier new" , "courier" , monospace;">get-pip.py</span> file is available at <a href="https://pip.pypa.io/en/stable/installing/" target="_blank">pip module</a> listing.</li>
<li>Run <span style="font-family: "courier new" , "courier" , monospace;">get-pip.py</span> with the version of Python you have installed. For example:<blockquote class="tr_bq">
<span style="font-family: "courier new" , "courier" , monospace;">python2.7 get-pip.py</span></blockquote>
This installs a <b><span style="font-family: "courier new" , "courier" , monospace;">pip </span></b>utility which you can use with suffix. e.g. <span style="font-family: "courier new" , "courier" , monospace;">pip2.7 install pyinstaller</span> <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGIWrao4CQeVgWZ3_3qhYCsUiRX4tAiDB8AQ3RHs1-lzSoG11sdfv3d_Dy7EagVRdbcDFU0CkuuP_ohVfQelXvjE-f3ctLXWoIEIUDjg-A-TV0vjYEL2__qOtI6EOPRygRryRuRydkpNg/s1600/Screenshot-python-pyinstaller.png" imageanchor="1" style="clear: left; float: left; margin-bottom: 1em; margin-right: 1em;"><img border="0" height="263" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjGIWrao4CQeVgWZ3_3qhYCsUiRX4tAiDB8AQ3RHs1-lzSoG11sdfv3d_Dy7EagVRdbcDFU0CkuuP_ohVfQelXvjE-f3ctLXWoIEIUDjg-A-TV0vjYEL2__qOtI6EOPRygRryRuRydkpNg/s400/Screenshot-python-pyinstaller.png" width="400" /></a></div>
</li>
</ol>
</div>
</blockquote>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com33tag:blogger.com,1999:blog-7474095347317730182.post-69058267706824647802016-02-11T07:53:00.004-08:002016-02-11T07:56:01.815-08:00Error: Failed to deploy OVF package: File ds:///*_deviceImage-0.iso was not found<div dir="ltr" style="text-align: left;" trbidi="on">
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSiFX8FFrec8wCcemQOLGkqwS9n3leB08OyTLt3clYCMx6EGr8Q5MPHDO5NhwNJal58_8aokUxlbdBiPRwKQ56jUzpPKx74FHy3VX-Z2ndKB2cf6JKa2W0BfLTFW8b1LdHeKAiY_IlKfI/s1600/VM_DeployError.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="98" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgSiFX8FFrec8wCcemQOLGkqwS9n3leB08OyTLt3clYCMx6EGr8Q5MPHDO5NhwNJal58_8aokUxlbdBiPRwKQ56jUzpPKx74FHy3VX-Z2ndKB2cf6JKa2W0BfLTFW8b1LdHeKAiY_IlKfI/s320/VM_DeployError.png" width="320" /></a></div>
<br />
<br />
This killer error would be enough to eat your productive time. Going through the forums does help you to fix this but the following are to be noted:<br />
<br />
<br />
<ol style="text-align: left;">
<li>OVA files are tar archive files with a <span style="font-family: "courier new" , "courier" , monospace;">.vmdk</span>, <span style="font-family: "courier new" , "courier" , monospace;">.mf</span> and <span style="font-family: "courier new" , "courier" , monospace;">.ovf</span> files inside it.</li>
<li>The files inside the OVA archives should be in specific order.</li>
</ol>
<div>
Here is what I had tried with no success:</div>
<div>
<ol style="text-align: left;">
<li>Unzipping the content, editing the respective files, zipping it again in <span style="font-family: "courier new" , "courier" , monospace;">.zip</span> format and renaming to <span style="font-family: "courier new" , "courier" , monospace;">.ova</span>. Used winrar tool for this. This may throw error "header not found".</li>
<li>Using 7zip to extract the files, edit the required files and drag-drop updated files to 7zip. Just because 7zip utility changes the order of files in the archive, it gave error "<span style="font-family: "courier new" , "courier" , monospace;">.ovf</span> file not found in the beginning of archive."</li>
</ol>
<div>
<b>Solution:</b></div>
</div>
<div>
<b><br /></b></div>
<div>
Following steps are executed on a Linux machine. You may use tools such as cygwin to perform them on Windows.</div>
<div>
<ol style="text-align: left;">
<li>Rename the <span style="font-family: "courier new" , "courier" , monospace;">.ova</span> file to <span style="font-family: "courier new" , "courier" , monospace;">.tar</span>: <span style="font-family: "courier new" , "courier" , monospace;">mv myovafile.ova myovafile.tar</span></li>
<li>List the <span style="font-family: "courier new" , "courier" , monospace;">.tar</span> file to note down the order of its contents: <span style="font-family: "courier new" , "courier" , monospace;">tar -tf myovafile.tar</span></li>
<li>Extract the tar archive: <span style="font-family: "courier new" , "courier" , monospace;">tar -xvf myovafile.tar</span></li>
<li>Edit the <span style="font-family: "courier new" , "courier" , monospace;">.ovf</span> file using text editor to have <span style="font-family: "courier new" , "courier" , monospace;">vmware.cdrom.remotepassthrough</span> as <span style="font-family: "courier new" , "courier" , monospace;">rasd:ResourceSubType</span> content for the <span style="font-family: "courier new" , "courier" , monospace;">rasd:ElementName</span> with CD-ROM DVD value. You may refer <a href="http://kb.vmware.com/kb/2034422" target="_blank">VMware KB document</a>.</li>
<li>Calculate SHA1 for the <span style="font-family: "courier new" , "courier" , monospace;">.ovf</span> file: <span style="font-family: "courier new" , "courier" , monospace;">sha1sum myovafile.ovf</span></li>
<li>Update the SHA1 value to the <span style="font-family: "courier new" , "courier" , monospace;">.mf</span> file using a text editor.</li>
<li>Create tar file with the updated files. Pass files in the order they was displayed when step 2 was performed: <span style="font-family: "courier new" , "courier" , monospace;">tar -cvf mynewova.tar {fileslist_separated_by_space}</span></li>
<li>Rename the file to have OVA extension: <span style="font-family: "courier new" , "courier" , monospace;">mv mynewova.tar mynewova.ova</span></li>
</ol>
<div>
<br /></div>
</div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com6tag:blogger.com,1999:blog-7474095347317730182.post-16902436534511232582015-07-14T05:16:00.000-07:002015-07-15T22:09:52.498-07:00Swapping Columns of CSV Row<div dir="ltr" style="text-align: left;" trbidi="on">
Swapping column values of CSV rows efficiently is not a simple piece of code, especially when it comes to processing of millions of records. Processing on String values consumes more CPU cycles when compared to char arrays. When char array representation of a String is easily available, let us try to make use of it for the swap purpose.<br />
<div>
<br /></div>
<div>
This sample code is written for a requirement where I had to swap two column values:</div>
<div>
<pre style="background: #ffffff; color: black;"><span style="color: #008c00;">6</span><span style="color: #808030;">,</span><span style="color: #008c00;">255</span><span style="color: #808030;">,</span>00aecd<span style="color: #808030;">,</span>00ebcb00<span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">28356</span><span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">417</span><span style="color: #808030;">,</span><span style="color: #008c00;">1</span></pre>
had to be converted to
<br />
<pre style="background: #ffffff; color: black;"><span style="color: #008c00;">6</span><span style="color: #808030;">,</span><span style="color: #008c00;">255</span><span style="color: #808030;">,</span>00ebcb00<span style="color: #808030;">,</span>00aecd<span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">28356</span><span style="color: #808030;">,</span><span style="color: #008c00;">0</span><span style="color: #808030;">,</span><span style="color: #008c00;">417</span><span style="color: #808030;">,</span><span style="color: #008c00;">1</span>
</pre>
i.e. to swap columns 2 and 3, if columns are numbered from 0.<br />
<br />
Simple approach towards this is to split the line with delimiter, swap values in the respective array and join with delimiter again. This would be an expensive operation when you have millions of lines to process.<br />
<br />
Other way of handling this operation is to get char array of the String, locate the starting and ending points of columns to be swapped and do array operation to move the characters.<br />
<br />
<h2 style="text-align: left;">
<b>Approach 1 (Simple and Expensive)</b></h2>
<br />
<pre class="java" name="code"> private static String swapColumns(final String value,
final String separator, final int swapColumn1, final int swapColumn2) {
String[] splitValues = value.split(separator);
String temp = splitValues[swapColumn1];
splitValues[swapColumn1] = splitValues[swapColumn2];
splitValues[swapColumn2] = temp;
StringBuilder stringBuilder = new StringBuilder();
boolean itemAdded = false;
for (String splitValue : splitValues) {
if (itemAdded) {
stringBuilder.append(separator);
}
stringBuilder.append(splitValue);
itemAdded = true;
}
return stringBuilder.toString();
}
</pre>
This code is available for download <a href="https://drive.google.com/file/d/0B3eOZZonu0OzUnpOY2tyS1ZsV00/view?usp=sharing" target="_blank">here</a>.<br />
<br />
<h2 style="text-align: left;">
<b>Approach 2 (char array)</b></h2>
<br />
<pre class="java" name="code"> private static String swapColumns(final String value, final char separator,
final int swapColumn1, final int swapColumn2) {
char[] charValues = value.toCharArray();
int swapColumn1StartIndex = swapColumn1 == 0 ? 0 : -1;
int swapColumn1EndIndex = -1;
int swapColumn2StartIndex = -1;
int swapColumn2EndIndex = -1;
int i = 0;
int separatorOccurance = 0;
for (; i < charValues.length; i++) {
if (charValues[i] == separator) {
separatorOccurance++;
if (swapColumn1StartIndex > -1) {
swapColumn1EndIndex = i;
break;
} else if (swapColumn1 == separatorOccurance) {
swapColumn1StartIndex = i + 1;
}
}
}
swapColumn2StartIndex = swapColumn2 == separatorOccurance
? i + 1
: swapColumn2StartIndex;
for (i++; i < charValues.length; i++) {
if (charValues[i] == separator || i + 1 == charValues.length) {
separatorOccurance++;
if (swapColumn2StartIndex > -1) {
swapColumn2EndIndex = i
+ ((i + 1 == charValues.length) ? 1 : 0);
break;
} else if (swapColumn2 == separatorOccurance) {
swapColumn2StartIndex = i + 1;
}
}
}
if (swapColumn1EndIndex - swapColumn1StartIndex > swapColumn2EndIndex
- swapColumn2StartIndex
|| swapColumn1EndIndex - swapColumn1StartIndex == swapColumn2EndIndex
- swapColumn2StartIndex) {
char[] tempArray = new char[swapColumn1EndIndex
- swapColumn1StartIndex];
System.arraycopy(charValues, swapColumn1StartIndex, tempArray, 0,
tempArray.length);
System.arraycopy(charValues, swapColumn2StartIndex, charValues,
swapColumn1StartIndex, swapColumn2EndIndex
- swapColumn2StartIndex);
System.arraycopy(charValues, swapColumn1EndIndex, charValues,
swapColumn1EndIndex - swapColumn1EndIndex
+ swapColumn1StartIndex + swapColumn2EndIndex
- swapColumn2StartIndex, swapColumn2StartIndex
- swapColumn1EndIndex);
System.arraycopy(tempArray, 0, charValues, swapColumn2StartIndex
- swapColumn1EndIndex + swapColumn1StartIndex
+ swapColumn2EndIndex - swapColumn2StartIndex,
tempArray.length);
} else {
char[] tempArray = new char[swapColumn2EndIndex
- swapColumn2StartIndex];
System.arraycopy(charValues, swapColumn2StartIndex, tempArray, 0,
tempArray.length);
System.arraycopy(charValues, swapColumn1StartIndex, charValues,
swapColumn2StartIndex + swapColumn2EndIndex
- swapColumn2StartIndex - swapColumn1EndIndex
+ swapColumn1StartIndex, swapColumn1EndIndex
- swapColumn1StartIndex);
System.arraycopy(charValues, swapColumn1EndIndex, charValues,
swapColumn1EndIndex + swapColumn2EndIndex
- swapColumn2StartIndex - swapColumn1EndIndex
+ swapColumn1StartIndex, swapColumn2StartIndex
- swapColumn1EndIndex);
System.arraycopy(tempArray, 0, charValues, swapColumn1StartIndex,
tempArray.length);
}
return new String(charValues);
}</pre>
Approach 2 has more advantage over Approach 1. Have performed a test and found that Approach 2 has completed task in 2.776421 ms where Approach 1 has taken 19.690034 ms to complete.<br />
<br />
You can download the Java source files <a href="https://drive.google.com/file/d/0B3eOZZonu0OzUnpOY2tyS1ZsV00/view?usp=sharing" target="_blank">here</a>.</div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com4tag:blogger.com,1999:blog-7474095347317730182.post-42306707804931494352014-08-16T23:40:00.002-07:002014-08-30T10:41:58.511-07:00CAFEBABE - Print the magic number from Java class file<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">Isn't it interesting to know that Java class files have a 4 byte header which could be read as CAFEBABE in Hexadecimal. This is being mentioned in <a href="http://en.wikipedia.org/wiki/Java_class_file" target="_blank">Wikipedia: Java class file</a>.</span><br />
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Have you ever tried printing this secret code out? If not, let's do that.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Below is the code which reads it's own class file and prints the code to output stream.</span></div>
<div>
<br /></div>
<div>
<pre style="background-image: URL(https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuHEZkyn5ygwIye0UpFCAz5h0vY8ggC7e6WGd74QmN61cWEgQDClHJNz6HUDuB5uHROb_UCjhFY5z8zxWmTyLlCdYmJ8vNM2qmJAHFG07nS1aUFGzo9buptXJC6RGUrJ8RJmLtA2S5eVoT/s320/codebg.gif); background: #f0f0f0; border: 1px dashed #CCCCCC; color: black; font-family: arial; font-size: 12px; height: auto; line-height: 20px; overflow: auto; padding: 0px; text-align: left; width: 99%;"><code style="color: black; word-wrap: normal;"> package com.javavirtues.cafebabe;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
public class PrintHeader {
public static void main(String[] args) {
File classFile = new File(
"bin/com/javavirtues/cafebabe/PrintHeader.class");
if (classFile.exists()) {
System.out.println("Class file exists. Reading header.");
byte[] readHeader = new byte[4];
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream(classFile);
fileInputStream.read(readHeader);
for (byte readByte : readHeader) {
System.out.printf("%02X", readByte);
}
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
</code></pre>
</div>
<br />
<span style="font-family: Verdana, sans-serif;">You can download the eclipse project <a href="https://drive.google.com/file/d/0B3eOZZonu0OzSnpYZ1dPczNYSTQ/edit?usp=sharing" target="_blank">here</a>.</span></div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com2tag:blogger.com,1999:blog-7474095347317730182.post-16864999488840030682013-02-09T22:13:00.002-08:002013-02-09T22:14:03.000-08:00Enabling JSTL (JSP Standard Tag Library) in your web application<span style="font-family: Verdana, sans-serif;">JSTL is a good to enable tag library while developing Java Server Pages. It speeds up the development and lets you write logical blocks in fewer lines.</span><br />
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">The JSTL library to be included to your web application depends on the version of Servlet you are using. The steps mentioned here lets you choose the right JSTL library version and include it to your web application. We continue from the <a href="http://javavirtues.blogspot.in/2013/01/first-java-web-application-using-eclipse.html" target="_blank">previous post</a> where we built the web application.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<h4>
<span style="font-family: Verdana, sans-serif;">Locating Servlet version</span></h4>
<div>
<ol>
<li><span style="font-family: Verdana, sans-serif;">Open </span><span style="font-family: Courier New, Courier, monospace;">web.xml</span><span style="font-family: Verdana, sans-serif;"> file located in </span><span style="font-family: Courier New, Courier, monospace;">WEB-INF</span><span style="font-family: Verdana, sans-serif;"> folder.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Check the </span><span style="font-family: Courier New, Courier, monospace;">version</span><span style="font-family: Verdana, sans-serif;"> attribute of </span><span style="font-family: Courier New, Courier, monospace;">web-app</span><span style="font-family: Verdana, sans-serif;"> tag. <div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDAJCsl71oBOZbHbqGGZKPTr7BsOV3XH8ccLPhb_7vgxJEEWreiMNxETYfZ5F-DDNpbd_Vfn0Md3pLdPSA4sbyK_t-blQHwfPfJuyT5p8dIt6IcpZZP4SWyWbPocXU3DXi4do4IOWlHyY/s1600/1.ServletVersion.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="50" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjDAJCsl71oBOZbHbqGGZKPTr7BsOV3XH8ccLPhb_7vgxJEEWreiMNxETYfZ5F-DDNpbd_Vfn0Md3pLdPSA4sbyK_t-blQHwfPfJuyT5p8dIt6IcpZZP4SWyWbPocXU3DXi4do4IOWlHyY/s400/1.ServletVersion.png" width="400" /></a></div>
Here, the version is 2.5.</span></li>
</ol>
<h4>
<span style="font-family: Verdana, sans-serif;">Downloading JSTL package depending upon the version</span></h4>
</div>
<div>
<span style="font-family: Verdana, sans-serif;">Refer this <a href="http://stackoverflow.com/tags/jstl/info" target="_blank">link</a> to know which version you would need to download depending upon your Servlet version. You will find the download link for each version. I downloaded </span><span style="font-family: Courier New, Courier, monospace;">javax.servlet.jsp.jstl-1.2.1.jar</span><span style="font-family: Verdana, sans-serif;"> as I have 2.5 version.</span></div>
<h4>
<span style="font-family: Verdana, sans-serif;">Using JSTL in your application</span></h4>
<div>
<ol>
<li><span style="font-family: Verdana, sans-serif;">Create a </span><span style="font-family: Courier New, Courier, monospace;">lib</span><span style="font-family: Verdana, sans-serif;"> folder under </span><span style="font-family: Courier New, Courier, monospace;">WEB-INF</span><span style="font-family: Verdana, sans-serif;">.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZjHKr1P49qiV7K1Y1cMRNQC7VIlPxn6F8HjvvBMUpvdF7sFMMZHNZcVM1N7Vjrsud8rl9NeqmSO0u49qZlXHF8d-Hz8KrHN_GGuuawVS3yrL-kaABxcqz9rIzLgAPLsSHE7uQmWyzB2I/s1600/2.NewFolder.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="195" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjZjHKr1P49qiV7K1Y1cMRNQC7VIlPxn6F8HjvvBMUpvdF7sFMMZHNZcVM1N7Vjrsud8rl9NeqmSO0u49qZlXHF8d-Hz8KrHN_GGuuawVS3yrL-kaABxcqz9rIzLgAPLsSHE7uQmWyzB2I/s400/2.NewFolder.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEcbTxIPvEgHURR8nScT0Gisc34g3haLbEFIZORwyb7mUd12G8X2UAGvaNm6ATDUtz76q2S0NzcfpZExxjFUZJs5Zidrd5uKwIaluu6CrMKv-wsiRUMN5exIpXMV243gC-2UU-9r1CkGU/s1600/3.LibFolder.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="375" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgEcbTxIPvEgHURR8nScT0Gisc34g3haLbEFIZORwyb7mUd12G8X2UAGvaNm6ATDUtz76q2S0NzcfpZExxjFUZJs5Zidrd5uKwIaluu6CrMKv-wsiRUMN5exIpXMV243gC-2UU-9r1CkGU/s400/3.LibFolder.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Copy the downloaded jar file into the </span><span style="font-family: Courier New, Courier, monospace;">lib</span><span style="font-family: Verdana, sans-serif;"> folder.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcWi2u02dKrw8_x_MhScQjfWjKXkdz9XSGHc0ct4QVNWiURI27BFq-2_ieKWIQSnzawbEjrGjHkrU9oCmq2NXUFn7SEZxjtjaO0ZcKCyYcj6grDjBv56r2wAL_Xj9zvoa5wXzVF8dXnTc/s1600/4.CopyLib.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhcWi2u02dKrw8_x_MhScQjfWjKXkdz9XSGHc0ct4QVNWiURI27BFq-2_ieKWIQSnzawbEjrGjHkrU9oCmq2NXUFn7SEZxjtjaO0ZcKCyYcj6grDjBv56r2wAL_Xj9zvoa5wXzVF8dXnTc/s400/4.CopyLib.png" width="340" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Include library to your JSPs and start using it. Here I have modified the existing function to use JSTL tag.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNSLLgKe7Y44CBQ1I0mt7DBQHjxDfduOflStPqqT1p6PhpVVYVx0jAl_QJB0UkemG2e8F7Sw_hHNj7wDtDKHzcHsMxKG3BhXl1mL8TQrKh8Y6o10imH0KImHW8Zb-HkvpfCOJZNODdnDg/s1600/5.JSPJSTL.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjNSLLgKe7Y44CBQ1I0mt7DBQHjxDfduOflStPqqT1p6PhpVVYVx0jAl_QJB0UkemG2e8F7Sw_hHNj7wDtDKHzcHsMxKG3BhXl1mL8TQrKh8Y6o10imH0KImHW8Zb-HkvpfCOJZNODdnDg/s400/5.JSPJSTL.png" width="345" /></a></div>
<span style="font-family: Verdana, sans-serif;"><b>Source</b></span><br />
<!-- HTML generated using hilite.me --><div style="background: #ffffff; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="color: green; font-weight: bold;"><%@</span> page language<span style="color: #303030;">=</span><span style="background-color: #fff0f0;">"java"</span> contentType<span style="color: #303030;">=</span><span style="background-color: #fff0f0;">"text/html; charset=UTF-8"</span>
pageEncoding<span style="color: #303030;">=</span><span style="background-color: #fff0f0;">"UTF-8"</span><span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><%@</span> taglib uri<span style="color: #303030;">=</span><span style="background-color: #fff0f0;">"http://java.sun.com/jsp/jstl/core"</span> prefix<span style="color: #303030;">=</span><span style="background-color: #fff0f0;">"c"</span><span style="color: green; font-weight: bold;">%></span>
<span style="color: #507090;"><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"></span>
<span style="color: #007000;"><html></span>
<span style="color: #007000;"><head></span>
<span style="color: #007000;"><meta</span> <span style="color: #0000c0;">http-equiv=</span><span style="background-color: #fff0f0;">"Content-Type"</span> <span style="color: #0000c0;">content=</span><span style="background-color: #fff0f0;">"text/html; charset=UTF-8"</span><span style="color: #007000;">></span>
<span style="color: #007000;"><title></span>Hello World<span style="color: #007000;"></title></span>
<span style="color: #007000;"></head></span>
<span style="color: #007000;"><body></span>
<span style="color: green; font-weight: bold;"><%-</span><span style="color: #303030;">-</span>
Object message <span style="color: #303030;">=</span> request<span style="color: #303030;">.</span><span style="color: #0000c0;">getAttribute</span><span style="color: #303030;">(</span><span style="background-color: #fff0f0;">"message"</span><span style="color: #303030;">);</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #303030;">(</span>message <span style="color: #303030;">==</span> <span style="color: green; font-weight: bold;">null</span><span style="color: #303030;">)</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">%></span>
<span style="color: #007000;"><form</span> <span style="color: #0000c0;">action=</span><span style="background-color: #fff0f0;">"sayHello"</span> <span style="color: #0000c0;">method=</span><span style="background-color: #fff0f0;">"post"</span><span style="color: #007000;">></span>
<span style="color: #007000;"><input</span> <span style="color: #0000c0;">type=</span><span style="background-color: #fff0f0;">"submit"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"Submit"</span> <span style="color: #007000;">/></span>
<span style="color: #007000;"></form></span>
<span style="color: green; font-weight: bold;"><%[</span>
<span style="color: #303030;">}</span> <span style="color: green; font-weight: bold;">else</span> <span style="color: #303030;">{</span>
<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><%=</span>message<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><%</span>
<span style="color: #303030;">}</span>
<span style="color: #303030;">--</span><span style="color: green; font-weight: bold;">%></span>
<span style="color: #007000;"><c:if</span> <span style="color: #0000c0;">test=</span><span style="background-color: #fff0f0;">"${empty message}"</span><span style="color: #007000;">></span>
<span style="color: #007000;"><form</span> <span style="color: #0000c0;">action=</span><span style="background-color: #fff0f0;">"sayHello"</span> <span style="color: #0000c0;">method=</span><span style="background-color: #fff0f0;">"post"</span><span style="color: #007000;">></span>
<span style="color: #007000;"><input</span> <span style="color: #0000c0;">type=</span><span style="background-color: #fff0f0;">"submit"</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"Submit"</span> <span style="color: #007000;">/></span>
<span style="color: #007000;"></form></span>
<span style="color: #007000;"></c:if></span>
<span style="color: #007000;"><c:if</span> <span style="color: #0000c0;">test=</span><span style="background-color: #fff0f0;">"${not empty message}"</span><span style="color: #007000;">></span>
<span style="color: #007000;"><c:out</span> <span style="color: #0000c0;">value=</span><span style="background-color: #fff0f0;">"${message}"</span> <span style="color: #007000;">/></span>
<span style="color: #007000;"></c:if></span>
<span style="color: #007000;"></body></span>
<span style="color: #007000;"></html></span>
</pre>
</div>
</li>
</ol>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com12tag:blogger.com,1999:blog-7474095347317730182.post-5414481281640070142013-01-20T10:38:00.002-08:002014-08-30T10:41:21.095-07:00Java Web Application using Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Java's web technology provides us the facility to create dynamic web applications easily with the components it provide. Servlets play an important role in Java web application development by providing the developer with its options to hold the client request, pass it to the handled method and send the response to the client. This </span><a href="http://docs.oracle.com/javaee/1.4/tutorial/doc/WebApp.html" style="font-family: Verdana, sans-serif;" target="_blank">document</a><span style="font-family: Verdana, sans-serif;"> will help you in understanding the architecture better.</span><br />
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Before starting web development, the developer should understand the request flow. All requests from the client (web browser) to server happens through <a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol#Request_methods" target="_blank">HTTP methods</a> defined by the protocol. GET and POST are the methods which are commonly used to pass the request. Each request contains an action and may have parameters sent to the server to perform specific operation. For example, in a login operation, the action would be loginaction and the parameters passed can be user-name and password.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Once the server processes the request, it responds back to the client with a status code saying if the operation was successful, a content type which helps browsers to show the response as html, pdf, zip etc. and optional message body.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Another important matter to be noticed is that the Java Server Pages (JSP) should be thought only in the scope of web server. The contents of the JSPs will get transformed to html elements before being served to the browser as response.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Moreover, </span><span style="font-family: Courier New, Courier, monospace;">web.xml</span><span style="font-family: Verdana, sans-serif;">, residing in the </span><span style="font-family: Courier New, Courier, monospace;">WEB-INF</span><span style="font-family: Verdana, sans-serif;"> folder is the major configuration file. You will come across this file as we go forward.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Here we start developing a hello world web application using Eclipse. Assuming that you have configured a web server on your Eclipse IDE. If you have not, you can follow this <a href="http://javavirtues.blogspot.in/2013/01/configuring-jboss-server-on-eclipse.html" target="_blank">link</a> to configure JBoss web server.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">This Eclipse project can be downloaded <a href="https://docs.google.com/file/d/0B3eOZZonu0OzX2tqalVzMks0Qjg/edit" target="_blank">here</a>. You can extract the file and use </span><span style="font-family: Courier New, Courier, monospace;">File > Import</span><span style="font-family: Verdana, sans-serif;"> to download the project into your Eclipse workspace.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">On Eclipse, go to </span><span style="font-family: Courier New, Courier, monospace;">File > New > Project</span><span style="font-family: Verdana, sans-serif;">.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAZ5poUoOmQfcFEjvkWzG_YpYULItyjERmT3J38ZF1plNs_Wj78kmoTHj2ZXv4e2vK49NvhtI7jSl8rv3LWQwYadD1kBMNFmglE1miCnouH0clAZkKueHj6C_-RZPmRLxO7487d1_Nyus/s1600/1.FileNew.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhAZ5poUoOmQfcFEjvkWzG_YpYULItyjERmT3J38ZF1plNs_Wj78kmoTHj2ZXv4e2vK49NvhtI7jSl8rv3LWQwYadD1kBMNFmglE1miCnouH0clAZkKueHj6C_-RZPmRLxO7487d1_Nyus/s400/1.FileNew.png" height="307" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Under Web, choose </span><span style="font-family: Courier New, Courier, monospace;">Dynamic Web Project</span><span style="font-family: Verdana, sans-serif;"> and choose </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Name your project and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN814WK9OjqaKRIQ5cYZj-UF-8ZgTQAuGyoWz2shjl7d2YE2rd5AZPmUOQtEOuzhcD_PPigz6jJ4QtKec2jON1nbes5_nMMxDVAGFX6TO_GBRMEUg22NUB3z5I27wUj3spJRsUwerAtzc/s1600/2.NewDynamicProj.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhN814WK9OjqaKRIQ5cYZj-UF-8ZgTQAuGyoWz2shjl7d2YE2rd5AZPmUOQtEOuzhcD_PPigz6jJ4QtKec2jON1nbes5_nMMxDVAGFX6TO_GBRMEUg22NUB3z5I27wUj3spJRsUwerAtzc/s400/2.NewDynamicProj.png" height="400" width="353" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">, choose your context root and content directory, and then click </span><span style="font-family: Courier New, Courier, monospace;">Finish</span><span style="font-family: Verdana, sans-serif;">. Context root is what you will use to access the application from a browser client. Content directory is where you will create your web pages and other resources such as images and javascript files.</span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">We have successfully created a web project. Next is to write a Servlet to handle requests.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Right-click on the project, choose </span><span style="font-family: Courier New, Courier, monospace;">New > Servlet</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheHQ6rjhLz6dk7OgmBrpCVRRDwKbF1fzKx0pKcawyrqpiHB_9mTYyyvR4ZLtLpwAYvKkIXlrDUUkLxaugmWpcS9ci8d1GpXxr7MmmYNGdK_d0Z2rauxU6Tbmgu9rD3qnGOSbt_v26bhCs/s1600/3.NewServlet.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEheHQ6rjhLz6dk7OgmBrpCVRRDwKbF1fzKx0pKcawyrqpiHB_9mTYyyvR4ZLtLpwAYvKkIXlrDUUkLxaugmWpcS9ci8d1GpXxr7MmmYNGdK_d0Z2rauxU6Tbmgu9rD3qnGOSbt_v26bhCs/s400/3.NewServlet.png" height="201" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Name the package, then the Servlet and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiZ7vKjetAtN2m6yFUdtfNbzmYG1RtmsCXvBa3qE5JSwSoLq9IZrcNzvW7dy_ZUWCcEPzbPlYqv67NZL27omQvz1_esydUojoeH_dMHiGSj7eCXmrh7spVW8PeV7Lmq2Vd2mP_E_hRnFA/s1600/4.HelloWorldServlet.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjiZ7vKjetAtN2m6yFUdtfNbzmYG1RtmsCXvBa3qE5JSwSoLq9IZrcNzvW7dy_ZUWCcEPzbPlYqv67NZL27omQvz1_esydUojoeH_dMHiGSj7eCXmrh7spVW8PeV7Lmq2Vd2mP_E_hRnFA/s400/4.HelloWorldServlet.png" height="293" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Type any description, edit the default URL mapping and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpE2r7LUwebm-nX4IBN_lta5cfgZFI_SdH3rn04_cAh7XKXEn1cesL4hXwntedTPYr-aB9llUxwak0nNfhYR_ToujXSoQ0Pw5ILpf9llKh9zUt5FG8hMhS_GrThPSC00_6sU9MGhAwpzU/s1600/5.ServletMapping.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhpE2r7LUwebm-nX4IBN_lta5cfgZFI_SdH3rn04_cAh7XKXEn1cesL4hXwntedTPYr-aB9llUxwak0nNfhYR_ToujXSoQ0Pw5ILpf9llKh9zUt5FG8hMhS_GrThPSC00_6sU9MGhAwpzU/s400/5.ServletMapping.png" height="365" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Choose the methods to handle the types of requests and click </span><span style="font-family: Courier New, Courier, monospace;">Finish</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK46KfI_SwqMH94SCZiZXn4xIGZViBp2S-oY_Ob0pf71ydt6GmtMFyTImOpN9hK_ZvYC2XvYUhFgMezXHMQd0yylHKzTf6IsW2oK_dvLoQfb66KEPwEoZcb3q32qB1vGv0oKMmTtQpWIc/s1600/6.ServletMetods.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgK46KfI_SwqMH94SCZiZXn4xIGZViBp2S-oY_Ob0pf71ydt6GmtMFyTImOpN9hK_ZvYC2XvYUhFgMezXHMQd0yylHKzTf6IsW2oK_dvLoQfb66KEPwEoZcb3q32qB1vGv0oKMmTtQpWIc/s400/6.ServletMetods.png" height="365" width="400" /></a></div>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">If you look into </span><span style="font-family: Courier New, Courier, monospace;">web.xml</span><span style="font-family: Verdana, sans-serif;"> contents, you will find that the Servlet which we created is mapped to the URL we had provided in the above step. Whatever requests arrives to the application in the url-pattern we have specified will be set to be handled by the mapped Servlet instance. Our Servlet extends from </span><a href="http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServlet.html" target="_blank"><span style="font-family: Courier New, Courier, monospace;">HttpServlet</span></a><span style="font-family: Verdana, sans-serif;">. Go through the javadoc of </span><span style="font-family: Courier New, Courier, monospace;">HttpServlet</span><span style="font-family: Verdana, sans-serif;"> and </span><a href="http://docs.oracle.com/javaee/1.3/api/javax/servlet/GenericServlet.html" target="_blank"><span style="font-family: Courier New, Courier, monospace;">GenericServlet</span></a><span style="font-family: Verdana, sans-serif;"> to find the basic methods and their descriptions. I have chosen to get hold of </span><a href="http://docs.oracle.com/javaee/1.3/api/javax/servlet/http/HttpServlet.html#service(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)" target="_blank"><span style="font-family: Courier New, Courier, monospace;">service</span></a><span style="font-family: Verdana, sans-serif;">, </span><a href="http://docs.oracle.com/javaee/1.3/api/javax/servlet/GenericServlet.html#init(javax.servlet.ServletConfig)" target="_blank"><span style="font-family: Courier New, Courier, monospace;">init</span></a><span style="font-family: Verdana, sans-serif;"> and </span><a href="http://docs.oracle.com/javaee/1.3/api/javax/servlet/GenericServlet.html#destroy()" target="_blank"><span style="font-family: Courier New, Courier, monospace;">destroy</span></a><span style="font-family: Verdana, sans-serif;"> methods just to print the life-cycle of this Servlet. Hence, the method call will be directed to the super class for actual processing.</span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;"><br /></span>
</li>
<li><span style="font-family: Verdana, sans-serif;">In the </span><span style="font-family: Courier New, Courier, monospace;">doGet</span><span style="font-family: Verdana, sans-serif;"> method, write the code to handle the request.
</span>
<!-- HTML generated using hilite.me -->
<br />
<div style="background: #f8f8f8; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: inherit;"> <span style="color: green; font-weight: bold;">protected</span> <span style="color: #b00040;">void</span> <span style="color: blue;">doGet</span><span style="color: #666666;">(</span>HttpServletRequest request<span style="color: #666666;">,</span> HttpServletResponse response<span style="color: #666666;">)</span> <span style="color: green; font-weight: bold;">throws</span> ServletException<span style="color: #666666;">,</span> IOException <span style="color: #666666;">{</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span><span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"In the doGet(HttpServletRequest request, HttpServletResponse response) method of HelloWorldServlet"</span><span style="color: #666666;">);</span>
response<span style="color: #666666;">.</span><span style="color: #7d9029;">setContentType</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"text/html"</span><span style="color: #666666;">);</span>
PrintWriter printWriter <span style="color: #666666;">=</span> response<span style="color: #666666;">.</span><span style="color: #7d9029;">getWriter</span><span style="color: #666666;">();</span>
printWriter<span style="color: #666666;">.</span><span style="color: #7d9029;">print</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"<html><head></head><body>Hello World</body></html>"</span><span style="color: #666666;">);</span>
printWriter<span style="color: #666666;">.</span><span style="color: #7d9029;">close</span><span style="color: #666666;">();</span>
<span style="color: #666666;">}</span></span></pre>
</div>
<span style="font-family: Verdana, sans-serif;">
</span></li>
<li><span style="font-family: Verdana, sans-serif;">Create a JSP file as referred in </span><span style="font-family: Courier New, Courier, monospace;">web.xml's</span><span style="font-family: Verdana, sans-serif;"> </span><span style="font-family: Courier New, Courier, monospace;">welcome-file-list</span><span style="font-family: Verdana, sans-serif;">. The first available among them will be be displayed by default on visiting the context root. </span><span style="font-family: Verdana, sans-serif;">Right-click on the project, choose </span><span style="font-family: Courier New, Courier, monospace;">New > JSP File</span><span style="font-family: Verdana, sans-serif;"> or from </span><span style="font-family: Courier New, Courier, monospace;">New > Other > Web > JSP File</span><span style="font-family: Verdana, sans-serif;">.</span>
</li>
<li><span style="font-family: Verdana, sans-serif;">Select WebContent folder or the contents folder mentioned in step 4, type the file name and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilTaJ25ElydYc2A89Lv3NgXHS8v-CoqGWQ8GQabmCyWXbOWLsKNnqUmi9WFlMF5fxancJPY5Eh5zvsXhGYrXH_-yF-79xU94kxkHJ0WvGrvuGsuBCmOlz3rBbbm0H_M04Or0glkc-S_k8/s1600/7.NewJSP.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEilTaJ25ElydYc2A89Lv3NgXHS8v-CoqGWQ8GQabmCyWXbOWLsKNnqUmi9WFlMF5fxancJPY5Eh5zvsXhGYrXH_-yF-79xU94kxkHJ0WvGrvuGsuBCmOlz3rBbbm0H_M04Or0glkc-S_k8/s400/7.NewJSP.png" height="360" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">By default, the JSP file will be opened in your editor. Modify the title as you wish.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Add the following code snippet in the body part of JSP and save the file.
<!-- HTML generated using hilite.me --></span><br />
<div style="background: #f8f8f8; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: inherit;"><span style="color: green; font-weight: bold;"><body></span>
<span style="color: green; font-weight: bold;"><%</span>
Object message <span style="color: #666666;">=</span> request<span style="color: #666666;">.</span><span style="color: #7d9029;">getAttribute</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"message"</span><span style="color: #666666;">);</span>
<span style="color: green; font-weight: bold;">if</span> <span style="color: #666666;">(</span>message <span style="color: #666666;">==</span> <span style="color: green; font-weight: bold;">null</span><span style="color: #666666;">)</span> <span style="color: #666666;">{</span>
<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><form</span> <span style="color: #7d9029;">action=</span><span style="color: #ba2121;">"sayHello"</span> <span style="color: #7d9029;">method=</span><span style="color: #ba2121;">"post"</span><span style="color: green; font-weight: bold;">></span>
<span style="color: green; font-weight: bold;"><input</span> <span style="color: #7d9029;">type=</span><span style="color: #ba2121;">"submit"</span> <span style="color: #7d9029;">value=</span><span style="color: #ba2121;">"Submit"</span> <span style="color: green; font-weight: bold;">/></span>
<span style="color: green; font-weight: bold;"></form></span>
<span style="color: green; font-weight: bold;"><%</span>
<span style="color: #666666;">}</span> <span style="color: green; font-weight: bold;">else</span> <span style="color: #666666;">{</span>
<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><%=</span>message<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"><%</span>
<span style="color: #666666;">}</span>
<span style="color: green; font-weight: bold;">%></span>
<span style="color: green; font-weight: bold;"></body></span></span><span style="font-family: Verdana, sans-serif;">
</span></pre>
</div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Edit the doPost method of Servlet as
<!-- HTML generated using hilite.me --></span><br />
<div style="background: #f8f8f8; background: white; border-width: .1em .1em .1em .8em; border: solid gray; color: black; overflow: auto; padding: .2em .6em; width: auto;">
<pre style="line-height: 125%; margin: 0;"><span style="font-family: inherit;"> <span style="color: green; font-weight: bold;">protected</span> <span style="color: #b00040;">void</span> <span style="color: blue;">doPost</span><span style="color: #666666;">(</span>HttpServletRequest request<span style="color: #666666;">,</span>
HttpServletResponse response<span style="color: #666666;">)</span> <span style="color: green; font-weight: bold;">throws</span> ServletException<span style="color: #666666;">,</span> IOException <span style="color: #666666;">{</span>
System<span style="color: #666666;">.</span><span style="color: #7d9029;">out</span>
<span style="color: #666666;">.</span><span style="color: #7d9029;">println</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"In the doPost(HttpServletRequest request, HttpServletResponse response) method of HelloWorldServlet"</span><span style="color: #666666;">);</span>
request<span style="color: #666666;">.</span><span style="color: #7d9029;">setAttribute</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"message"</span><span style="color: #666666;">,</span> <span style="color: #ba2121;">"Hello World through post"</span><span style="color: #666666;">);</span>
RequestDispatcher requestDispatcher <span style="color: #666666;">=</span> getServletContext<span style="color: #666666;">()</span>
<span style="color: #666666;">.</span><span style="color: #7d9029;">getRequestDispatcher</span><span style="color: #666666;">(</span><span style="color: #ba2121;">"/index.jsp"</span><span style="color: #666666;">);</span>
requestDispatcher<span style="color: #666666;">.</span><span style="color: #7d9029;">forward</span><span style="color: #666666;">(</span>request<span style="color: #666666;">,</span> response<span style="color: #666666;">);</span>
<span style="color: #666666;">}</span></span><span style="font-family: Verdana, sans-serif;">
</span></pre>
</div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Let us try running the application. Right-click on the application and choose </span><span style="font-family: Courier New, Courier, monospace;">Run As > Run On Server</span><span style="font-family: Verdana, sans-serif;"> and click </span><span style="font-family: Courier New, Courier, monospace;">Finish</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">On your browser, go to </span><span style="font-family: Courier New, Courier, monospace;">http://<server-host-name>:<port>/<contextpath></span><span style="font-family: Verdana, sans-serif;">. In my case </span><a href="http://localhost:8080/MyHelloWorldWeb" target="_blank"><span style="font-family: Courier New, Courier, monospace;">http://localhost:8080/MyHelloWorldWeb</span></a><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">You will see a Submit button because </span><span style="font-family: Courier New, Courier, monospace;">web.xml</span><span style="font-family: Verdana, sans-serif;"> is blindly set to show the welcome file. The content of the file tries to get the request attribute named </span><span style="font-family: Courier New, Courier, monospace;">message</span><span style="font-family: Verdana, sans-serif;">, which will be </span><span style="font-family: Courier New, Courier, monospace;">null</span><span style="font-family: Verdana, sans-serif;"> since the request has not gone to the </span><span style="font-family: Courier New, Courier, monospace;">doPost</span><span style="font-family: Verdana, sans-serif;"> method.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Click on the Submit button and see the server log on the console. The doPost method would be called and message will be passed to </span><span style="font-family: Courier New, Courier, monospace;">index.jsp</span><span style="font-family: Verdana, sans-serif;">, which behaves in a different way.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Try the URL </span><span style="font-family: Courier New, Courier, monospace;">contextpath/sayHello</span><span style="font-family: Verdana, sans-serif;"> and check the log to see the request being handled by doGet method.</span></li>
<li><span style="font-family: Verdana, sans-serif;">From the Servers view of Eclipse, expand the server, right click on the web application and say </span><span style="font-family: Courier New, Courier, monospace;">Remove</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXlqDW1mdqewvMTXiy_mW6t0L0tIvh7wYyB1TF_U8G2dXFHyBx_C8XosWaHw5ouScbcmbbSH7BPjYkng42-3NRg9STVs_7VkkuXz3trTp6ZrH9Ubwc591V7TTOknO1irS9JiPBQMJ3I5o/s1600/8.WebAppRemove.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiXlqDW1mdqewvMTXiy_mW6t0L0tIvh7wYyB1TF_U8G2dXFHyBx_C8XosWaHw5ouScbcmbbSH7BPjYkng42-3NRg9STVs_7VkkuXz3trTp6ZrH9Ubwc591V7TTOknO1irS9JiPBQMJ3I5o/s400/8.WebAppRemove.png" height="172" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Look at the console to see the message given to print at destroy method being called which completes the life-cycle of the Servlet.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLNtnOStZfPeHcp6ScWkp0N4La2SomLp6cjbtm-SRC4KYTC7JTA9q639ghOjBWMm0NHChb8xV1iQJQ8IRNby2A24voEC28dG0L7cxIeLCRKSiD5IYB5iIp4rNoDnuLOizysuJx6AQyW8o/s1600/9.WebAppLogs.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhLNtnOStZfPeHcp6ScWkp0N4La2SomLp6cjbtm-SRC4KYTC7JTA9q639ghOjBWMm0NHChb8xV1iQJQ8IRNby2A24voEC28dG0L7cxIeLCRKSiD5IYB5iIp4rNoDnuLOizysuJx6AQyW8o/s400/9.WebAppLogs.png" height="107" width="400" /></a></div>
</li>
</ol>
</div>
</div>
</div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com8tag:blogger.com,1999:blog-7474095347317730182.post-4682156157801759162013-01-20T02:23:00.000-08:002013-02-09T20:22:41.317-08:00Configuring JBoss server on Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">JBoss application server is a server which offers <a href="http://www.oracle.com/technetwork/java/javaee/overview/index.html" target="_blank">Java Enterprise Edition</a> services to web applications authored in Java language.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<span style="font-family: Verdana, sans-serif;">Here are the steps to configure JBoss server on Eclipse:</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Download JBoss server from </span><a href="http://www.jboss.org/jbossas/downloads" style="font-family: Verdana, sans-serif;" target="_blank">here</a><span style="font-family: Verdana, sans-serif;">. I have chosen the 4.2.3 version of JBoss server to download.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Extract the downloaded compressed file to a suitable location. I have extracted the file to </span><span style="font-family: Courier New, Courier, monospace;">/opt</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">On Eclipse, go to Servers view. If you do not see Servers view, go to </span><span style="font-family: Courier New, Courier, monospace;">Window > Show View > Other</span><span style="font-family: Verdana, sans-serif;">. </span><span style="font-family: Verdana, sans-serif;">On the Show View window, choose </span><span style="font-family: Courier New, Courier, monospace;">Server > Servers</span><span style="font-family: Verdana, sans-serif;"> and click </span><span style="font-family: Courier New, Courier, monospace;">OK</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Click on new server wizard or right-click and choose </span><span style="font-family: Courier New, Courier, monospace;">New > Server</span><span style="font-family: Verdana, sans-serif;">.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh32JmIv2tesxLIT356pnzDwfW0NjvUleeAlRh5XBxI7-RoWHCImjG2PbgSb2pfFqQ9SWkwGGK8TDxqqjGT3SUPrmwom57JSkAdnCzHXLRHOH6wAP3aJhVjpVkzS9k5BdzIqw3GRBQcdDc/s1600/1.Servers.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="176" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh32JmIv2tesxLIT356pnzDwfW0NjvUleeAlRh5XBxI7-RoWHCImjG2PbgSb2pfFqQ9SWkwGGK8TDxqqjGT3SUPrmwom57JSkAdnCzHXLRHOH6wAP3aJhVjpVkzS9k5BdzIqw3GRBQcdDc/s400/1.Servers.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Choose </span><span style="font-family: Courier New, Courier, monospace;">JBoss > JBoss v4.2</span><span style="font-family: Verdana, sans-serif;"> and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-QAc_jKBfpHPdjPLuhOFIDkz1cfpm6XkTCkrY_jSKXArYZqOm0P68T_RIgs_kmV0qL0R3inrtyFWM4GP_EX2bbyxqMFl1aCbtlitL-_Dp2bfi-fvZ34dvjq2uEIzawS3oIVjpIpVA2s/s1600/2.NewServer.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="375" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhK-QAc_jKBfpHPdjPLuhOFIDkz1cfpm6XkTCkrY_jSKXArYZqOm0P68T_RIgs_kmV0qL0R3inrtyFWM4GP_EX2bbyxqMFl1aCbtlitL-_Dp2bfi-fvZ34dvjq2uEIzawS3oIVjpIpVA2s/s400/2.NewServer.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">To set the Application Server Directory, click <span style="font-family: Courier New, Courier, monospace;">Browse</span> or type the path to the extracted server folder in step 2 and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjR7QC-LXKL1pm7g01NaWli4Qm3goXD5aOI5pQkKQe4oAy0Jnl364iJghIHtgUE-aqYkgBpXCnALtweiOlekG8M89-lgbjg8BkzsH50FIuXTB50N_5kPzc8JKbRQeZJAH1BRaz3Q52wVj0/s1600/3.ServerPath.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="355" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjR7QC-LXKL1pm7g01NaWli4Qm3goXD5aOI5pQkKQe4oAy0Jnl364iJghIHtgUE-aqYkgBpXCnALtweiOlekG8M89-lgbjg8BkzsH50FIuXTB50N_5kPzc8JKbRQeZJAH1BRaz3Q52wVj0/s400/3.ServerPath.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Choose address, port, JNDI port and server configuration and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">. I have left it as default values here.</span>
</li>
<li><span style="font-family: Verdana, sans-serif;">If you have any web application to be set to run on the server, add them and click </span><span style="font-family: Courier New, Courier, monospace;">Finish</span><span style="font-family: Verdana, sans-serif;">. You will find the server being listed in the Servers view.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Double click on the server listed in the Servers view or right-click and choose Open.</span>
</li>
<li><span style="font-family: Verdana, sans-serif;">The server can be configured in the window opened up. Expand the </span><span style="font-family: Courier New, Courier, monospace;">Timeouts</span><span style="font-family: Verdana, sans-serif;"> section and modify the </span><span style="font-family: Courier New, Courier, monospace;">Start</span><span style="font-family: Verdana, sans-serif;"> and </span><span style="font-family: Courier New, Courier, monospace;">Stop</span><span style="font-family: Verdana, sans-serif;"> values. These are the values in which the server has to start-up or shutdown as monitored by Eclipse. You may need to increase these timings, otherwise you will end up with an error saying server could not be started as displayed below. Once done, save the configuration from </span><span style="font-family: Courier New, Courier, monospace;">File > Save</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwYt9Gse7Qecn7rex_3TVEBJ_cIQGtu5nWfZYLqeZRwau2gYif8Rg1rWKt9bhxar5obAvCnxuKQDgoLjlh80Ihw05vVqlTSpFF8-IHdeGoHHx23t8vxRdHZsdRWN21OMNay4a9VCk3wLU/s1600/4.ServerConfig.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="136" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjwYt9Gse7Qecn7rex_3TVEBJ_cIQGtu5nWfZYLqeZRwau2gYif8Rg1rWKt9bhxar5obAvCnxuKQDgoLjlh80Ihw05vVqlTSpFF8-IHdeGoHHx23t8vxRdHZsdRWN21OMNay4a9VCk3wLU/s400/4.ServerConfig.png" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwM6cL8ds_bMtuH8JVIjHm4TfFq30GgmC8yhiTTRwPDzsQJ60y85X9CO6JzhbWlY8j_Eeg64vWQePJyDyOC_D6bw4bZISDgnMOO3i2mwBD0nGgWeP1paAsxQfXA7uNzduobEQaBvAj24/s1600/5.ServerStartupError.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="216" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjhwM6cL8ds_bMtuH8JVIjHm4TfFq30GgmC8yhiTTRwPDzsQJ60y85X9CO6JzhbWlY8j_Eeg64vWQePJyDyOC_D6bw4bZISDgnMOO3i2mwBD0nGgWeP1paAsxQfXA7uNzduobEQaBvAj24/s320/5.ServerStartupError.png" width="320" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Right-click on the server from Servers view and choose </span><span style="font-family: Courier New, Courier, monospace;">Start</span><span style="font-family: Verdana, sans-serif;">.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWaOgUzbU-WOqfIZT1NHtPVupMWycM116pPmoX1-pOHAd5xaxpK7CiwWChLjg99ArimQ-1xg9pHiGOMOVqCOFUo_fH_7wd7ZSgi483Iu0whStZVy1x4WDuZf2z5HtPCkAvoEtMWoYcE9w/s1600/6.ServerStart.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="263" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjWaOgUzbU-WOqfIZT1NHtPVupMWycM116pPmoX1-pOHAd5xaxpK7CiwWChLjg99ArimQ-1xg9pHiGOMOVqCOFUo_fH_7wd7ZSgi483Iu0whStZVy1x4WDuZf2z5HtPCkAvoEtMWoYcE9w/s400/6.ServerStart.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Monitor the Console view to see if the server has started successfully.</span>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh37_ueKxbGcJ2WVpMxkwuYfj4rOpOkMsusorg5_QU6QGpGoDmpin0r-T3HqhcY4Sf0BNGmZZ9q8ACgNQs1rz1eh_H4H0N5Q46VzOJij5F2ifZ_KtfvAUVt3utJuufEbssq_6frwV6LVA0/s1600/7.SuccessfullyStarted.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="66" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh37_ueKxbGcJ2WVpMxkwuYfj4rOpOkMsusorg5_QU6QGpGoDmpin0r-T3HqhcY4Sf0BNGmZZ9q8ACgNQs1rz1eh_H4H0N5Q46VzOJij5F2ifZ_KtfvAUVt3utJuufEbssq_6frwV6LVA0/s400/7.SuccessfullyStarted.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Test the server from browser by going to the host name and port configured.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsxfjIY6hd6N8yh9GnfpDt8JItcu9tfdkTfre7lypno-EJ6ju8AxvOdJ_4oNAGX42r0FEkRc9TypcQBCQl8TRCW5WDC2hhyphenhyphenEFmKMgvkM72iPsMdykrp-ABkbnNEk7rm2eVz9YDdsmT8BE/s1600/8.ServerRunning.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="185" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgsxfjIY6hd6N8yh9GnfpDt8JItcu9tfdkTfre7lypno-EJ6ju8AxvOdJ_4oNAGX42r0FEkRc9TypcQBCQl8TRCW5WDC2hhyphenhyphenEFmKMgvkM72iPsMdykrp-ABkbnNEk7rm2eVz9YDdsmT8BE/s400/8.ServerRunning.png" width="400" /></a></div>
</li>
</ol>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com1tag:blogger.com,1999:blog-7474095347317730182.post-80789854343736556542013-01-19T23:11:00.000-08:002014-08-30T10:40:46.157-07:00Java standalone application on Eclipse<div dir="ltr" style="text-align: left;" trbidi="on">
<span style="font-family: Verdana, sans-serif;">Java standalone application would be the easiest one to start when learning the language. We will print "Hello World" text on the console as the first step.</span><br />
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<span style="font-family: Verdana, sans-serif;">Java standalone applications should have at least a class with <a href="http://docs.oracle.com/javase/tutorial/getStarted/application/index.html" target="_blank">main</a> method, in the right signature. This is the first method that will take the handle of the application when called by Java Virtual Machine. Here we go.</span></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<div>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">On your Eclipse IDE, click on </span><span style="font-family: Courier New, Courier, monospace;">File > New > Project</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Choose </span><span style="font-family: Courier New, Courier, monospace;">Java > Java Project</span><span style="font-family: Verdana, sans-serif;"> and click </span><span style="font-family: Courier New, Courier, monospace;">Next</span><span style="font-family: Verdana, sans-serif;">.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIx2a-L6idEZiIo3NWMCR6WvLVLdoI3cALqiAIX53EpacYmaYz9wcUTjCl8s1gUg9D1Qz1KJjdawAnwJdOdTWh2_RnS3pq4kXlFDA3qsU5pkxqSxXDEOSLWA18hmxVydi27vViUqn69xg/s1600/1.NewProject.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgIx2a-L6idEZiIo3NWMCR6WvLVLdoI3cALqiAIX53EpacYmaYz9wcUTjCl8s1gUg9D1Qz1KJjdawAnwJdOdTWh2_RnS3pq4kXlFDA3qsU5pkxqSxXDEOSLWA18hmxVydi27vViUqn69xg/s400/1.NewProject.png" height="307" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Key in the project name and click </span><span style="font-family: Courier New, Courier, monospace;">Finish</span><span style="font-family: Verdana, sans-serif;">.<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTVfhN1qR8n-VhwvLlOqxIWQhCEmgRgO0dxbJjV01NBmJklA4YEd2SnHOE2jbK1fqEykm0ajO0vRybdXlcQaQJjHpgLvlKKmS7rmE1KS5g8ok_79eVGCLDfeXk97Jxbo5FT1BQZCriPm0/s1600/2.NewJavaProject.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgTVfhN1qR8n-VhwvLlOqxIWQhCEmgRgO0dxbJjV01NBmJklA4YEd2SnHOE2jbK1fqEykm0ajO0vRybdXlcQaQJjHpgLvlKKmS7rmE1KS5g8ok_79eVGCLDfeXk97Jxbo5FT1BQZCriPm0/s400/2.NewJavaProject.png" height="400" width="353" /></a></div>
</span></li>
<li><span style="font-family: Verdana, sans-serif;">From the Package Explorer, right click and create new Class.<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYPSO5yWBGNm2kkiYAVyYU5W9SRxNTFzp4uLWG4-WsdwiRxjt_qim0Vcf1DLPNjgOg1bPy2ECLQINLPGsAJJvnPKDbrP3aKB1Lvt0WdA_6nK7Web7xDJAWGL7G49o5w0iVU_T4P_y99pM/s1600/3.New+Class.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhYPSO5yWBGNm2kkiYAVyYU5W9SRxNTFzp4uLWG4-WsdwiRxjt_qim0Vcf1DLPNjgOg1bPy2ECLQINLPGsAJJvnPKDbrP3aKB1Lvt0WdA_6nK7Web7xDJAWGL7G49o5w0iVU_T4P_y99pM/s400/3.New+Class.png" height="202" width="400" /></a></div>
</span></li>
<li><span style="font-family: Verdana, sans-serif;">Name your new Class. If you need to place the new Class in a package, fill-in the package name. Since this is your Main class, you can check "</span><span style="font-family: Courier New, Courier, monospace;">public static void main(String[] args)</span><span style="font-family: Verdana, sans-serif;">" under "</span><span style="font-family: Courier New, Courier, monospace;">Which method stubs would you like to create?</span><span style="font-family: Verdana, sans-serif;">". I would not select this option now.</span></li>
<li><span style="font-family: Verdana, sans-serif;">You will find your Main class being opened in the editor. You can type the entire signature of the main method or just type </span><span style="font-family: Courier New, Courier, monospace;">main</span><span style="font-family: Verdana, sans-serif;">, then use </span><span style="font-family: Courier New, Courier, monospace;">Ctrl + Space</span><span style="font-family: Verdana, sans-serif;"> to show the proposed templates and choose </span><span style="font-family: Courier New, Courier, monospace;">main method</span><span style="font-family: Verdana, sans-serif;"> to generate the main method.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0Vhrm17ZIYOlj9W7JDWKfqSguzECupE8lqSzg8xlsMgmO-fKGfrXiIbRgyU5iHsPW5eDUyBaiUsURB2AT-S3o5CAdrO3jNZquQ8_FmRDTGMGO0MVO2ldjOxiSpz4CQzPt9SMc8YLVxxk/s1600/4.main+template.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh0Vhrm17ZIYOlj9W7JDWKfqSguzECupE8lqSzg8xlsMgmO-fKGfrXiIbRgyU5iHsPW5eDUyBaiUsURB2AT-S3o5CAdrO3jNZquQ8_FmRDTGMGO0MVO2ldjOxiSpz4CQzPt9SMc8YLVxxk/s400/4.main+template.png" height="201" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Type the statement to print "Hello World" statement on the output stream. That is, </span><span style="font-family: Courier New, Courier, monospace;">System.out.println("Hello World");</span><span style="font-family: Verdana, sans-serif;">. The template way to type the statement is </span><span style="font-family: Courier New, Courier, monospace;">sysout</span><span style="font-family: Verdana, sans-serif;"> and using </span><span style="font-family: Courier New, Courier, monospace;">Ctrl + Space</span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Format the code snippet using </span><span style="font-family: Courier New, Courier, monospace;">Source > Format</span><span style="font-family: Verdana, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;">Ctrl + Shift + F</span><span style="font-family: Verdana, sans-serif;"> and save the file.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Right-click on the project or the class and use </span><span style="font-family: Courier New, Courier, monospace;">Run As > Java Application</span><span style="font-family: Verdana, sans-serif;"> to run the file.</span></li>
<li><span style="font-family: Verdana, sans-serif;">See the console. If console could not be found, select to show it from Window menu.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHoXieHeaQ1Xi1EYfec-Xo1QmTyKJ8mP5XrqTv_ZYsM8GMmMNAjPBLTRUF-dX6ll04-UxLkydJSPBG79qjMDldGfL2dmZql27Ehv8dquha3e54oXrH0DjkVT6NgZsHCrEFtS5TvFtCW8I/s1600/5.ShowConsole.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhHoXieHeaQ1Xi1EYfec-Xo1QmTyKJ8mP5XrqTv_ZYsM8GMmMNAjPBLTRUF-dX6ll04-UxLkydJSPBG79qjMDldGfL2dmZql27Ehv8dquha3e54oXrH0DjkVT6NgZsHCrEFtS5TvFtCW8I/s400/5.ShowConsole.png" height="202" width="400" /></a></div>
<br />
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuoEbEF2xWsL9aekGGtbydlQ-EvGf8X0iH7Z8Nuv-_3BEkv2Mw94m6NDHp74L50McdDWr5Np3m56tEBSHgo5E59JbElzTbhzbPxA2KXLQLpj6yOrB5jN0gOmedetq669uhZgzfiThX_sg/s1600/6.ConsoleWindow.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjuoEbEF2xWsL9aekGGtbydlQ-EvGf8X0iH7Z8Nuv-_3BEkv2Mw94m6NDHp74L50McdDWr5Np3m56tEBSHgo5E59JbElzTbhzbPxA2KXLQLpj6yOrB5jN0gOmedetq669uhZgzfiThX_sg/s400/6.ConsoleWindow.png" height="151" width="400" /></a></div>
</li>
</ol>
</div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com43tag:blogger.com,1999:blog-7474095347317730182.post-60829475291662473402012-12-30T04:23:00.000-08:002013-06-12T09:37:49.425-07:00Setting up Java Development Environment (JDK) on Linux<div dir="ltr" style="text-align: left;" trbidi="on">
<br />
<div dir="ltr" style="text-align: left;" trbidi="on">
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Java Development Kit (JDK) is essential for developing Java applications on your computer. JDK consists of many tools which can make your development easy and comfortable, especially when you do not have an Integrated Development Environment (IDE) such as Eclipse or NetBeans.</span></div>
<span style="font-family: Verdana, sans-serif;"><br /></span><span style="font-family: Verdana, sans-serif;">Here, I will mention about setting up JDK on CentOS 64 bit Operating System. However, the steps of installation are common on most of the Linux flavors.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h3 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Steps:</span></h3>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Visit </span><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html" style="font-family: Verdana, sans-serif;">Oracle website</a><span style="font-family: Verdana, sans-serif;"> to download the installation file.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Click on the "Download" button at the JDK section to download the latest release of JDK. If you are looking for a previous version of JDK, you can visit the "Previous Releases" section on the same page. </span><span style="font-family: Verdana, sans-serif; font-size: x-small;">If you could not locate the section, you can search for "Previous" keyword on the page using the <i>Ctrl + F</i> key combination supported by most browsers.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Accept the license agreement and choose the download. Determine your Operating System architecture bit length by executing </span><span style="font-family: Courier New, Courier, monospace;"><b>getconf LONG_BIT</b></span><span style="font-family: Verdana, sans-serif;"> or </span><span style="font-family: Courier New, Courier, monospace;"><b>uname -m</b></span><span style="font-family: Verdana, sans-serif;"> command on console. </span><span style="font-family: Verdana, sans-serif;">CentOS uses RPM based package management. Hence, you can download either </span><span style="font-family: Courier New, Courier, monospace;">.rpm</span><span style="font-family: Verdana, sans-serif;"> or the zipped package. The steps of path configuration varies with the kind of package you choose.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Follow the below steps according to the package you have downloaded:</span></li>
</ol>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
<h4 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">RPM Installation:</span></h4>
<div style="text-align: left;">
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Switch to root user using </span><b><span style="font-family: Courier New, Courier, monospace;">su</span></b><span style="font-family: Verdana, sans-serif;"> command.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Copy the downloaded file to a location or navigate to the directory.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Use </span><span style="font-family: Courier New, Courier, monospace; font-weight: bold;">rpm -ivh jdk<version>-<XXbit>.rpm</span><span style="font-family: Verdana, sans-serif;"> to start the installation.</span></li>
</ol>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2rxac_5Py7aDshglZTmbBEapMZHVDpDbwM0Ulxw5inQolMCHUnMzg3QRHi1LiZyb128UVWlqPgjvA6VcuyNKGFzHMxvYODLdBjqrrmdDrFyDra2IU6I2Wve4lHB2r8sX0x-ZA2WHkWag/s1600/JDK_1.7_Installation_RPM.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh2rxac_5Py7aDshglZTmbBEapMZHVDpDbwM0Ulxw5inQolMCHUnMzg3QRHi1LiZyb128UVWlqPgjvA6VcuyNKGFzHMxvYODLdBjqrrmdDrFyDra2IU6I2Wve4lHB2r8sX0x-ZA2WHkWag/s400/JDK_1.7_Installation_RPM.png" width="400" /></a></div>
<div>
<span style="font-family: Verdana, sans-serif;"><br /></span></div>
</div>
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">While installing JDK 7, you may find the errors being displayed as in the above image. These can be ignored. You can verify the installation using the </span><span style="font-family: Courier New, Courier, monospace;"><b>java -version</b></span><span style="font-family: Verdana, sans-serif;"> command and you are done.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<h4 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Compressed Package Installation (tar.gz):</span></h4>
<div>
<div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguTaKRBQAsk3wCWoDWK3iBoWnl_sNuBk5frN1QraoL8gjdcMWV3xBD8YmTLPXFrKLDcQe7QAX0dzrlASmIimXGbL8Ii0KuhygFJ5cD4Qcf2vzcuaPUo3pWklC9C3ksFmSkKUcWdV2ChWs/s1600/JDK_1.7_Installation_tar.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEguTaKRBQAsk3wCWoDWK3iBoWnl_sNuBk5frN1QraoL8gjdcMWV3xBD8YmTLPXFrKLDcQe7QAX0dzrlASmIimXGbL8Ii0KuhygFJ5cD4Qcf2vzcuaPUo3pWklC9C3ksFmSkKUcWdV2ChWs/s400/JDK_1.7_Installation_tar.png" width="400" /></a></div>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Execute </span><b style="font-family: 'Courier New', Courier, monospace;">su</b><span style="font-family: Verdana, sans-serif;"> command to switch to root user.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Use </span><span style="font-family: Courier New, Courier, monospace;"><b>tar <download-path>/<jdk<version>-<XXbit>.tar.gz -C /usr/java</b> </span><span style="font-family: Verdana, sans-serif;">to extract the contents to the installation location. If </span><span style="font-family: Courier New, Courier, monospace;">/usr/java</span><span style="font-family: Verdana, sans-serif;"> does not exist, you may create the directory using </span><b style="font-family: 'Courier New', Courier, monospace;">mkdir /usr/java</b><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Once extracted the contents, you are done with the basic installation. You can follow the <a href="http://www.blogger.com/blogger.g?blogID=7474095347317730182#setpathvar">section</a> to set the </span><span style="font-family: Courier New, Courier, monospace; font-weight: bold;">PATH </span><span style="font-family: Verdana, sans-serif;">environmental variable.</span></li>
</ol>
<h4 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Binary File Installation:</span></h4>
</div>
<div style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Packages are available with </span><b><span style="font-family: Courier New, Courier, monospace;">.bin</span></b><span style="font-family: Verdana, sans-serif;"> extension. They can be installed by executing them from the shell.</span></div>
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Switch to root user with </span><span style="font-family: Courier New, Courier, monospace;"><b>su</b></span><span style="font-family: Verdana, sans-serif;">.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Copy the installation file to a location.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Add execution permission to the file using </span><span style="font-family: Courier New, Courier, monospace;"><b>chmod u+x jdk<version>-<XXbit>.bin</b></span><span style="font-family: Verdana, sans-serif;"> command.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Execute the file using shell. </span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuQjtEdxGVtRGztc7wLgqUbGGSOrYPVPzCH75BFlJSd2HdybhDbV3I2m198aMG5Y0NKxrjdvQj5yj7cpOJ2Ug2x4nCgDiWu9aNFAb6OFbsVRXI8-6W3abRfUrGk7rjgoKG1mc0HMqcObE/s1600/JDK1.6_bin_1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhuQjtEdxGVtRGztc7wLgqUbGGSOrYPVPzCH75BFlJSd2HdybhDbV3I2m198aMG5Y0NKxrjdvQj5yj7cpOJ2Ug2x4nCgDiWu9aNFAb6OFbsVRXI8-6W3abRfUrGk7rjgoKG1mc0HMqcObE/s400/JDK1.6_bin_1.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Follow the prompts for installation.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Move the installation folder to </span><span style="font-family: Courier New, Courier, monospace;">/usr/java</span><span style="font-family: Verdana, sans-serif;">. Create the folder if it does not exist.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA8j7Xrp8kto6bmaQSB53C7PdflyEvO5WDDptDh7bpmnkSmhANwHLhu6pR9O-HZvlArATn23GD3vz-JXG-jgtH6qbqL16-bz1vDlu3m7DwnZc9XapHxtMnb3Usb0slaoWI2oLaWwIRlVI/s1600/JDK1.6_bin_2.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhA8j7Xrp8kto6bmaQSB53C7PdflyEvO5WDDptDh7bpmnkSmhANwHLhu6pR9O-HZvlArATn23GD3vz-JXG-jgtH6qbqL16-bz1vDlu3m7DwnZc9XapHxtMnb3Usb0slaoWI2oLaWwIRlVI/s400/JDK1.6_bin_2.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Follow the steps in the <a href="http://www.blogger.com/blogger.g?blogID=7474095347317730182#setpathvar">section</a> to set the </span><span style="font-family: Courier New, Courier, monospace;"><b>PATH</b></span><span style="font-family: Verdana, sans-serif;"> variable.</span></li>
</ol>
<br />
<br />
<a href="javascript:void()" id="setpathvar"> </a>
<br />
<h4 style="text-align: left;">
<span style="font-family: Verdana, sans-serif;">Setting PATH variable:</span></h4>
<div>
<span style="font-family: Verdana, sans-serif;">While installing the package other than from rpm, you will need to set the </span><span style="font-family: Courier New, Courier, monospace;"><b>PATH</b></span><span style="font-family: Verdana, sans-serif;"> environmental variable to run the java development tools from console. Extra care should be taken while performing the steps below.</span><br />
<span style="font-family: Verdana, sans-serif;"><br /></span>
<br />
<ol style="text-align: left;">
<li><span style="font-family: Verdana, sans-serif;">Copy the path where you have java installed; remember to omit the '/' at the end. Usually on </span><span style="font-family: Courier New, Courier, monospace;">/usr/java/jdk<version></span><span style="font-family: Verdana, sans-serif;">. This becomes the path for </span><span style="font-family: Courier New, Courier, monospace;">JAVA_HOME</span><span style="font-family: Verdana, sans-serif;"> environmental variable.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPfYf8sQ7PodnZd5CsXfImosvfDiVBA5-QlxlIWey5_kZD99cG-mWJnSnKiCgVqIcbwkImkJNRFPLY0zaL_f3Y8zYBuiV_G_FFyuyb5X6QcO_zL6Cn29imqfZjc1_yVw2fxsPwa0Q0c8w/s1600/JDK_1.7_Installation_tar1.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhPfYf8sQ7PodnZd5CsXfImosvfDiVBA5-QlxlIWey5_kZD99cG-mWJnSnKiCgVqIcbwkImkJNRFPLY0zaL_f3Y8zYBuiV_G_FFyuyb5X6QcO_zL6Cn29imqfZjc1_yVw2fxsPwa0Q0c8w/s400/JDK_1.7_Installation_tar1.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Edit </span><span style="font-family: Courier New, Courier, monospace;"><b>/etc/profile</b></span><span style="font-family: Verdana, sans-serif;"> file using an editor such as </span><span style="font-family: Courier New, Courier, monospace;">vi</span><span style="font-family: Verdana, sans-serif;"> to set the </span><b><span style="font-family: Courier New, Courier, monospace;">PATH</span></b><span style="font-family: Verdana, sans-serif;"> variable for all users, on their log in. Locate the section where the </span><b><span style="font-family: Courier New, Courier, monospace;">PATH</span></b><span style="font-family: Verdana, sans-serif;"> variable is exported.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi73An6j5kwuT1DfHbJjgFX9-xAxmN4Si-1p57AJpr-w3vrQNo9plNjXcTx3M_wCknvWK7oksUBAEVjxqCzbUX5SEHim5Sr2o615rnn5N4sR1XCkyhzpXbN4TZY_6xYG4v_WKJbAho5dQY/s1600/JDK_1.7_installation_tar3.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi73An6j5kwuT1DfHbJjgFX9-xAxmN4Si-1p57AJpr-w3vrQNo9plNjXcTx3M_wCknvWK7oksUBAEVjxqCzbUX5SEHim5Sr2o615rnn5N4sR1XCkyhzpXbN4TZY_6xYG4v_WKJbAho5dQY/s400/JDK_1.7_installation_tar3.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Add line defining </span><span style="font-family: Courier New, Courier, monospace;"><b>JAVA_HOME</b></span><span style="font-family: Verdana, sans-serif;">, append </span><b><span style="font-family: Courier New, Courier, monospace;">$JAVA_HOME/bin</span></b><span style="font-family: Verdana, sans-serif;"> to </span><b><span style="font-family: Courier New, Courier, monospace;">PATH</span></b><span style="font-family: Verdana, sans-serif;"> variable and include </span><b><span style="font-family: Courier New, Courier, monospace;">JAVA_HOME</span></b><span style="font-family: Verdana, sans-serif;"> to the export command.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm1ymaPAfLnTgagrljvltdYRtVHdkDtnT98vpobypwxW51OhrDaBHH_WhapJ-0ADyBDQqPfiEG4VRLmvxU_mbHhrho-Hd6UgrBz3-v3ncj_wiXKi5dUAX5zdBg1GN42oLNUxfNgxRgIuY/s1600/JDK_1.7_installation_tar4.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="265" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhm1ymaPAfLnTgagrljvltdYRtVHdkDtnT98vpobypwxW51OhrDaBHH_WhapJ-0ADyBDQqPfiEG4VRLmvxU_mbHhrho-Hd6UgrBz3-v3ncj_wiXKi5dUAX5zdBg1GN42oLNUxfNgxRgIuY/s400/JDK_1.7_installation_tar4.png" width="400" /></a></div>
</li>
<li><span style="font-family: Verdana, sans-serif;">Save the file and exit the editor.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Source the file to reflect the changes in the current console window.</span></li>
<li><span style="font-family: Verdana, sans-serif;">Verify if </span><span style="font-family: Courier New, Courier, monospace;">java</span><span style="font-family: Verdana, sans-serif;"> command works.</span><div class="separator" style="clear: both; text-align: center;">
<a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt0Wg3jHLsLooH3Km72HXEqDWPxR7mVDK7xx4q36PfyCAPMuQ53eUVbWboANYyRhS7r9Q3bqhSLstJywCRYecJFUNgMmigonos3GceUSGlAGGyd2E4uwVtBdzSjklFr-jIMInY4IjpKPA/s1600/JDK_1.7_Installation_tar5.png" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="281" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjt0Wg3jHLsLooH3Km72HXEqDWPxR7mVDK7xx4q36PfyCAPMuQ53eUVbWboANYyRhS7r9Q3bqhSLstJywCRYecJFUNgMmigonos3GceUSGlAGGyd2E4uwVtBdzSjklFr-jIMInY4IjpKPA/s400/JDK_1.7_Installation_tar5.png" width="400" /></a></div>
</li>
</ol>
</div>
</div>
</div>
</div>
James Jithinhttp://www.blogger.com/profile/03645657682299592077noreply@blogger.com3