How does the Java run-time system know where to look for packages that you create?
The answer has two parts:
First, by default, the Java run-time system uses the current working directory as its starting point. Thus, if your package is in the current directory, or a subdirectory of the current directory, it will be found.
Second, you can specify a directory path or paths by setting the CLASSPATH environmental variable. For example, consider the following package specification. package MyPack;
In order for a program to find MyPack, one of two things must be true. Either the program is executed from a directory immediately above MyPack, or CLASSPATH must be set to include the path to MyPack.
The answer has two parts:
First, by default, the Java run-time system uses the current working directory as its starting point. Thus, if your package is in the current directory, or a subdirectory of the current directory, it will be found.
Second, you can specify a directory path or paths by setting the CLASSPATH environmental variable. For example, consider the following package specification. package MyPack;
In order for a program to find MyPack, one of two things must be true. Either the program is executed from a directory immediately above MyPack, or CLASSPATH must be set to include the path to MyPack.
// A simple package
package MyPack;
class Balance
{
String name;
double bal;
Balance(String n, double b)
{
name = n; bal = b;
}
void show()
{
if(bal<0) System.out.print("--> ");
System.out.println(name + ": $" + bal);
}
}
//AccountBalance.java
class AccountBalance
{
public static void main(String args[])
{
Balance current[] = new Balance[3];
current[0] = new Balance("K. J. Fielding", 123.23);
current[1] = new Balance("Will Tell", 157.02);
current[2] = new Balance("Tom Jackson", -12.33);
for(int i=0; i<3; i++)
current[i].show();
}
}
//To compile javac AccountBalance.java
//To run java MyPack.AccountBalance
//java AccountBalance invalid