Thursday, July 22, 2010

Batch scripts in action - Java LAB

As I mentioned in earlier blog the batch scripts are very helpful. Below is another incident when Command-prompt-programming comes to rescue.

Java newbies know the usual procedure to compile and run Java program
  •  Open Notepad
  •  Write program(Optional) and save it as something.java
  •  Open CMD
  •  Run following command "set path=c:\program Files\Java\jdk1.6.0\bin"
  •  Navigate to your folder having something.java
  •  Run command "javac something.java"
  •  Run command "java something"

Thats a lot of procedure. Lets put Batch to help.

One more thing. I got to make folder "Lab_DATE" every lab and do respective practicals in them. Hence I created "start.bat" with following obvious commands.

mkdir Lab_
cd Lab_
cmd notepad
set path=C:\Program Files\Java\jdk1.6.0\bin

But it had a problem. The CMD window closed after executing last command. I needed it live after changing the path. Try it and you will know.

Help was the first thing that came to this computer engineers mind. Running "cmd /?" yields help. Thankfully there is /K switch that keeps terminal live after executing command. Finally tweaking start.bat to

mkdir Lab_
cd Lab_
cmd /C notepad
cmd /K set path=C:\Program Files\Java\jdk1.6.0\bin

did the trick... "Cut the crap" in my words.

I am still searching for some script that would write my Java program ;-)

Batch scripts in action - Algorithms LAB

Lets have a look @ batch scripts aka batch programming.

I have algorithm lab - C programming.

On very first day new instructions were given - You must create new folder every time named "Lab_DATE" and do respective programs in them. First assignment was to write a C program that performed Quick sort.

As soon as task was fixed, my lab-mates pounced on traditional C editor - Blue-screen and rectangular cursor. I rather prefer to write my program in notepad and then later debug and run in C environment. Notepad is quick, easy copy and paste ;-) , clean interface and much more.



So my actions were prefixed to be performed in every lab,
  •  Create new folder every time
  •  Open notepad and write program
  •  Open that program in C

I thought of making a batch script that would do all these for me.

Quickly, I opened notepad and typed these commands.

mkdir Lab_
cd Lab_
notepad test.c
C:\tc\bin\tc.exe test.c

Next thing to do is save it as "start.bat". Job's done.

Just two lazy clicks on start.bat
  •  Creates "Lab_" folder
  •  Goes into folder
  •  Starts notepad with test.c file
  •  Once program is written and notepad is closed, the program opens in C editor ready for CTRL+F9.

Jobs well done but I am still finding script that would write a Quick sort program Quickly ;-)

Wednesday, July 21, 2010

Evolution Of Languages

I read the first chapter of Java-The complete Refernce.

Apart facts, this topic was most interesting.


Long ago, Before computers were invented . . . Just joking, Not so long ago lets us see evolution of first language Fortran

Scenario: Computers were limited to big companies.Average people still did not have the smell of computers. Small softwares were needed. Assembly language prevailed at that time and there was dire need of another programming language which was easy to debug.

Hence evolved Fortan.



Years passed. Now computers were becoming public. But yet no language was able to tackle new problems. Moreover Fortan was using GOTO - the most dark word in world of programming. So code had many GOTO's and jumps made larger programs intractable. It was called Spaghetti code. Lets see birth of revolution "C".

Scenario: There were several languages, Each for different purposes but none robust. Hence there was a need of ONE-FOR-ALL language that had definite structure.

Hence evolved C.

It was a total revolution. It was structured, It was robust, It was efficient and it was one-for-all. But one factor that C had and none language possessed was the feeling of "Programmers Language".



C was dominant for many following years. But environment was changing. Lets see evolution of C++ the first OOP language.

Scenario: Hardware was out in public. Programs were becoming gigantic and difficult to handle. Procedure orientation could not handle the growing complexity. At the same time new concept of OOP was introduced to handle prevailing complexity.

Finally something struck Stroustroup's mind and C++ evolved.



Everybody was happy with C++. Once again the same thing - Environment was changing.

Computers were common now. Internet was about to bloom. But now C++ was lagging the upcoming need, Portablity.

Scenario: Machines were out and so was variety among them. Variety of processors, variety of OS were bothering software vendors. Every new architecture or OS would require new COMPILER - difficult piece of software. At the same time internet was blooming. This opened a new dimension to software world. Web apps were failing to satisfy large diverse internet audience in multiple factors like security, ability to run on all OS and all architecture, Compactness and ability to run quickly.

Thus some brains @ Sun started to work and finally announced Java.

Java had it all. Portability to run on any PC, Structured, Ability to handle complexity, Robustness and blah blah blah....


Lets see what do current scenario demands. I am ready for it ;-)

Saturday, July 17, 2010

Environment Variables in Windows


Being a computer engineering student, I had to face Java language this year.

On the first lab some instructions were given to run commands prior compiling (Setting path.)
set path=c:\program files\java\jdk1.6.0\bin

Surprisingly (for me) just running javac command then worked. I knew there was no command called "javac" and the current directory in cmd did not have any "javac" file.

Curiosity bubbled and I asked the instructor of this, and he explained:

The command "set path" alters the default-set system path "C:\windows" and "C:\windows\system32".

It can be verified by My Computer > Properties > Advanced tab > Environment Variables Button.






















Hence First lesson: System path (Under Environment Variables) is like virtual current directory. You can execute exe's lying in these folders, just like you execute them as if they are in your current directory.

Lemme explain,

Suppose there is an executable "temp.exe" in directory D:\temp. When you start command prompt, you are at c:\Documents and settings\

Now try to run "temp.exe".

Failed ? Eh ?

Reason is, there is no file called "temp.exe" in your current folder.

Navigate to D:\temp\ and then run "temp.exe". That goes well.

So to run any executable just by name (as we did by just typing "javac") we need to be in that directory.(My perception before lab)

But windows provides a mechanism through which you can setup another directories virtually as your current directories and execute files in them as if they were in your current directory.

Best example could be "notepad". Running this command opens up notepad editor. The executable for notepad is located @ C:\windows\system32 folder. Hence this directory being set as path by default we can run files in this folder just by names.

When I altered the path by "Set path" command, I could no longer run "notepad". It had the error.


Hence the confusion waded away and I carried on with Java.