Results 1 to 13 of 13

Thread: Java Packages

  1. #1
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default Java Packages

    Hi! Everybody,
    I'm new to java. Please help me solve this problem:
    I have one package named: pack in d:\
    In "pack" I have two other package: " A" and "B".(d:\pack\A and d:\pack\B)
    Package "A" consist of a file known as "Greet.java"
    Package "B" consist of a file known as "TestGreet.java"
    Greet.java:
    package pack.A;
    public class Greet{
    public void disp()
    {
    System.out.println("In Greet Class!");
    }
    }


    TestGreet.java:
    package pack.B;
    import pack.A.*;
    public class TestGreet{
    public static void main(String args[])
    {
    Greet G= new Greet();
    G.disp();
    }
    }


    I tried setting classpath to d:\
    so files got compiled but not executed...
    Please, let me know what can be done to execute it...
    Regards,
    Shital jain.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    What happens when you execute them? Errors? If so, post the exact error message(copy/paste not paraphrasing)

  3. #3
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    And tell us the command you use to run the program and location from where you run it (if you are using the command line), or what IDE you are using (if you are not).

  4. #4
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    Hi!
    The execution went this way....

    C:\>set classpath=d:\

    C:\>d:

    D:\>cd pack

    D:\pack>javac a\Greet.java

    D:\pack>javac b\TestGreet.java

    D:\pack>java b\TestGreet
    Exception in thread "main" java.lang.NoClassDefFoundError: b\TestGreet

    D:\pack>


    ShitalJain

  5. #5
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    simply using notepad editor and jdk1.6

  6. #6
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Cd all the way into the package and just type "java TestGreet"

  7. #7
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    nothing is working...
    I tried:
    D:\pack>cd\

    D:\>java TestGreet
    Exception in thread "main" java.lang.NoClassDefFoundError: TestGreet

    D:\>cd pack

    D:\pack>java TestGreet
    Exception in thread "main" java.lang.NoClassDefFoundError: TestGreet

    D:\pack>cd B

    D:\pack\B>java TestGreet
    Exception in thread "main" java.lang.NoClassDefFoundError: TestGreet

    D:\pack\B>

    Please let me knoe what exactly has to be tried...

  8. #8
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Where is the .class file located?

  9. #9
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Default

    its there in B for TestGreet and A for Greet

  10. #10
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Try

    Java Code:
    D:\>dir pack\B
    D:\>java -cp . pack.B.TestGreet
    (The first command is just to check that TestGreet.class really does exist.) The classpath is specified here using the "-cp ." arguments. This is more flexible than setting an OS variable.

    The java executable expects the name of the class which has the main() method. This is the full name of the class, ie including the package of which it is a part.

  11. #11
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Someone else will hopefully help you soon. I must be too tired, I am unable to even replicate the errors(damn compilation error I am running into...hence the too tired part).

  12. #12
    ShitalJain is offline Member
    Join Date
    Jun 2011
    Posts
    10
    Rep Power
    0

    Cool

    Quote Originally Posted by pbrockway2 View Post
    Try

    Java Code:
    D:\>dir pack\B
    D:\>java -cp . pack.B.TestGreet
    (The first command is just to check that TestGreet.class really does exist.) The classpath is specified here using the "-cp ." arguments. This is more flexible than setting an OS variable.

    The java executable expects the name of the class which has the main() method. This is the full name of the class, ie including the package of which it is a part.
    Hi!,
    It really worked......
    Thank you and all for help...
    ShitalJain.

  13. #13
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

Similar Threads

  1. Java packages and confusion (Yes, I've RTFM)
    By unaligned in forum New To Java
    Replies: 12
    Last Post: 06-03-2011, 05:45 PM
  2. How to find java packages
    By h.ghorashi in forum New To Java
    Replies: 1
    Last Post: 02-16-2011, 06:03 PM
  3. Java packages / statsic problem - Please help!
    By brendan_1986 in forum New To Java
    Replies: 14
    Last Post: 02-09-2011, 01:51 PM
  4. Java Packages/Jars
    By Silentstormz in forum Advanced Java
    Replies: 4
    Last Post: 09-14-2008, 07:58 PM
  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
  •