Popular Posts

Monday, February 23, 2009

Exercise 10.3: Classpath again at the command line

Please do this exercise at the command line instead of using NetBeans. This is to learn the concept of classpath without the help of NetBeans.

Introduction:

I hope you have a reasonably good understanding on classpath. Since classpath is a very important concept, we are revisiting it here. The goal of this exercise is that even the javac compiler depends on the classpath.

Steps to follow:

1. If you have used NetBeans to do the exercise 10.2 above, please create StudentRecord.java and StudenRecordExample.java as shown in Code-10.1.a and Code-10.1.b below using your editor of choice at the command line. Please note that StudentRecordExample class uses StudentRecord class.

* set CLASSPATH= (in order not to depend on the classpath environment variable just for this exercise)
* cd \myjavaprograms
* jedit StudentRecord.java (using code Code-10.1.a above)
* jedit StudentRecordExample.java (using code Code-10.1.b above)

2. Compile code using the following method. The compilation should succeed.

* javac StudentRecord.java StudentRecordExample.java

3. Compile code individually as following.

* javac StudentRecord.java
* javac StudentRecordExample.java

4. The compilation of the StudentRecord.java should succeed but the compilation of StudentRecordExample.java should fail as following:

* C:\myjavaprograms>javac StudentRecord.java

C:\myjavaprograms>javac StudentRecordExample.java
StudentRecordExample.java:6: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord annaRecord =new StudentRecord();
^
StudentRecordExample.java:6: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord annaRecord =new StudentRecord();
^
StudentRecordExample.java:9: cannot find symbol
symbol : variable StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord.increaseStudentCount();
^
StudentRecordExample.java:12: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord beahRecord =new StudentRecord();
^
StudentRecordExample.java:12: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord beahRecord =new StudentRecord();
^
StudentRecordExample.java:15: cannot find symbol
symbol : variable StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord.increaseStudentCount();
^
StudentRecordExample.java:18: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord crisRecord =new StudentRecord();
^
StudentRecordExample.java:18: cannot find symbol
symbol : class StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord crisRecord =new StudentRecord();
^
StudentRecordExample.java:21: cannot find symbol
symbol : variable StudentRecord
location: class studentpackage.StudentRecordExample
StudentRecord.increaseStudentCount();
^
StudentRecordExample.java:32: cannot find symbol
symbol : variable StudentRecord
location: class studentpackage.StudentRecordExample
System.out.println("Count="+StudentRecord.getStudentCount());
^
10 errors

The compile error occurs because StudentRecordExample class uses StudentRecord class and the javac compiler could not find the StudentRecord class in the default classpath. Since you did not specify the "-classpath " option when you compile the code, the javac compiler takes either CLASSPATH environment variable if it has been or no classpath. This is in a sense the same problem as you might have seen in Exercise 3.1.

5. Specify the classpath for compilation as well. Now the compilation of both files should succeed.

* javac StudentRecord.java
* javac -classpath . StudentRecordExample.java

No comments:

Post a Comment