Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 7 of 7

Thread: cannot find symbol (in a jar file)

  1. #1
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default cannot find symbol (in a jar file)

    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.
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,049
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: cannot find symbol (in a jar file)

    A Pack.jar file is then created with:

    jar -cf Pack.jar Pack.class
    Since the Pack class is in a package: package bfs.javaPackage.jarPackage.george.washington;
    the class "file" needs to be on that path in the jar file.
    Look in the jar file using a zip utility to see if the Pack class is on that path.
    I suspect that the class is at the root level.
    Change the args to the jar command so that it finds the Pack class at the end of the correct path and uses that path when it creates the jar file.

    Note: the jar file can be in the same folder with the class that is being compiled. It does not need to be on any path.

    javac -cp .;Pack.jar Call.java
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol (in a jar file)

    Norm,

    First, thank you very much for your help on this issue!

    I think I understood what you suggested. I changed the creation
    of my jar file so that it is now created in the parent directory
    of the 'bfs' directory.

    Now when I run:

    jja@sutton: unzip Pack.jar

    The results show:

    Archive: Pack.jar
    creating: META-INF/
    inflating: META-INF/MANIFEST.MF
    inflating: bfs/javaPackage/jarPackage/george/washington/Pack.class

    whereas, previously the results were:

    Archive: Pack.jar
    creating: META-INF/
    inflating: META-INF/MANIFEST.MF
    inflating: Pack.class

    If I place, Pack.jar in the same directory as the Call.java class and
    run:

    javac -cp .ack.jar Call.java

    I am still getting an error:

    Call.java:15: error: cannot find symbol
    pack.message();
    ^
    symbol: method message()
    location: variable pack of type Pack


    I am still missing someting from your instructions or I misunderstood
    your feedback. :-(

    Jim

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,049
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: cannot find symbol (in a jar file)

    If the compiler is finding the Pack class in the jar file
    but it does not find the message() method in the Pack class, I suggest you check that the class has a public message() method.

    Why is the Pack class's constructor private?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol (in a jar file)

    I have double checked the Pack class and it does the message method is declared public:

    >>> public String message() {
    >>> return("Running message() method of Pack class.");
    >>> }

    The Pack class constructor should be public and I have fixed that. Thank you.

    I'm going to put this thread on hold. While it is on hold, I am going to do some experimenting with some simpler cases and see if I can work my way back to this case. My experimenting could be a few days, maybe even a week, but I shall return to this case.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,049
    Thanks
    63
    Thanked 2,710 Times in 2,660 Posts

    Default Re: cannot find symbol (in a jar file)

    do some experimenting with some simpler cases
    That was going to be my suggestion:
    Start with both classes in the same folder and neither class in a package.
    If that works, put the Pack class in a package and move it to a folder on a path representing the package,
    then use the path to the root of the package path in -cp option to the javac and java commands.
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Dec 2017
    Location
    Saylorsburg, PA, USA
    Posts
    8
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: cannot find symbol (in a jar file)

    This can be closed. As I mentioned earlier, I did some experimenting, starting with a simple case with no hierarchy, then added the hierarchy that I need and got it working.

    To Norm, my thanks for the help.

Similar Threads

  1. [SOLVED] cannot find symbol symbol: constructor ()
    By cnmeysam in forum Java SE APIs
    Replies: 1
    Last Post: March 24th, 2022, 06:56 AM
  2. cannot find symbol
    By lungelo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 15th, 2013, 05:06 AM
  3. [SOLVED] cannot find symbol
    By ATIBA SHEIKH in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 26th, 2012, 03:28 AM
  4. Cannot find symbol
    By waarten in forum What's Wrong With My Code?
    Replies: 18
    Last Post: January 11th, 2012, 03:15 PM
  5. Cannot find Symbol?
    By defmetalhead in forum What's Wrong With My Code?
    Replies: 8
    Last Post: July 5th, 2011, 08:48 AM