Please do this exercise at the command line instead of using NetBeans. This is to learn the concept of packaging without the help of NetBeans.
Introduction:
You already have some exposure to the concept of a package. A Java package is basically a collection of Java classes. One important thing you have to remember is that a Java package structure should be reflected in the directoryt structure in the file system. In this exercise, you will do a bit more comprehensive exercise involving classpath and packaging structure. Please feel free to do some more experimentation of your own.
* If you are new to Packaging concept, please read "Creating and Managing Packages" section of the Java Progamming Tutorial.
Steps to follow
1. Modify StudentRecord.java as following to add a package statement as shown in Code-10.4.a below. By adding package statement, you are basically saying the StudentRecord class now belongs to studentpackage package. The code fragement that needs to be added is highlighted with bold and blue-colored font.
package studentpackage;
public class StudentRecord {
...
}
Code-10.4.a: StudentRecord.java with package statement
2. Modify StudentRecordExample.java as shown in Code-10.4.b below to add a package statement. By adding package statement, you are basically saying the StudentRecordExample class now belongs to studentpackage package. The code fragement that needs to be added is highlighted with bold and blue-colored font.
package studentpackage;
public class StudentRecordExample{
...
}
Code-10.4.b: StudentRecordExample.java with package statement
3. Compile code.
* cd \myjavaprograms (if you are not in this directory already)
* del StudentRecord.class (just to start from a clean slate)
* del StudentRecordExample.class (just to start from a clean slate)
* javac StudentRecord.java StudentRecordExample.java
4. Run the code. You will experience an NoClassDefFoundError exception as shown below.
* java -classpath . StudentRecordExample
Think about why you are getting this exception for a moment. It is because the java runtime is trying to find StudentRecordExample.class under studentpackage directory since the StudentRecordExample.java now has a package studentpackage; statement, which indicates that the Java class file resides under studentpackage directory.
* C:\myjavaprograms>java -classpath . StudentRecordExample
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecordExample
(wrong name: studentpackage/StudentRecordExample)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
5. Create a new directory called strudentpackage and then move StudentRecord.java and StudentRecordExample.java under it.
* mkdir \myjavaprograms\studentpackage
* move \myjavaprograms\StudentRecordExample.java \myjavaprograms\studentpackage\StudentRecordExample.java
* move \myjavaprograms\StudentRecord.java \myjavaprograms\studentpackage\StudentRecord.java
6. Compile the code. You will experience compile errors as following. You get this compile error because you are trying to compile the two Java files that are not present in the current directory anymore.
* cd \myjavaprograms (if you are not in this directory already)
* del StudentRecord.class
* del StudentRecordExample.class
* C:\myjavaprograms>javac StudentRecord.java StudentRecordExample.java
error: cannot read: StudentRecord.java
1 error
7. Compile the code using a directory structure. The compilation should succeed. Note that the class files are now created under studentpackage directory not in the current directory
* javac studentpackage\StudentRecord.java studentpackage\StudentRecordExample.java
* C:\myjavaprograms>dir studentpackage
Volume in drive C is S3A1256D004
Volume Serial Number is 447E-6EBC
Directory of C:\myjavaprograms\studentpackage
07/06/2005 12:39 PM DIR .
07/06/2005 12:39 PM DIR ..
07/06/2005 12:40 PM 1,499 StudentRecord.class
07/06/2005 12:16 PM 1,425 StudentRecord.java
07/06/2005 12:40 PM 880 StudentRecordExample.class
07/06/2005 12:17 PM 690 StudentRecordExample.java
4 File(s) 4,494 bytes
2 Dir(s) 1,415,856,128 bytes free
8. Run the code as following. You will experience NoClassDefFoundError because Java runtime now is trying to find the class in the current directory instead of in the studentpackage directory.
* C:\myjavaprograms>java -classpath . StudentRecordExample
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecordExample
9. Run the code with propert package structure. It should work this time.
* C:\myjavaprograms>java -classpath . studentpackage.StudentRecordExample
Anna
Count=3
10. Now you thught you should be able to run the application under the studentpackage directory itself so you go into the directory and run the code. And the following is what you will experience. It is because it is still looking for studentpackage/StudentRecordExample.class in the currently directory and it could not find it.
* C:\myjavaprograms>cd studentpackage
C:\myjavaprograms\studentpackage>java -classpath . StudentRecordExample
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecordExample
(wrong name: studentpackage/StudentRecordExample)
at java.lang.ClassLoader.defineClass0(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:539)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:123)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:251)
at java.net.URLClassLoader.access$100(URLClassLoader.java:55)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:187)
at java.lang.ClassLoader.loadClass(ClassLoader.java:289)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:274)
at java.lang.ClassLoader.loadClass(ClassLoader.java:235)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:302)
11. Now can specify the classpath using -classpath command line option as following. The reason you are experiencing the problem is because you do not provide the proper directory structure when invoking the application even though you gave the correct classpath.
* C:\myjavaprograms\studentpackage>java -classpath \myjavaprograms StudentRecordExample
Exception in thread "main" java.lang.NoClassDefFoundError: StudentRecordExample
12. Now use correct directory structure of the class.
* C:\myjavaprograms\studentpackage>java -classpath \myjavaprograms studentpackage.StudentRecordExample
Anna
Count=3
Homework:
1. Write a class called Food under foodpackage.fruitpackage pacakge
* Food.java should have the following package statement at the top
o package foodpackage.fruitpackage
* Add a couple of methods of your own in the Food.java.
2. Write a class called FoodMain under foodpackage.fruitpackage package
* FoodMain class creates an Food object
* FoodMain class then calls a method of Food object
3. Compile and run the code
Hint: The goal of this homework exercise is to let you experience s two-level package structure instead of just one. For example, the "foodpackage" is a parent package of the "fruitpackage" package. And each should have its own directory and they should reflect the parent and child relationship of the package structure. What this means is that you would have to create foodpackage directory first and then fruitpackage directory underneath it. Then you will create Food.java and FoodMain.java under fruitpackage directory.
Popular Posts
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Using JavaServer Pages...
-
Please do this exercise at the command line instead of using NetBeans. This is to learn the concept of classpath without the help of NetBean...
-
1 Introducing the .NET Framework with C# The .NET Framework is such a comprehensive platform that it can be a little difficult to descr...
-
Working With Integers The process of creating variable called "Declaring" To Declare (=create) Integer variable simply write: Dim ...
-
INTRODUCTION TO 'C': C is a programming language developed at AT & T's Bell laboratories of USA in 1972.it was designed by d...
-
STRINGS: The way a group of integers can be stored in an integer array, similarly a group of characters can be stored in a character array. ...
-
More About Ascii How can I know what is the Ascii value of a specific character? Use the Asc command. For example, the following line: Print...
-
Using functions we can structure our programs in a more modular way, accessing all the potential that structured programming can offer to us...
-
4.5 Using FCL Documentation for Types Using the SDK documentation for a given type will likely be a daily or even hourly event when ...
-
Files in VB .NET Working with Directories We will work with the File and Directory classes in this section. We will create a directory and c...
No comments:
Post a Comment