It has been a while since I have programmed using java. I'm
in the process relearning java and I have run into a problem.
I am trying to create a java program that calls a method in
a package method stored in a library jar file.
BTW, my version of java is:
java version "1.8.0_301"
Java(TM) SE Runtime Environment (build 1.8.0_301-b09)
Java HotSpot(TM) 64-Bit Server VM (build 25.301-b09, mixed mode)
My sample jar file is "Pack.jar", using a source file named, "Pack.java",
and shown below.
I have also created a java file named, "Call.java" whose purpose
is to access the the methods within the Pack.jar file. This file is
shown below also.
The directory structure is:
...
|
bfs
____________________________
| |
javaPackage lib
|
_____________________________
| |
| |
jarPackage jarCaller
| |
george Call.java
|
washington
|
Pack.java
The Pack.java file has a main() method that allows me to test if
the Pack class is working as I expect. I have a test c-shell script
named, "run". When I run a test on Pack.class, it yields the
following output:
% run
1: Start Pack class.
2: Created Pack instance.
3: Running message() method of Pack class.
4: End Pack class.
No problem here, the script successfully pushes to the directory
containing, "bfs", and runs java using the full path from that
directory to the directory where the Pack class resides. I run
the following command to get the out when the "run" script is
executed.
java bfs/javaPackage/jarPackage/george/washington/Pack
The Pack main() method successfully calls the message() method of the
Pack class.
A Pack.jar file is then created with:
jar -cf Pack.jar Pack.class
The jar file is copied to a local library directory with:
cp Pack.jar .../bfs/lib/Pack.jar
Next I go to the jarCaller directory and compile the Call.java class.
I compile with the following:
javac -classpath .:../bfs/lib/Pack.jar ./Call.java
In the first pass of Call.java, the main() method simply creates an
instance of the Pack class using the Pack class constructor. When run,
it successfully creates an instance of the Pack class and the Call
class prints the message:
Pack instance created
In the second pass, I add the following line to the Call main() method:
pack.message();
When I try to compile the Call.class, it now gives the error message:
./Call.java:29: error: cannot find symbol
pack.message();
NOTE: 'pack' is the variable created with:
Pack pack = new Pack();
I am baffled by this error message.
The possible causes for the error message that I can think of are:
1) The Pack class does not have a message() method.
- This is not the case, since I run a test in the Pack class
that runs the message() method.
2) The java compiler is not finding the Pack.jar class.
- This is not the case. I know the Pack.jar class is found
because if I comment out the pack.message() line, the
Call class succesfully calls the Pack class constructor
and I can then run the Call main() method.
3) The message() method is not public.
- The Pack class message method is public.
// *************************************************
// Source codce: Pack.java
// ************************
package bfs.javaPackage.jarPackage.george.washington;
public class Pack {
private Pack() {
System.out.println("\t2: Created Pack instance.");
}
public String message() {
return("Running message() method of Pack class.");
}
public static void main(String args[]) {
System.out.println("\t1: Start Pack class.");
Pack pack = new Pack();
System.out.println("\t3: " + pack.message());
System.out.println("\t4: End Pack class.");
}
}
// *************************************************
// Source code: Call.java
// ************************
import bfs.javaPackage.jarPackage.george.washington.Pack;
public class Call {
public static void main(String args[]) {
System.out.println("This is the Call class.");
Pack pack = new Pack();
if ( pack == null ) {
System.err.println("pack is null.");
System.exit(-1);
}
else {
System.out.println("Pack instance created.");
}
pack.message(); // when this line is commented out. The file
// can be compiled and run w/o error.
}
}