Reality is a perception. Perceptions are not always based on facts, and are strongly influenced by illusions. Inquisitiveness is hence indispensable

Wednesday, May 23, 2007

Paving Path to ClassPath and Packaging

Last fortnight vineela posed a question. Sounded silly in the first go but later found it to be not so silly. She was trying to compile java files from command prompt. Guess what happened. A disaster! Now she is not a run of the mill kind. Only issue was that she was born and brought up in the world of user-friendly IDEs. She never worked with the age old tools "javac" and java. Why should she? When was the last time I used javac? Let me guess, it is nearly four years.


There were three problems she was facing, packing the sources, compiling and running. The next step of packing the sources into directories sounds easy. What about classes? As per the practice, they also need to mimic the file structure. We need to duplicate the directory structure. What about running the app? Run it from the top-level package. Wait... Wait... why are we hurrying? Take a break and breath-in... & out... repeat it 3 times (use for loop!!)


Some references before we start.

http://java.sun.com/docs/books/tutorial/java/package/QandE/packages-answers.html http://java.sun.com/j2se/1.5.0/docs/tooldocs/solaris/javac.html http://www.cs.wisc.edu/~hasti/cs368/JavaTutorial/NOTES/Packages.html



Simple Example:


Assuming we know how packages are formed we shall go ahead.

Have a directory called C:\Workarea. Now create three folders called "src", "classes". Under "src" create your package structure.

My source files are: myblog.master.Lord.java, myblog.servant.Gini.java.


There is a correlation between and packages and directory structure.

Under C:\Workarea\src create 3 directories

1.) myblog, 2.) mblog\master, 3.)myblog\servant.


Create the same structure under C:\Workarea\classes. We need to copy java sources to src directory location.

Now copy Lord.java to master and Gini.java to the servant directories. Now come to top directory i.e. C:\Workarea.


Under windows run these commands "dir /s /b *.java > a.txt" and then "javac -sourcepath src -classpath . -d classes @a.txt"

javac essentially takes parameters describing the source location, classpath info and destination where classes are to be saved. @a.txt is the file that contains the locations of .java source files


Bingo...


To run the app from C:\Workarea call "java mainclass"

No comments:

Popular Posts

Labels

About Me

Well for a start, I dont' want to!. Yes I am reclusive, no I am not secretive; Candid? Yes; Aspergers? No :). My friends call me an enthusiast, my boss calls me purist, I call myself an explorer, to summarise; just an inquisitive child who didnt'learn to take things for granted. For the sake of living, I work as a S/W engineer. If you dont' know what it means, turn back right now.