Results 1 to 13 of 13
  1. #1
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default Java packages and confusion (Yes, I've RTFM)

    Question regarding packages:

    I have read the tutorial at Creating and Using Packages (The Java™ Tutorials > Learning the Java Language > Packages) and am still a bit confused/receiving errors while trying to compile.

    I have two packages, one nested inside of the other. AKA:
    /core/
    /core/lib/

    I have given each java file in both packages the correct package names and tried to import those where needed. AKA:
    package core;
    package core.lib;

    import core.*;
    import core.lib.*;

    Yet when I try to compile, I'm being told by the classes in the core package that the symbols cannot be found (the files in the core.lib package cannot be found). If I change the core.lib to just lib, it works fine. Likewise, the core.lib files cannot find the core files when I try to import those.

    The questions is, is this why setting the classpath variable is necessary?

    I have attempted setting it via the command "set CLASSPATH=H:\core" to no avail. When I look for my CLASSPATH variables in the Environmental Variables menu, it doesn't exist and won't allow me to create one. Suggestions? Ideas?

    Thanks in advance.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    If your two packages are located at:

    /some/top/level/directory/core
    /some/top/level/directory/core/lib

    your classpath should point at /some/top/level/directory

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    You are most kind sir... the root directory is simply H:/ and I thought I had tried that, but apparently it barks if you use \ instead of /. Thanks.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unaligned View Post
    You are most kind sir... the root directory is simply H:/ and I thought I had tried that, but apparently it barks if you use \ instead of /. Thanks.
    It doesn't matter much which one you use in Java for MS Windows; / or \. In source code the \ is an escape character, used in \n (new line) etc. If you want to use a literal \ you have to escape it with the backslash again: \\ Otherwise / and \ are treated equal.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    Alright... here's a new one. Now that it all compiles, when I try to run the class with the main, it tells me that it can't find the main class. I see this from time to time but I've never quite understood why it shows up. Could you shed some light into it for me when the only thing that's in the class I'm running is the main?

  6. #6
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    If you want the canonical OS-independent method to find the file separator, use System.getProperty("file.separator"), which will return you the correct string to use according to the OS. See System.getProperties() for details.

  7. #7
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by unaligned View Post
    Could you shed some light into it for me when the only thing that's in the class I'm running is the main?
    Post the relevant class and the command you use to run it.

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unaligned View Post
    Alright... here's a new one. Now that it all compiles, when I try to run the class with the main, it tells me that it can't find the main class. I see this from time to time but I've never quite understood why it shows up. Could you shed some light into it for me when the only thing that's in the class I'm running is the main?
    If you have a class, say, YourClass, in package core its full name is core.YourClass and you should run it as such.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  9. #9
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    Java Code:
    
    package core;
    
    import core.*;
    
    public class Runner {
    
    	public static void main(String[] args){
    		Runnable runnable = new AutomatedProcessingUI();
    		Thread thread = new Thread(runnable);
    		thread.start();
    	}
    
    }
    javac Runner.java
    java Runner

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,335
    Rep Power
    25

    Default

    The classpath should point to the folder that holds the core folder.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,457
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by unaligned View Post
    [code]
    javac Runner.java
    java Runner
    See my previous reply: it should be "java core.Runner" (without the quotes).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    unaligned is offline Member
    Join Date
    Jun 2011
    Posts
    23
    Rep Power
    0

    Default

    Thanks again. You've been helpful.

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,335
    Rep Power
    25

    Default

    You might be in the wrong directory. The classpath would have to go up one to be at the start of the package path:
    java -classpath ..\. core.Runner

Similar Threads

  1. Java Generics assistance and confusion
    By sideswipe091976 in forum Advanced Java
    Replies: 9
    Last Post: 03-30-2011, 08:24 AM
  2. Java i/o confusion
    By willemien in forum New To Java
    Replies: 1
    Last Post: 02-11-2011, 01:35 PM
  3. java API (SE) version confusion
    By Arnold in forum New To Java
    Replies: 3
    Last Post: 04-30-2010, 10:42 AM
  4. confusion with Design Pttern in java
    By sandeepsai39 in forum New To Java
    Replies: 2
    Last Post: 11-23-2009, 07:45 AM
  5. Replies: 3
    Last Post: 02-12-2008, 11:03 AM

Posting Permissions

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