Exercise-12
a) Illustration of class path
AIM: To write a JAVA program, illustrate class path
import java.net.URL;
import java.net.URLClassLoader;
public class App
{
public static void main(String[] args)
{
ClassLoader sysClassLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader)sysClassLoader).getURLs();
for(int i=0; i< urls.length; i++)
{
System.out.println(urls[i].getFile());
}
}
}
OUT-PUT:
E:/java%20work/
b) A case study on including in class path in os environment
AIM: To write a case study on including in class path in your os environment of your
package.
The differences between path and classpath are given by.
1.The PATH is an environment variable used to locate "java" or "javac" command, to run
java SOURCE-CODE and compile java source file. The CLASSPATH is an
environment variable used to set path for java classes.
2.In order to set PATH in Java, we need to include bin directory in PATH environment
while in order to set CLASSPATH we need to include all directories where we have
put either our .class file or JAR file, which is required by our Java application.
3.PATH environment variable is used by operating system while CLASSPATH is used by
Java ClassLoaders to load class files.
4.Path refers to the system while classpath refers to the Developing Environment.
By default the java run time system uses the current working directory.
Normally to execute a java SOURCE-CODE in any directory we have to set the path by
as follows set path= c:\SOURCE-CODE Files\java\jdk1.5.0_10\bin;
Setting environmental variable in windows xp:
Step-1:
Select My computer on the desktop and right click the mouse and then select properties
It displays the following “System Properties” dialog.
Step-2:
In System Properties click Advanced and then click Environment Variables
It displays the following “Environment Variables” dialog.
Step-3:
In Environment Variables click New in System variables
It displays the following “New System Variable” dialog box.

Step-4:
Now type variable name as a path and then variable value as
c:\SOURCE-CODE Files\java\jdk1.5.0_10\bin;
Step-5:
Click OK
c) Creating and importing a package
AIM: To write a JAVA SOURCE-CODE that import and use the defined your package in
the previous
Problem
(i) Creating a package:
Steps:
1. First declare the name of the package using package keyword
Example: package mypack;
2. Type the following SOURCE-CODE under this package statement. In package : class
,data, methods all are public
package mypack;
public class box
{
public int l=10,b=20;
public void display()
{
System.out.println(l);
System.out.println(b);
}
}
3. Create sub directory with a name same that of package name under the current working
directory by as follows. d:\>md mypack
4. Under this subdirectory store the above SOURCE-CODE with a file name “box.java”.
(ii) importing a package:
Steps:
1. packages can be accessed by using the import statement
General form: import pack1[.pack2].(classname/*);
Example: import java.io.*;
Here pack1 is name of top level package and pack2 is name of sub package
2. Type the following SOURCE-CODE under the current working directory and save the
SOURCE-CODE with a file name “example.java”.
import mypack.box;
class packagedemo
{
public static void main(String args[])
{
box b1=new box();
b1.display();
}
}
3. Now compile the above SOURCE-CODE in the current working directory d:\
javac packagedemo.java
4. Execute the above SOURCE-CODE in current working directory
java packagedemo
OUT-PUT:
10
20



Comments
Post a Comment