Classes in Java are organized by packages. However we do not have to use a package. Most of the times if we read a book or tutorial online; the files will not have a package associated with them .
File: "home.java"
class home
{
int noOfRooms = 10 ;
public static void main(String args[] )
{
System.out.println("Inside home class." ) ;
}
}
Suppose this file is in a folder called "/users/amittal/java/tij/AccessControl" . We can go through the terminal to this folder and run this file.
javac home.java
java home
To run the file from another folder we need to make sure that "/users/amittal/java/tij/AccessControl" is in the classpath. For Unix we can do:
[amittal@hills Generics]$ cd /users/amittal/java/tij/AccessControl/package_test
[amittal@hills package_test]$
[amittal@hills package_test]$ export CLASSPATH=/users/amittal/java/tij/AccessControl:.
[amittal@hills package_test]$ java home
Inside home class.
In addition to the package being default we can also have the class being "default" . There are 4 access specifiers: public, protected, default, private. A class can either have default or public access. It cannot have "private" access as that would mean that the class cannot be accessed by anyone else. It cannot have "protected" access either. The default access means the class is only accessible from the default package.
Member variables and member methods can also have default access ( no access specifier applies to them ) and that means other classes in the same package can access them. The intent is that files in the same package should be able to access each other's data.
Instead of using a file "home_test.java" to see if we have access to the "home" class.
1)
This exercise deals with the default package. Open up the Terminal window and it should take you to your home folder. You can see the folder you are at with the "pwd" command. It should show a path that ends with the username such as:
[amittal@hills ~]$ pwd
/users/amittal
In this folder create a folder called "java" and then a subfolder called "tij" ( thinking in java ) .
In this folder "tij" create a file called "home.java" . Compile and run the file:
File: "home.java"
class home
{
int noOfRooms = 10 ;
public static void main(String args[] )
{
System.out.println("Inside home class." ) ;
}
}
[amittal@hills AccessControl]$ javac home.java
[amittal@hills AccessControl]$ java home
Inside home class.
Now create a folder inside "tij" called "package_test" . In this folder create a file called:
"home_test.java"
class home_test
{
public static void main(String args[] )
{
home homeObj = new home() ;
System.out.println("Inside test4 class." + homeObj.noOfRooms ) ;
}
}
Cd into the folder "package_test" and try to compile the file "home_test.java". You will get a compiler error .
That is even though "home.java" belongs to the default package and "home_test" belongs to the default package; they are in different folders and the "home_test.java" cannot see "home.java" . We can fix this by setting the class path to include the folder that the "home" is in.
[amittal@hills package_test]$ export CLASSPATH=/users/amittal/java/tij/:.
In unix we use the colon ":" to separate the folder paths and then the dot "." means current folder. We use export to denote that the child processes have access to the "CLASSPATH" variable.
Now compile and run the "home_test.java" .
2)
This exercise illustrates the package concept. In the "java/tij" folder create a folder called "com" and inside "com" create a subfolder called "ajay" . Inside this folder create a file called "mymath.java" .
package com.ajay ;
public class mymath
{
int x1 ;
public static void function1()
{
System.out.println( "Inside mymath function1()" ) ;
}
public static void main(String args[] )
{
//home homeObj = new home() ;
System.out.println( "Inside mymath main" ) ;
}
}
go to the foider "ajay" and compile this file. Try to run the file .
[amittal@hills com]$ javac mymath.java
[amittal@hills com]$ java mymath
Error: Could not find or load main class mymath
Caused by: java.lang.NoClassDefFoundError: com/ajay/mymath (wrong name: mymath)
Since our file is in the package "com.ajay" we cannot run it like this. We can go up to where the folder "com" is and then try to run it. We have to be right above the "com" folder in our path.
[amittal@hills AccessControl]$ java com.ajay.mymath
Inside mymath main
Notice we have to give the package name "com.ajay" in order to run the file. And we are in the folder just above the com folder. Now let's try to run this class from another folder in our system.
From the command line go to the "package_test" folder. Reset the CLASSPATH variable
[amittal@hills package_test]$ export CLASSPATH=.:
[amittal@hills package_test]$
[amittal@hills package_test]$
[amittal@hills package_test]$ java com.ajay.mymath
Error: Could not find or load main class com.ajay.mymath
Caused by: java.lang.ClassNotFoundException: com.ajay.mymath
The way packages work is; Java is going to go through each path in the CLASSPATH variable and check if that path contains the folder , subfolder of "com/ajay" . Since it only has "." representing the current folder it does not find the class. We need to help the JVM by specifying the path just above the com folder on the CLASSPATH.
[amittal@hills package_test]$ export CLASSPATH=/users/amittal/java/tij/AccessControl:.
[amittal@hills package_test]$ java com.ajay.mymath
Inside mymath main