Saturday, June 01, 2013

Running Java And Groovy on Windows And Linux

Groovy, Another Language that runs on Java Virtual Machine. It supports both Static and Dynamic Typing. If you haven't heard about this language till now. No Problems!

As, Groovy runs on VM so you must install Java first. Groovy doesn't require you to be a Java Programmer but some hands-on will help you learn it fast. Those who already have installed Java must set the JAVA_HOME Environment Variable. Here is how you do.

On Windows Machine:

Right-Click My Computer -> Properties -> In Windows XP (Advanced) | In Windows 7 (Advanced System Settings) ->  Environment Variable -> New

Here You will see Two Panes. If you select the New on the First One it will set User-Specific Setting i.e., Only the Currently Logged In User can use Java and Groovy.

If you Choose the New from the System Variables, It will be available to all the existing users as well all users that you create later. Choose what you prefer.

Note: System Variables already Contains PATH Key/value pair. Never remove the existing value instead append to it or you end you with an unstable machine. If you are unsure always create the NEW PATH Key/value in the First Pane that way you are sure that your System Settings remains untouched.

Create a New Variable anywhere you prefer.

Variable Name:  JAVA_HOME

For Example: I have installed Java in C:\jdk1.6

Variable Name:  JAVA_HOME
Variable Value:  c:\jdk1.6 \\ Never put a Semi-Colon at the end.

Once Done Press OK.

Now Create one more Environment Variable. Again by Clicking New any place you prefer:

Variable Name:  CLASSPATH
Variable Value:  .;%JAVA_HOME%\lib;

Note: You must put a Semi-Colon at the End. Press OK. The Reason to put a Semi-Colon here is a good practice as you might in near future append a new library to your CLASSPATH just to separate one from another you put a Windows PATH Separator.

Again, If you are unsure which pane to choose for this one always prefer the first one and add the below Key instead of updating your existing System Variable PATH key in global/system settings that way it will remain untouched.

Variable Name:  PATH
Variable Value:  %JAVA_HOME%\bin;

Note: Never Override a Key's Value always append to it.

On Linux Machine:

There is a ~/.bash_profile that is a start-up script which generally runs once. People find it common place to add environment variables such as PATH, JAVA_HOME, create aliases, etc.

Please remember the file ~/.bashrc is similar but ~/.bash_profile runs for Bash Shell whereas, ~/.bashrc runs for every Bash Shell.

Similarly, You can setup for all users i.e., Global Settings to do so you need to add the following entries mentioned below in /etc/profile.

Fire your Terminal or Console: I'm using ~/.bash_profile you can choose what you prefer.

Type: $ vi ~/.bash_profile

export PATH=$PATH:$JAVA_HOME/bin

You have to logout and login back to see the new changes in effect. Alternatively you can type the below command to see the changes immediately.

$ source ~/.bash_profile


$ . ~/.bash_profile

Always Remember always place Binary files i.e., files that are executable in the PATH variable irrespective you are working on Windows or Linux OS. CLASSPATH is for libraries.

Now it's time to download Groovy. Please head over to their Site to download it and all we need is to extract it and add/update some Environment Variables. The same way we did for Java.

Groovy Site:

You need to Add a New Environment Variable.

Variable Name:  GROOVY_HOME

Append to PATH Variable,

Windows Users: %GROOVY_HOME%\bin;
Linux Users: $GROOVY_HOME/bin

Make sure that there is a Semi-Colon (Windows), Colon (Linux Users) at the end before you append these values to it. You can confirm this using the echo command which works on both windows and Linux.

Windows Users: echo %PATH%
Linux Users: echo $PATH

If it's not present you must provide it otherwise it will not work. For Example: Instead of doing $GROOVY_HOME/bin
you do :$GROOVY_HOME/bin.

Remember Colon is a Linux Path Separator whereas, Semi-Colon is Windows.

Now As all the installations are done now we head over to test if everything is working fine or not.

Windows Users, Please open Command Prompt close any if already Opened. Linux Users either use the source command to make the settings available to existing Console or login again.

Now, Before we proceed any further we must check our installation.

At prompt or Terminal or Console,


javac -version // To See if the Java Compiler is Working or not.
java -version // To See if the java execution environment is ready to run anything you throw at it.

groovyc -version // To See if the Groovy Compiler is Working or not.
groovy -version // To See if the Groovy execution environment is ready to run anything you throw at it.

groovysh -version // It's something that only Groovy had not Java. It can execute commands you throw at it. It's a REPL - Read Evaluate Print Loop.

groovyConsole // It's a GUI Version.

I hope you don't ran into any problem. Next Article is coming up on Groovy.

Hope this Helps!
Please leave your comments.

1 comment: