Packages concept in java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gaya3
    New Member
    • Aug 2007
    • 184

    #1

    Packages concept in java

    Hi,
    I have small doubt in packages concept..
    i have 2 java files Access1 & Access2 under Declare(folder)

    Access1:

    package Declare;
    class Access1 {
    String name;
    double bal;
    Access1(String n, double b) {
    name = n;
    bal = b;
    }
    void show() {
    if(bal<0)
    System.out.prin t(">");
    System.out.prin tln(name + ": $" + bal);
    }
    }


    *************** *************** *************** ***************

    Access2:

    class Access2 {
    public static void main(String args[]) {
    Access1 current[] = new Access1[3];
    current[0] = new Access1("K. J. Fielding", 123.23);
    current[1] = new Access1("Will Tell", 157.02);
    current[2] = new Access1("Tom Jackson", -12.33);
    for(int i=0; i<3; i++) current[i].show();
    }
    }


    *************** *************** *************** *************** *****
    When i tried to compile "Access2.ja va"

    Following error message is displayed..

    Access2.java:3: cannot access Access
    bad class file: .\Access1.java
    file does not contain class Access1
    Please remove or make sure it appears in the correct subdirectory of the classpath
    Access1 current[] = new Access1[3];
    ^
    1 error


    Anyone please say me ... rootcause for that error..


    -Thanks & Regards,
    Hamsa
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by gaya3
    Hi,
    I have small doubt in packages concept..
    i have 2 java files Access1 & Access2 under Declare(folder)

    Access1:

    package Declare;
    class Access1 {
    String name;
    double bal;
    Access1(String n, double b) {
    name = n;
    bal = b;
    }
    void show() {
    if(bal<0)
    System.out.prin t(">");
    System.out.prin tln(name + ": $" + bal);
    }
    }


    *************** *************** *************** ***************

    Access2:

    class Access2 {
    public static void main(String args[]) {
    Access1 current[] = new Access1[3];
    current[0] = new Access1("K. J. Fielding", 123.23);
    current[1] = new Access1("Will Tell", 157.02);
    current[2] = new Access1("Tom Jackson", -12.33);
    for(int i=0; i<3; i++) current[i].show();
    }
    }


    *************** *************** *************** *************** *****
    When i tried to compile "Access2.ja va"

    Following error message is displayed..

    Access2.java:3: cannot access Access
    bad class file: .\Access1.java
    file does not contain class Access1
    Please remove or make sure it appears in the correct subdirectory of the classpath
    Access1 current[] = new Access1[3];
    ^
    1 error


    Anyone please say me ... rootcause for that error..


    -Thanks & Regards,
    Hamsa
    1.) Please use code tags everytime when posting code.
    2.) Compile Access1.java first. Then pass the classpath of Access1.class to javac when compiling Access2.java.

    Comment

    • gaya3
      New Member
      • Aug 2007
      • 184

      #3
      Originally posted by r035198x
      1.) Please use code tags everytime when posting code.
      2.) Compile Access1.java first. Then pass the classpath of Access1.class to javac when compiling Access2.java.

      Hi,
      c:\Data\ Declare(folder)----- Access1.java & Access2.java
      I compiled Access1.java
      Then i did like
      c:\Data javac Declare.Access2 .java
      is that rt??
      but error "error: cannot read: training.Access 1.java" is thrown

      pl..

      -Hamsa

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by gaya3
        Hi,
        c:\Data\ Declare(folder)----- Access1.java & Access2.java
        I compiled Access1.java
        Then i did like
        c:\Data javac Declare.Access2 .java
        is that rt??
        but error "error: cannot read: training.Access 1.java" is thrown

        pl..

        -Hamsa
        You see my reply above there? It doesn't say anything about changing directory e.t.c. You simply need to pass the path of the class to the compiler.

        Read more about it here.

        Comment

        • gaya3
          New Member
          • Aug 2007
          • 184

          #5
          Originally posted by r035198x
          You see my reply above there? It doesn't say anything about changing directory e.t.c. You simply need to pass the path of the class to the compiler.

          Read more about it here.

          Thank u Admin..:-)


          -Hamsa

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by gaya3
            Thank u Admin..:-)


            -Hamsa
            The name is r035198x.
            You're welcome.

            Comment

            Working...