Page 1 of 2 12 LastLast
Results 1 to 20 of 26
  1. #1
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Can't compile a class that invokes a method in a class in a different package

    Hello,
    I must be missing something fundamental.
    I have a class, Test7, in a package, test:

    Java Code:
    package test;
    
    public class Test7
    {   
        public static void foo(int x)
        {
            System.out.println("test7 foo int:" + x);
        }
    }
    This compiles fine.

    Now, I want to call this, from the class Test8, which is NOT in a package.

    Java Code:
    public class Test8
    {
        public static void main(String[] args)
        {
            System.out.println("Test8 main");
            test.Test7.foo(10);        
        
        }
    }

    I'm not able to compile Test8 :(
    Test8 is in the parent directory of the "test" directory (well, it's not in any package so I figured as long as it's not in the "test" directory it should work).
    I have tried compiling Test8.java both with and without "-cp test" (which I am presuming specifies the classpath for .class files) but neither works.
    What am I doing wrong? Surely I should be able to compile a .java file that invokes a method in an "already compiled" .class file?
    The directory listings and compilation attempts are below. Thanks in advance.


    Java Code:
    C:\Users\J\My Documents\java>dir test\Test7*
     Volume in drive C has no label.
     Volume Serial Number is 3E5E-B363
    
     Directory of C:\Users\J\My Documents\java\test
    
    12/08/2013  22:16               607 Test7.class
    12/08/2013  22:15               144 Test7.java
                   2 File(s)            751 bytes
                   0 Dir(s)  36,002,934,784 bytes free
    
    C:\Users\J\My Documents\java>javac Test8.java
    Test8.java:6: error: cannot find symbol
            test.Test7.foo(10);
                ^
      symbol:   variable Test7
      location: class test
    1 error
    
    C:\Users\J\My Documents\java>javac -cp test Test8.java
    Test8.java:6: error: cannot find symbol
            test.Test7.foo(10);
                ^
      symbol:   variable Test7
      location: class test
    1 error
    
    C:\Users\J\My Documents\java>

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,762
    Rep Power
    7

    Default Re: Can't compile a class that invokes a method in a class in a different package

    have you tried importing the class?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Can't compile a class that invokes a method in a class in a different package

    javac Test8.java

    For this one, I'm going to guess you have a CLASSPATH environment variable declared somewhere, Quicktime used to be the prime culprit for this, so the compiler is using that as the default classpath. This is one reason to get in the habit of using the -cp switch.

    javac -cp test Test8.java
    For this one, the parameters for the -cp swicth are the locations of the root of your packages. 'test' is a package, so you want to point at the directory above that.
    Since you are executing from that directory you can simply use '.', which means "current directory".
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    @Tolls

    Thanks, but I've tried already using -cp (as shown in the code listing in my question) but it didn't work.
    I also tried "-cp . " after I had posted the question but that doesn't work either:
    Java Code:
    C:\Users\J\My Documents\java>javac -cp . Test8.java
    Test8.java:6: error: cannot find symbol
            test.Test7.foo(10);
                ^
      symbol:   variable Test7
      location: class test
    1 error
    
    C:\Users\J\My Documents\java>dir test\Test7*
     Volume in drive C has no label.
     Volume Serial Number is 3E5E-B363
    
     Directory of C:\Users\J\My Documents\java\test
    
    12/08/2013  22:16               607 Test7.class
    12/08/2013  22:15               144 Test7.java
                   2 File(s)            751 bytes
                   0 Dir(s)  35,850,567,680 bytes free
    
    C:\Users\J\My Documents\java>
    I'm not sure if it makes any difference but I have CLASSPATH defined already:

    Java Code:
    C:\Users\J\My Documents\java>echo %CLASSPATH%
    .;C:\oracleee\product\11.2.0\dbhome_1\jdbc\lib

  5. #5
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Quote Originally Posted by Junky View Post
    have you tried importing the class?
    Eh Junky, that cracked it. Well, at least, I imported the package, rather than the class. Thanks very much Sir. But why does this work -

    Java Code:
    import test.*;
    
    public class Test8
    {
        public static void main(String[] args)
        {
            System.out.println("Test8 main");
            //test.Test7.foo(10);        
            Test7.foo(10);        
        }
    }
    C:\Users\J\My Documents\java>javac -cp . Test8.java


    and this NOT work?

    Java Code:
    public class Test8
    {
        public static void main(String[] args)
        {
            System.out.println("Test8 main");
            test.Test7.foo(10);        
        }
    }
    C:\Users\J\My Documents\java>javac -cp . Test8.java
    Test8.java:8: error: cannot find symbol
    test.Test7.foo(10);
    ^
    symbol: variable Test7
    location: class test
    1 error



    I thought the import statement was just a convenience for the programmer's benefit.
    What's the difference between
    Java Code:
    import test.*
    ...
    Test7.foo()
    and

    Java Code:
    test.Test7.foo()
    ?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,861
    Rep Power
    19

    Default Re: Can't compile a class that invokes a method in a class in a different package

    It could be down to Test8 not being in a package.
    It is generally recommended that you put classes in a proper package and not use the default.
    Put Test8 in another package (say test2) and try again, but be sure to have the correct path in your -cp.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Actually you're absolutely right, both should work. So I don't agree that cracked it. Something else was wrong, probably classpath related. Did you by any chance not compile the Test7 class first?

    And you're not importing a package, you can't do that. You're importing all classes inside the package. Don't worry about that too much, once you start to use an IDE you can use its "fix imports" function to generate the proper import statements for you.

    edit: ninja'd!
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,184
    Rep Power
    19

    Default Re: Can't compile a class that invokes a method in a class in a different package

    There's no difference. But it's good to be aware of the difference between importing a single Type and import-on-demand.
    Chapter*7.*Packages

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  9. #9
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Yes I just noticed something.
    I had thought that the process goes like this:
    1) compile Test7 (into a class file of course)
    2) compile Test8, which uses Test7.*CLASS*

    Well, I don't think that is what is going on, because I made a change to Test7 (removing the "public" access modifier to verify that Test8 wouldn't be able to call the public method foo in a package-private Test7) and saved it but did not compile it.
    I then tried to compile Test8.java and it complained that "Test7 is not public in test".

    Therefore what I'm doing is compiling both at the same time. What I expected (and now, what I want to do) is to be able to compile a class (Test7.class) and independently use Test7.class file in another .java file.
    So yes, I don't think -cp has anything to do with it, because surely -cp is specifying where to find class files.
    Last edited by mikomi; 08-13-2013 at 11:16 AM.

  10. #10
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    And I just recompiled Test8.java without specifying "-cp ." and it worked:

    C:\Users\J\My Documents\java>javac Test8.java

  11. #11
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    And this does NOT work:

    Java Code:
    import test.Test7;
    
    public class Test8
    {
        public static void main(String[] args)
        {
            System.out.println("Test8 main");  
            foo(10);        
        }
    }

    Edit: I understand now why this fails; the import statement means the class specified can be referred to without qualification, not that stuff within it can be referred to without the class name.
    So changing foo to Test7.foo fixes the error.

    C:\Users\J\My Documents\java>javac Test8.java
    Test8.java:9: error: cannot find symbol
    foo(10);
    ^
    symbol: method foo(int)
    location: class Test8
    1 error
    Last edited by mikomi; 08-13-2013 at 11:27 AM. Reason: light scattering of enlightenment

  12. #12
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    No, that's because it is incorrect.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Quote Originally Posted by gimbal2 View Post
    No, that's because it is incorrect.
    If you're referring to my post of 10:21, that's a side-issue and yes, a couple of minutes' googling fixed that.
    The main question is why importing the package works but referring to the class by its fully qualified name does not.
    Also a second question is how do I compile a .java file with a .class file rather than compiling 2 .java files at the same time?

  14. #14
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Well thumbs up that you Googled it. I was hoping you would. Beware of timezones by the way, for you it may seem that you posted it at 10:21 but for me you posted it at 11:21. Posts have a number you can see in the top-right, refer to that. And slap on my wrist for not quoting the post I was referring to.

    As to your question, that is the underlying theme of this whole thread; repeating that question is not going to net you different answers. Did you try tolls' suggestion and put your Test8 class in a package to see if that changes anything? Using the 'default package' (IE. no package) is very much not recommended.

    Just to be clear:

    - in a normal Java application you NEVER use the default package
    - in a normal Java application you use import statements, you hardly ever refer to classes using their fully qualified name
    - generally you don't compile on the command prompt because that is incredibly cumbersome as you now find out, you use specialized build tools (ANT, Maven) to automate the process

    But of course you have to learn, so we have to bite through this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    I just can't rob people of the experience to read the incredibly ugly continuation and conclusion of this thread:

    https://forums.oracle.com/thread/2569160

    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  16. #16
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Ah my good friend and stalker whoops sorry follower gimbal, glad to see you're still following me across the Interweb

  17. #17
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    You'd better mosey on to a dark corner of the internet, I don't think anyone will be willing to help you now after you've shown your true colors.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  18. #18
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Yes that's what you get for sticking up for yourself isn't it? If true colours means standing by what you believe, in the face of condescending and patronizing and ultimately incorrect responses, then yes, I'm guilty.
    I stand by my convictions, and my convictions this time were entirely correct. I tried to be polite. The responses I received, not here but at the other place, were unpleasant. But worse than that, as I say, in the end, they were wrong.
    You are guilty of your own transgression. I'll leave it at that.

  19. #19
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,766
    Rep Power
    5

    Default Re: Can't compile a class that invokes a method in a class in a different package

    No they weren't, you just refuse to believe that you can be wrong. At least in this forum you have a proper username which I can ignore, so don't worry - you won't hear any more from me.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  20. #20
    mikomi is offline Member
    Join Date
    Jul 2013
    Posts
    28
    Rep Power
    0

    Default Re: Can't compile a class that invokes a method in a class in a different package

    Quote Originally Posted by gimbal2 View Post
    No they weren't, you just refuse to believe that you can be wrong. At least in this forum you have a proper username which I can ignore, so don't worry - you won't hear any more from me.
    Except, in the end, I was not wrong. Do try to keep up.

Page 1 of 2 12 LastLast

Similar Threads

  1. Replies: 5
    Last Post: 06-03-2012, 07:20 PM
  2. Replies: 1
    Last Post: 10-17-2011, 01:00 AM
  3. calling method of different project package class
    By oneofthelions in forum New To Java
    Replies: 3
    Last Post: 02-16-2010, 05:36 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •