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
-
CONSTANTS,VARIABLES AND KEYWORDS: CONSTANTS: Two types of constants those are: a)primary constants b)secondary constants a)In primary consta...
-
Input/Output with files C++ provides the following classes to perform output and input of characters to/from files: * ofstream: Stream c...
-
<%@page contentType="text/html" import="java.util.*" %> <!-- http://www.roseindia.net/jsp --> ...
-
# When the user submits form, his information is sent to the corresponding servlet file because we've set the ACTION attribute to point ...
-
Introduction: In this exercise, you will learn how to nvoke both static and non-static (instance) methods of a class. Please note that a st...
-
Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any a...
-
2 Managed Code and the CLR Remember that managed code is code written for (and with) the .NET Framework. Managed code is called manage...
-
The Form's KeyPreview Property To understand this property, lets look on the following example: Start new project, and add 1 Command But...
-
2.1 Intermediate Language, Metadata and JIT Compilation Managed code is not interpreted by the CLR. I mentioned that earlier. But ...
-
JSP scripting elements let you insert Java code into the servlet that will be generated from the current JSP page. There are three forms: ...
No comments:
Post a Comment