Tuesday, March 19, 2013

Setup your Java Development Environment in Windows 7


I have been receiving a lot of requests over the time, from students and people willing to experiment with the Java language, on providing a simple guide on how to setup a Java development environment , similar to the one I’ve written a year ago for Mac users. See here and here. So, this post is mainly addressed to people new to Java development, that seek information about setting up their Windows (7 or similar) environment, with all the basic tools and settings, in order to start writing Java code and experiment with various tools and technologies in the Java ecosystem.


I am pretty sure there are numerous similar resources out there, I just thought to add one more. Hope you find it handy. It is really easy, so don’t be afraid of the this post’s size, I just tried to elaborate on some simple steps, but for sure it is not rocket science


and you don’t have to be a ‘hacker’ in order to complete all the steps. You may not need all the tools below, so it is not necessary to have them all installed at once.
Index


1. Setup Java JDK and JAVA_HOME (this is a must)

2. Setup Apache Ant and ANT_HOME (optional)

3. Setup Apache Maven and MAVEN_HOME (optional)

4. Setup SVN (subversion) support, using TortoiseSVN (optional)

5. Setup Git using Git Extentions (optional)

6. Setup a Java IDE, Netbeans or Eclipse (this is a must)
1. Setup Java JDK and JAVA_HOME


I assume that you have not installed any related tool before, and your system is ‘clean‘ from Java and other Java development tools. So I guess, if you open a Command Prompt and you type ‘java -version’ you will get something like the following.





We need to install the Java Development Toolkit aka JDK, which is bundled with the Java Runtime Environment. At this moment, that I am writing this post the latest JDK versions is JDK 7u17 (update 17 that is). All you have to do is head to the main download page provided by Oracle (here) , and download the latest version that you will find. The direct link for the JKD 7u17 is here.Start the installer and go through the steps.


Tip: One small tip or let’s say a deviation from the installer that I prefer, is to change the installation path provided by the program to something easier to remember or even type in the command line tool. So instead of installing the JDK to ‘C:\Program Files\Java\jdk7_17‘ etc, i always choose something that looks like the following, ‘C:\Java\jdk17‘.


I do the same when the installer prompts me to install the JRE (java run time) and I provide the following path ‘C:\Java\jre7‘. When it is complete, I open a new command line and I re-type ‘java -version‘ as before. As you can see the installer has already done a good job and I have java in my environment. (You may use the ‘where‘ command to find out which executable is loaded on the Path – (similar to ‘which‘ in Unix) ).




We are almost there, we just need to set, one more environment variable, that is very important by many tools that are based on Java or require Java to work. It is the famous JAVA_HOME variable and you must have heard it or seen it in various tutorials and setup resources. It is easy though! Open your Advance System Settings ( Press Start , type in the search box ‘View Advanced System Settings‘, open it, select the tab ‘Advanced‘ and press the button in the bottom called ‘Environment Variables’). Add a new system variable as it is illustrated below. The name is JAVA_HOME and the path is actually the path our JDK installation, in my case ”C:\Java\jdk17‘.



Press OK, and open a new Command prompt ( it is required). Type ‘echo %JAVA_HOME%‘ and you are done.




Congrats you have successfully set up the java run time and the jdk environment! Let’s move on.
2. Setup Apache Ant and ANT_HOME


Apache Ant is a popular build tool used in many Java projects and it is still needed in many cases when you need to setup some servers or complex projects. You can find more about Ant here. Installing Ant is easy, there is no fancy installer just a zip file you need to extract somewhere and add a similar varible like JAVA_HOME (see above) called ANT_HOME. At the time being, the latest version of Apache Ant is, 1.8.4. and can be downloaded here. After downloading the zip file, I choose to extract the contents to a similar path like before ‘C:\ant184‘. (make sure that c:\ant184 is the root folder and then you have all the sub-folders like c:\ant184\bin’. So, now we must set our ANT_HOME variable and add the ‘ant’ executable to our windows Path so that we can invoke it in our command line by typing ‘ant’. Setting ANT_HOME is similar to JAVA_HOME, add a new system variable named ANT_HOME and value the path where you have the ant zip extracted in my case ‘C:\ant184‘. See below





In order to add the ‘ant’ command to our executable path we need to update the PATH system variable, in the very same window, as illustrated below, just hit ‘Edit’. Go to the end of the line and add a new entry (all the entries are separated with ‘;’ ). The path for the ant executable is ‘C:\ant184\bin‘ but we will use the ANT_HOME variable in order to be more flexible so we will add, %ANT_HOME%\bin”. See the image below.





You are done, open a new Command Prompt, and type ‘ant -version‘, or ‘echo %ANT_HOME%‘ you should get similar results are illustrated in the image below.





Congrats, you have successfully installed Apache Ant! Some Java IDE’s have Apache Ant already pre-bundled (as we will see in a later section) but I truly believe it is better to be installed manually,it is cleaner and easier to maintain, update.
3. Setup Apache Maven and MAVEN_HOME


Apache Maven is very popular tool among java developers, it does many things and has many uses. It is a building tool, a library dependency tool, a software project management and comprehension tool as it is indicated in the tool’s site. You may find more about maven here. The installation of maven is similar to Apache’s Ant, just a zip extracted somewhere and setting up a couple of variables + updating the system path. The latest version at the moment is 3.0.5 and can be downloaded here. There is already a short and handy setup guide provided in Maven’s site (see here), I am going to write similar steps. After downloading the zip file, I choose to extract the contents to a similar path like before ‘C:\maven305‘. (make sure that c:\maven305 is the root folder and then you have all the sub-folders like c:\maven305\bin’. So, now we must set our MAVEN_HOME variable and add the maven executable to our windows Path so that we can invoke it in our command line by typing ‘mvn‘. Setting MAVEN is similar to JAVA_HOME or ANT_HOME, just add a new system variable named MAVEN_HOME and value the path where you have the ant zip extracted in my case ‘C:\maven305‘.


See below.





In order to add the ‘mvn’ command to our executable path we need to update the PATH system variable like we did previously, in the very same window, as illustrated below, just hit ‘Edit’. Go to the end of the line and add a new entry (all the entries are separated with ‘;’ ). The path for the maven executable is ‘C:\maven305\bin‘ but we will use the MAVEN_HOME variable in order to be more flexible so we will add, %MAVEN_HOME%\bin”. See the image below.





You are done, open a new Command Prompt, and type ‘mvn -version‘, or ‘echo %MAVNE_HOME%‘ you should get similar results are illustrated in the image below.





Congrats, you have successfully installed Apache Maven 3!Some Java IDE’s have Apache Maven already pre-bundled (as we will see in a later section) but I truly believe it is better to be installed manually,it is cleaner and easier to maintain,use or update.
4. Setup Subversion (version control) using Tortoise SVN


Subversion is a popular versioning system among Java developers. You may find further information about it here. There are numerous subversion clients that you may install in your computer. The original subversion command line client can be found here (for windows). All of the Java IDE’s come with support of SVN either with pre-bundled clients or plug ins. I tend to use the clients offered by my IDE but I always want to have an extra client that gives me extra flexibility on certain things. I think that tortoise svn is an excellent choice for Windows users, it has full SVN support and excellent integration with the Windows file system.

You find the latest version of the client here, current is 1.7.11 (compatible with SVN 1.7.8). Download the executable and follow the steps. The only addition on the default settings is that I select to install the command line tools as well, as illustrated in the image below.




After completing the installation you can access Tortoise, using your right click in any folder, like the image below.





As I have already pointed out, most of the Java IDE’s around offer SVN integration, out of the box but is always a good thing to have an extra client, in your windows shell. Tortoise is a great one, you can actually work with tortoise and skip your IDE’s integration if you like so! If you don’t like Tortoise and you want to consider yet another client then I suggest you have a look on the free edition of SmartSVN.
5. Setup Git (version control) using Git Extentions


Git, is another version control system that is becoming very popular among developers in general. In many cases it is replacing SVN, so there is a high chance you might be required to work with it, or work in projects that store their code on a remote Git repository or contribute to an open source project at GitHub. You may find more about Git here.


Git is the new kid on the block on popular versioning systems, so some tools or support still may not be so user friendly comparing to tools and clients of SVN. Git is also encouraging users to interact with it, using the command line interface. The stand alone git installation for windows can be found here. This will install the git client and support tools and you may start experiments with it, using the command line. If you want a more UI friendly way, then you may choose one of the free clients. (so, don’t install the standalone client).


My choice at the time being is Git Extentions, which can be found here. The nice thing about Git Extentions is that with one downloadyou get the native git client and all the nice UI tools, similar to Tortoise featured in the previous section. So you can just download the git extensions msi package, and follow the installer’s step – just make sure to enable (when you are prompted to do so) the mygit support. See the image below.





I have disabled any visual studio support, since i am not going to to install this particular IDE. On the next step, I select Putty to be installed as my ssh client.




On the related prompt of the Git installer, about altering your System’s Path, I always select the second one, so that the ‘git’ command is added to my command line exec path. See the image below.



Continue with the rest of the installation and the various tools installed. The provided, default selections are safe to use.



As you can see, by the end you are going to have the related icons, copied in your Desktop. Congrats you have full Git support on your development environment! Double click on the Git Extentions icons and set the final details like language of use and username / email to be used while interacting with git repositories.
6. Pick your Java IDE : )


Up until now we have installed a variety of tools towards setting up our Java Development environment. Since the JDK is already installed (from step one) we could actually jump to coding just by using our text editor of choice (NotePad++, TextPad, NotePad, Ultra Edit etc) and invoking the javac and java commands from the command line. It is good to know that this is an option but I would highly recommend you start doing your experiments using a real IDE, and there are a lot of them in the Java development world and are considered very very powerful.
Netbeans


If you are a total newbie and you have just started using or learning Java, then your first stop, according to my opinion should be Netbeans. It is one of the best Java IDE’s around, used by many professional java developers all around the world and is really targeting towards ease of use and productivity from the very beginning. You can find Netbeans (currently latest version 7.3) here. If you are about to work with some more advanced java stuff like the Java Enterprise edition (aka Java EE) then I think you should try downloading and install Netbeans with Java EE support and not the simple version. Upon installation it will prompt you about the bundled Java EE container (aka application server) that can install for you. My choice is GlassFish.


See the image below.




Continue with the rest of the installer prompts, your already existing JDK installation from step 1 should be picked accordingly by the setup script and be used by the IDE as well. The image below illustrates a fresh install of Netbeans running.




If you are looking for a quick guide or tutorial on using Netbeans, have a look here or have a look on numerous videos on youtube, here.
Eclipse


Eclipse is along with Netbeans, one of the most famous Java IDE’s in the whole Java development community. It’s user (installation base) might be larger comparing to Netbeans and is usually the IDE of choice for hard core Java developers. I am currently an Eclipse user as well, but I personally don’t find it a good fit for someone that is now starting to ‘play‘ with Java. There is a large community behind it, (as with Netbeans) numerous resources and support forums. It is not that hard, but if you are a new developer and you are still trying to make your way into basic stuff there is some possibility that you lose your way and get disappointed, something that we don’t want.


I don’t want the above paragraph to be the start of a flame war especially for hard cord eclipse users, I am actually an eclipse user myself, I just think that Netbeans is more ‘newbie’ user friendly, and it is critical that new java developers don’t get disappointed after trying to complete their first assignment or task in Java.The latest version of Eclipse currently is Eclipse 4.2 SR2 (codename Juno), and the downloads page can be found here. You can either select the Eclipse for Java or JEE developers download, depending on your assignments/tasks or interest. Eclipse does not pre- install any servers or support tools as such. All the extra functionality should be added afterwards through the Plugin system that offers. (See Eclipse Market place). Eclipse does not have any installer, it comes as a zip file, all you have to do, is download the zip and extract the contents (like we did with Ant, or Maven). I usually pick a simple folder, like in previous section e.g ‘C:\eclipseJuno42′ . Open the folder and you will find the executable (eclipse.exe). You should have something like the image below.




Most popular application servers


This is the second post in the series where we publish statistical data about the Java installations. The used dataset originates from free Plumbr installations out there totalling 1,024 different environments we have collected during the past six months.
First post in the series analyzed the foundation – on what OS the JVM is run, whether it is a 32 or 62-bit infrastructure and what JVM vendor and version were used. In this post we are going to focus on the application servers used. It proved to be a bit more challenging task than originally expected – the best shot we had towards the goal was to extract it from the bootstrap classpath. With queries similar to “grep -i tomcat classpath.log”. Which was easy. As opposed to discovering that:


Out of the 1024 samples 92 did not contain a reference to bootstrap classpath at all. Which was our first surprise. Whether they were really ran without any entries to bootstrap classpath or our statistics just do not record all the entries properly – failed to trace the reason. But nevertheless, this left us with 932 data points. Out of the remaining 932 we were unable to link 256 reports to any of the application servers known to mankind. Before jumping to the conclusion that approx. 27% of the JVMs out there are running client side programs, we tried to dig further
  • 57 seemed to be launched using Maven plugins, which hides the actual runtime from us. But I can bet the vast majority of those are definitely not Swing applications.
  • 11 environments were running on Play Framework, which is not using Java EE containers to run.
  • 6 environments were launched with Scala runtime attached, so I assume these were also actually web applications.
  • 54 had loaded either jgoodies or swing libraries which make them good candidates for being a desktop application
  • 6 were running on Android. Which we don’t even support. If you guys can shed some light on how you managed to launch Plumbrwith Android, let us know.
  • And the remaining 122 – we just failed to categorize – they seemed to range from MQ solutions to batch processes to whatnot.
But 676 reports did contain reference to the Java EE container used. And results are visible from the following diagram:
Application Server Marketshare
The winner should not be a surprise to anyone – Apache Tomcat is being used in 43% of the installations. Other places on the podium are a bit more surprising – Jetty coming in second with 23% of the deployments and JBoss third with 16%. The expected result was exactly reversed, but apparently the gears have shifted during the last years.


Sunday, December 9, 2012

Chapter 3. SQL Essentials - D

Transactions

A transaction is the logical unit of work when performing database operations. A transaction is created by grouping a sequence of one or more DML statements into a single, all-or-nothing proposition with the following characteristics.
  • Atomicity— All statements must complete successfully or the entire set is aborted.
  • Consistency— The statements' net effect is to leave the database in a consistent state.
  • Isolation— Intermediate statements should not be visible to other transactions.
  • Durability— When the transaction is complete, the changes are made permanent.
These four characteristics of a transaction are often abbreviated ACID. To ensure the consistency of the data, we need to identify the boundaries of a transaction and the SQL statements it comprises. Oracle then does the rest of the work necessary for ensuring that the transaction passes the ACID test.
The environment in which we are executing SQL, whether it's SQL*Plus, SQLJ, or JDBC, normally allows us to execute our statements in one of two modes, manual commit or auto-commit. In auto-commit mode, changes made to the database with DML statements are immediately made permanent in the database and are immediately visible to other transactions. In other words, transactions can include only a single SQL DML statement. In this case, Oracle ensures that the transaction meets the ACID requirements.
In manual commit mode, Oracle provides isolation and durability. Changes made to the database with DML are not visible to other transactions and are made permanent only once the transaction has been successfully completed. We take responsibility for atomicity and consistency. We determine what set of DML statements we want to group as an atomic set, and we perform any intermediate validation or error checking that might be necessary.
We group DML statements by marking the boundaries of the transaction—the beginning and the end. Oracle has no statement to mark the start of a transaction explicitly; a transaction begins either at the start of a session (or connection) or after the previous transaction has ended by being committed or cancelled. There are two statements used to mark the end of a transaction:
  • COMMIT— Save the results of the transactions permanently in the database.
  • ROLLBACK— Abort the transaction and return the database to its state at the beginning of the transaction.
In addition, DDL statements, such as CREATE TABLE, ALTER TABLE, and DROP TABLE, have an implied COMMIT.

A Transaction: All or Nothing

SQL*Plus, like other interfaces to Oracle, has an auto-commit feature. When this feature is enabled, every SQL statement is automatically committed as it is entered. By default, auto-commit is off in SQL*Plus, so we can manually control a transaction by entering a sequence of SQL statements, then decide at the end whether to call COMMIT or ROLLBACK.
In other interfaces, we need to be aware of the default and change it if necessary to support transaction processing. In SQLJ, auto-commit is off by default. In JDBC, it is on by default, meaning that each SQL statement we send to the database is committed automatically; this is less efficient and less flexible than auto-commit.
Suppose that we are adding a CD to our CD collection by typing SQL statements directly into SQL*Plus. We need to update two tables—the CD_COLLECTION table, which contains information about the CD as a whole, and the SONGS table, which contains information for all the songs. We don't want to have incomplete information available in the database, so we'll consider the SQL statement that inserts the CD information and the SQL statements that insert each of the songs into the database as a single transaction.
Assuming that the session just started or that we have just called either COMMIT or ROLLBACK for the previous transaction, we are ready to begin a new transaction:
INSERT INTO CD_COLLECTION
(CD_ID, ARTIST, ALBUM_TITLE, RELEASE_DATE, COUNTRY, LABEL)
VALUES
(100, 'PJ Harvey','Rid of Me','1-JAN-1993','UK','Island');

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
(1,100, 'Rid of Me','Harvey',1);

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
VALUES (2, 100, 'Missed','Harvey', 2);

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
VALUES (NULL, 100, 'Missed','Harvey', 3);
Suppose that, at this point, we realize it is far too tedious to enter each song manually and decide we'll write a script to do this, instead. We can undo all that we've done so far by typing ROLLBACK. All of the records that we entered up until that point in this session will be removed from the database.
If, on the other hand, we decide we'll just pick up from here later, either with a script or manually, we would commit the transaction so far by entering:
COMMIT;
Up until the time we enter the COMMIT statement, the records we enter into SQL*Plus are not visible in other users' sessions. We can stop partway through, for example, and query the database, and we will see any records we have entered into the CD_COLLECTION table or the SONGS table. But nobody else can see the new records. You can verify this by opening another SQL*Plus session and querying the databases in the second session. You will find that until you type COMMIT in the first, you will not see the updates in the second.

Partial Rollbacks

In addition to allowing us to commit or roll back a group of SQL statements as a whole, Oracle also allows us to mark intermediate points to allow a partial rollback. The commands to do this are:
  • SAVEPOINT savepoint_name— Names a point to which we can selectively roll back a transaction.
  • ROLLBACK TO savepoint_name— Rolls back a transaction to previously named SAVEPOINT.
This is not as generally useful as the basic COMMIT/ROLLBACK arrangement, but it allows creating a more sophisticated scheme for recovering from a partial failure while processing a transaction.
For example, suppose that we have an application with a user interface that allows a user to enter the information for a CD. Suppose we will allow the users to enter just the general CD information or the general information and all the songs. Now suppose that a user tries to enter information and songs, but an error occurs while inserting the songs. We can prompt the user with three choices:

  1. Save just the general CD information.
  2. Try to pick up where the error occurred.
  3. Abandon the entire transaction.

We won't present the application code here to support this logic, but here is a sequence of SQL statements that could cause an error like this—notice NULL in the third INSERT into the SONGS table:
INSERT INTO CD_COLLECTION
(CD_ID, ARTIST, ALBUM_TITLE, RELEASE_DATE, COUNTRY, LABEL)
VALUES (100, 'PJ Harvey','Rid of Me','1-JAN-1993','UK','Island');
SAVEPOINT CD_INFO;

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
(1,100, 'Rid of Me','Harvey',1);
SAVEPOINT SONG_1;

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
VALUES (2, 100, 'Missed','Harvey', 2);
SAVEPOINT SONG_2;

INSERT INTO SONGS
(SONG_ID, CD_ID, SONG_TITLE, COMPOSER, TRACK)
VALUES (NULL, 100, 'Missed','Harvey', 3);
SAVEPOINT SONG_3;
Because the third insert is missing its primary key, this causes an error. At this point, if the user chose option 1, to save just the CD information, we would perform a partial rollback and commit:
/* Save just CD information */
ROLLBACK TO CD_INFO;
COMMIT;
If the user chose instead to pick up from where the error occurred, we could roll back to SONG_2, the last successful insert, and let the user start again from there:
/* Start again just before first error */
ROLLBACK TO SONG_2;
/* Continue inserting more songs ... */
If the user chose to abandon the entire transaction, we would use an unqualified ROLLBACK, which returns the database to its state prior to starting the transaction:
/* Abandon transaction entirely */
ROLLBACK;
Partial rollbacks should be approached cautiously. It is easy to create complicated scenarios that can be hard to manage and which can leave the application in an invalid or undetermined state from which it is impossible to recover.