Sunday, February 04, 2007

Makefile to compile java programs

The following is the makefile to compile java programs:

all: | remove_classes ProxyServer.class

rm -f *.class

javac -nowarn

The order of execution of jobs within the makefile can be normal or we can specify the order. The jobs listed after the pipe (|) are all orderly, as mentioned above.

all: | first second third

Makefile can be executed as:
> make

If target-name is omitted, then the first job is run. So in the first job, we name it as "all" and then there we specified the jobs it should run, along with the order. It is simple to understand that the current make file removes all old class files and then compiles the java program to get the latest class files. Though this is not needed, as javac would update the class files, this simple example helps us to understand "make".

Have fun!

Setting path variables in Linux permanently

Recently I have installed Linux Ubuntu on my laptop on top of Virtual PC. There is a very good post on how to do this at this link:

Then I had installed Eclipse and JAVA 1.5 on the new linux workstation! I wanted to work out a few programs on the linux platform.

When you start the terminal in Ubuntu and type "java -version", it displays the version but as you notice down it is not the Sun HotSpot VM that is running but is the GCJ, a free java compiler which doesnt actually interpret. So you would like to change the system to use the Java VM you installed. For that the obvious way is to change the Path variables for JAVA_HOME and PATH.

You can set a path variable using "export" command as :

export JAVA_HOME=/opt/jdk1.5.0_11
export PATH=$JAVA_HOME/bin:$PATH

When we set PATH variable, we should not forget to append already existing path variables( using :$PATH).

Now when you type "java -version", you can see your Java VM as HotSpot one. But then close the terminal and open it again.

Again try the "java -version", you will find your settings to be gone! What happens is that your settings were temporary and effective only for the terminal window that was launched and path set. In order for the variables to be effective for the user throughout the sessions, you should edit the bashrc script.

You can set environment variables permanently for a given user by changing the bashrc:

>pico ~/.bashrc

This opens the bashrc for you. Paste the two lines of "export" i mentioned earlier. Make sure you give the path of your JDK properly.
Save the changes and exit.

Now type

>source ~/.bashrc

This updates the changes made the new variables have been set. To test your settings you can now type:
"which java" and it should display the path of the java compiler that is being use( the one you set in the bashrc).

I am trying to find out how to set variables to reflect on all the user accounts !!! Let me know in case you guys know how to!