Page 1 of 2 12 LastLast
Results 1 to 20 of 22
Like Tree8Likes

Thread: Issue with Using packages

  1. #1
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Issue with Using packages

    I have created three packges

    three classes >> AClass (SuperClass) >> BCLass(Subclass) and DemoClass

    AClass is in package p1
    BClass is in Package p2 and is sublcass of AClass
    DemoPackage is in package in p3


    Error I am getting while compiling BClass is : Cannot find package p1.
    Directory structure is :

    C:\Users\John\Documents\Java Tuts\Complete Java Ref\Package\UsingPackage

    This has three folder p1 , p2 and p3 and respective files are placed in respective folder.

    Here are my code please advice what mistake I am doing.

    Java Code:
    package p1;
    public class AClass
    {
    public AClass()
    {
    System.out.println("This is p1.AClass Constructor");
    }//End>Constructor
    
    public void Show()
    {
    System.out.println("This is method A in P1");
    }//End>Method
    }//End>Class
    Java Code:
    package p2;
    import p1.*;
    
    public class BClass extends p1.AClass
    {
    public BClass()
    {
    System.out.println("This is p2.BClass Constructor");
    }//End>Constructor
    
    public void Show()
    {
    System.out.println("This is method B in P2");
    }//End>Method
    }//End>Class
    Java Code:
    package p3;
    import p2.*;
    
    class DemoPackage
    {
    public static void main(String[] args)
    {
    //objects
    p1.AClass obj1= new p1.AClass();
    System.out.println(obj1.show());
    
    obj1 = new p2.BClass();
    System.out.println(obj1.show());
    }//End>Main
    }//End>Class

    Please help, thank you for your help.

  2. #2
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Issue with Using packages

    Somebody please help me ??

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

    Default Re: Issue with Using packages

    Did you try compiling those three source files all at once? i.e. pass those three .java files on the command line of the javac command (including their paths).

    kind regards,

    Jos
    John2find likes this.
    cenosillicaphobia: the fear for an empty beer glass

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Issue with Using packages

    What command did you give to compile?
    The full command, that is.
    John2find likes this.
    Please do not ask for code as refusal often offends.

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

    Default Re: Issue with Using packages

    Quote Originally Posted by Tolls View Post
    What command did you give to compile?
    The full command, that is.
    Let me guess: you want to see that the classpath is not being set properly, right? ;) Because I'm quite positive that the problem lies there. Its a nasty path with spaces in it too, I would pick an easier spot to store projects like c:\java\projects.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Issue with Using packages

    Or, there's no -cp used and they have quicktime installed somewhere...:)
    Please do not ask for code as refusal often offends.

  7. #7
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Issue with Using packages

    well I have sorted the issue a bit, by some research.
    I was using the wrong command while compiling, i was not defining -classpath variable.
    So was able to solve the error ,by using this command line :

    C:\Users\John\Documents\Java Tuts\Complete Java Ref\Package\UsingPackage1>javac -classpath . p3\DemoPackage.java

    This has compiled successfully but I am getting another error now while running the DemoPackage.

    Exception in thread "main" java.lang.NoClassDefFoundError: DemoPackage (wrong name: p3/DemoPackage)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknow n Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.launcher.LauncherHelper.checkAndLoadMain(Unkno wn Source)
    Any help on this now ?
    gimbal2 likes this.

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Issue with Using packages

    And what command are you using to run the class file?
    John2find likes this.
    Please do not ask for code as refusal often offends.

  9. #9
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Issue with Using packages

    i used :

    C:\Users\John\Documents\Java Tuts\Complete Java Ref\Package\UsingPackage1>java -classpath . p3\DemoPackage

  10. #10
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Issue with Using packages

    I liked your post because you did research on your own accord and I can only applaud that; so many people don't.

    But you're not entirely there yet though, with your initial research I feel confident enough I'm not robbing you of a learning experience to spell it out.

    Let me set an example. Say you have a folder c:\java\projects\myproject. In myproject you have three packages; p1, p2 and p3. So to make that crystal clear: you have the folders

    c:\java\projects\myproject\p1
    c:\java\projects\myproject\p2
    c:\java\projects\myproject\p3

    Now, if you want the p1, p2 and p3 packages to be on the Java classpath, you need to add the directory 'c:\java\projects\myproject' to the classpath. See how that works? The directory in which your actual Java packages exist.

    Ex:

    java - cp .;c:\java\projects\myproject p3.DemoPackage

    This classpath adds two elements: the current working directory and c:\java\projects\myproject. Doing it this way makes it so it doesn't matter WHERE you invoke the class, it will always work. If you rely only on '.' (the current working directory), you have to point your command prompt to the exact correct directory which becomes a bother once you start to expand your application more.

    See also how to specify the class to run with the full package.
    Last edited by gimbal2; 08-05-2013 at 12:47 PM.
    John2find likes this.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  11. #11
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Issue with Using packages

    Quote Originally Posted by John2find View Post
    i used :

    C:\Users\John\Documents\Java Tuts\Complete Java Ref\Package\UsingPackage1>java -classpath . p3\DemoPackage
    (Take note of gimbal2's bit about the classpath, but...)
    The bolded part is the problem.
    That 'java' command expects a full class name, which means the full package name and classname as if you were using the 'import' statement...ie with '.' as a separator, not the file system '/'.
    gimbal2 and John2find like this.
    Please do not ask for code as refusal often offends.

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

    Default Re: Issue with Using packages

    Yeah I was confused by that last bit before I edited my last post, I glanced over it too quickly and thought it was part of the classpath definition ;)
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Issue with Using packages

    Thank you Gimabal2 and Tolls.

    It worked finally.

    I used the command >>
    C:\Users\John\Documents\Java Tuts\Complete Java Ref\Package\UsingPackage1>java -classpath . p3/DemoPackage

    and it worked.
    and I got the concept of how classpath works, one need to mention from where the javac(compiler) need to look for classes that are going to be used.

    and the use this "/" in java than "\" unlike windows

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

    Default Re: Issue with Using packages

    I have no clue how that is ever going to work :s
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    John2find is offline Member
    Join Date
    Jul 2013
    Posts
    14
    Rep Power
    0

    Default Re: Issue with Using packages

    Quote Originally Posted by gimbal2 View Post
    I have no clue how that is ever going to work :s
    Yep its working with either "/" or "." as a seperator.

  16. #16
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Issue with Using packages

    Weird. I wonder if that was sneakily added at one point in time since Oracle took over, because the proper documented way to do it is by using a proper fully qualified class name, which is p3.DemoPackage. I would get in the habit of doing it that way always.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  17. #17
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    Default Re: Issue with Using packages

    When you run this command:
    java -classpath . p3.DemoPackage
    from the correct directory (the one containing the package directories) what error do you get?
    Please do not ask for code as refusal often offends.

  18. #18
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,083
    Rep Power
    4

    Default Re: Issue with Using packages

    There is no more error, the guy got it working.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

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

    Default Re: Issue with Using packages

    Oops.
    I misread "working with either" as "not working with either"...
    That doesn't bode well for today.

    And it's not good if Oracle have buggered up how the java command works....
    Please do not ask for code as refusal often offends.

  20. #20
    Sumit is offline Member
    Join Date
    Aug 2013
    Posts
    1
    Rep Power
    0

    Default Re: Issue with Using packages

    Quote Originally Posted by John2find View Post
    Somebody please help me ??

    use IDE for java code

Page 1 of 2 12 LastLast

Similar Threads

  1. [SOLVED] Class paths and Packages issue
    By kammce in forum New To Java
    Replies: 14
    Last Post: 07-12-2012, 05:10 AM
  2. Regular Expression issue and setName() method issue
    By geforce in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 03:33 AM
  3. packages
    By fogus in forum New To Java
    Replies: 1
    Last Post: 03-24-2009, 06:14 AM
  4. 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
  •