How to import class from package files under the same directory

Discussion in 'Java' started by lonelyplanet999, Oct 10, 2003.

  1. Hi,

    I'm learning java programming and I use java SDK 1.4 for writing
    programs.

    I met one problem about importing self developed class illustrated by
    below examples:

    File: c:\javapgm\Caltor.java

    File content:

    package Caltor;

    class Calculator {
    private String[] functions = {"+", "-", "*", "/", "="};

    String getFunctions() {
    String s = functions[0];
    for (int i=1; i<functions.length; i++)
    s += ", " + functions;
    return s;
    }
    }

    File: c:\javapgm\Tester.java

    File content:

    import Caltor.*;

    class Tester {
    public static void main (String[] args) {
    Calculator c = new Calculator();
    System.out.println(c.getFunctions());
    }
    }

    Compiling Caltor.java returned no error.
    Compiling Tester.java returned below error.

    Tester.java:4: package Caltor does not exist
    import Caltor.*;
    ^
    Tester.java:8: cannot access Calculator
    bad class file: .\Calculator.class
    class file contains wrong class: Caltor.Calculator
    Please remove or make sure it appears in the correct subdirectory of
    the classpath.
    Calculator c = new Calculator();
    ^
    2 errors

    I tried create a subdirectory named Caltor under c:\javapgm\ but still
    couldn't make compilation of Tester.java succeed.

    Could anyone help me how to fix this problem ? I found no clue from
    books teaching java programming nor could I find answer from sun's FAQ
    list for java programming.

    Tks!
     
    lonelyplanet999, Oct 10, 2003
    #1
    1. Advertising

  2. lonelyplanet999 wrote:

    > I met one problem about importing self developed class illustrated by
    > below examples:
    >
    > File: c:\javapgm\Caltor.java
    >
    > File content:
    >
    > package Caltor;
    >
    > class Calculator {

    []

    > File: c:\javapgm\Tester.java
    >
    > File content:
    >
    > import Caltor.*;
    >
    > class Tester {

    []

    > Compiling Caltor.java returned no error.
    > Compiling Tester.java returned below error.
    >
    > Tester.java:4: package Caltor does not exist
    > import Caltor.*;
    > ^
    > Tester.java:8: cannot access Calculator
    > bad class file: .\Calculator.class
    > class file contains wrong class: Caltor.Calculator


    First, the class Calculator should be defined in a file named
    Calculator.java. Second, it should be situated in a directory named Caltor.
    Then, you can compile it not from inside that directory, but from the
    root of your class tree, like this:

    javac Tester.java Caltor\Calculator.java
     
    Michael Borgwardt, Oct 10, 2003
    #2
    1. Advertising

  3. lonelyplanet999

    Roedy Green Guest

    On 10 Oct 2003 01:47:36 -0700,
    (lonelyplanet999) wrote or quoted :

    >File content:
    >
    >package Caltor;


    You don't import classes in the same package. You don't import
    classes in the default package. The default package is only for tiny
    experiments. As soon as you have multiple classes, give your classes
    a proper package name of the form com.mydomain.mypackage, all lower
    case.

    see http://mindprod.com/jgloss/import.html
    http://mindprod.com/jgloss/package.html

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 10, 2003
    #3
  4. lonelyplanet999

    Roedy Green Guest

    On Fri, 10 Oct 2003 10:59:29 +0200, Michael Borgwardt
    <> wrote or quoted :

    >directory named Caltor.


    Rather in a package named com.mydomain.caltor and in a directory
    called c:\com\mydomain\caltor

    all lower case.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 10, 2003
    #4
  5. lonelyplanet999

    dealmen Guest

    Michael Borgwardt <> wrote in message news:<bm5sdh$ipslj$-berlin.de>...
    > lonelyplanet999 wrote:
    >
    > > I met one problem about importing self developed class illustrated by
    > > below examples:
    > >
    > > File: c:\javapgm\Caltor.java
    > >
    > > File content:
    > >
    > > package Caltor;
    > >
    > > class Calculator {

    > []
    >
    > > File: c:\javapgm\Tester.java
    > >
    > > File content:
    > >
    > > import Caltor.*;
    > >
    > > class Tester {

    > []
    >
    > > Compiling Caltor.java returned no error.
    > > Compiling Tester.java returned below error.
    > >
    > > Tester.java:4: package Caltor does not exist
    > > import Caltor.*;
    > > ^
    > > Tester.java:8: cannot access Calculator
    > > bad class file: .\Calculator.class
    > > class file contains wrong class: Caltor.Calculator

    >
    > First, the class Calculator should be defined in a file named
    > Calculator.java. Second, it should be situated in a directory named Caltor.
    > Then, you can compile it not from inside that directory, but from the
    > root of your class tree, like this:
    >
    > javac Tester.java Caltor\Calculator.java


    Your suggestion couldn't pass the compiler. Please correct me if I
    interpret it incorrectly.

    Firstly, I created directory Caltor and copied Caltor.java to it,
    renamed it as Calculator.java. I also removed the package statment
    inside the new Calculator.java. Then I compiled Calculator.java then
    Tester.java.

    "javac Tester.java Caltor\Calculator.java" returned below error
    message:

    error: Cannot read: Caltor\Calculator.java
    Tester.java:8: cannot access Calculator
    bad class file: .\Calculator.class
    class file contains wrong class: Caltor.Calculator
    Please remove or make sure it appears in the correct subdirectory of
    the classpath.

    Calculator c = new Calculator();
    2 errors^

    I tried "javac Tester.java Caltor\Calculator.java" but same error
    appeared !

    I also tried re-inserted "package Caltor;" to Calculator.java but
    compilation result still the same.

    It seemed from the error message I need to change the classpath
    parameter but where can I do it under windows 98 environment ?
     
    dealmen, Oct 14, 2003
    #5
  6. dealmen wrote:
    > Your suggestion couldn't pass the compiler. Please correct me if I
    > interpret it incorrectly.
    >
    > Firstly, I created directory Caltor and copied Caltor.java to it,
    > renamed it as Calculator.java. I also removed the package statment
    > inside the new Calculator.java.


    No, you still need the package statement!
    Besides, the class should probably be declared as public.
     
    Michael Borgwardt, Oct 14, 2003
    #6
  7. lonelyplanet999

    dealmen Guest

    Roedy Green <> wrote in message news:<>...
    > On 10 Oct 2003 01:47:36 -0700,
    > (lonelyplanet999) wrote or quoted :
    >
    > >File content:
    > >
    > >package Caltor;

    >
    > You don't import classes in the same package. You don't import
    > classes in the default package. The default package is only for tiny
    > experiments. As soon as you have multiple classes, give your classes
    > a proper package name of the form com.mydomain.mypackage, all lower
    > case.
    >
    > see http://mindprod.com/jgloss/import.html
    > http://mindprod.com/jgloss/package.html


    I've read through your homepage, but still it couldn't help solve my
    problem. My problem is I defined class Calculator in one file, named
    it Caltor.java. Then I defined another class in Tester.java which
    called method of Calculator class. Initially I put these 2 files under
    the same subdirectory C:\JAVAPGM while all the java binaries &
    libraries being installed C:\J2SDK1.4.1_01\ since I don't want mixing
    my own written codes with that of standard java SDK.

    Is it not possible for my case to have Tester.java & Caltor.java be
    under the same subdirectory ? I tried put Caltor.java under
    C:\JAVAPGM\CALTOR but the result is the same.

    I also tried compiling via \j2sdk1.4.1_01\bin\javac -classpath
    "c:\javapgm" Tester.java but the result still the same.

    Anything I can do to solve the problem ?
     
    dealmen, Oct 14, 2003
    #7
  8. lonelyplanet999

    Roedy Green Guest

    On 14 Oct 2003 09:55:29 -0700, (dealmen) wrote
    or quoted :

    > My problem is I defined class Calculator in one file, named
    >it Caltor.java.


    See http://mindprod.com/jgloss/gettingstarted.html

    A public class named Calculator MUST live in a file called
    Calculator.java precisely, including case.

    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 14, 2003
    #8
  9. lonelyplanet999

    Sudsy Guest

    dealmen wrote:
    > I've read through your homepage, but still it couldn't help solve my
    > problem. My problem is I defined class Calculator in one file, named
    > it Caltor.java. Then I defined another class in Tester.java which
    > called method of Calculator class. Initially I put these 2 files under
    > the same subdirectory C:\JAVAPGM while all the java binaries &
    > libraries being installed C:\J2SDK1.4.1_01\ since I don't want mixing
    > my own written codes with that of standard java SDK.

    <snip>
    > Anything I can do to solve the problem ?


    I realize that it can be challenging when you first start working
    with packages so ignore it for the time being. Remove any "package"
    statements from your code and rename Caltor.java to Calculator.java;
    a class name XXX MUST BE contained in a file called XXX.java (where
    XXX can be anything you choose).
    Now simply compile the files and run the Tester (which I assume has
    a main method):
    $ javac Calculator.java Tester.java
    $ java Tester
    (where $ is the prompt)
     
    Sudsy, Oct 14, 2003
    #9
  10. "Roedy Green" <> wrote in message
    news:...
    > On 14 Oct 2003 09:55:29 -0700, (dealmen) wrote
    > or quoted :
    >
    > > My problem is I defined class Calculator in one file, named
    > >it Caltor.java.

    >
    > See http://mindprod.com/jgloss/gettingstarted.html
    >
    > A public class named Calculator MUST live in a file called
    > Calculator.java precisely, including case.


    Roedy, I'm not certain this is correct. The .java name I believe is
    immaterial. It's the .class name that must match.
    It's truly easier if you name the .java file the same as the desired .class
    file since the default is to produce the .class file name the same as the
    source, but I believe the .class file can be renamed if you wish. I'll check
    this later if I get time, but I urge you to try it too.
    --
    Gary
     
    Gary Labowitz, Oct 15, 2003
    #10
  11. "Roedy Green" <> wrote in message
    news:...
    > On 14 Oct 2003 09:55:29 -0700, (dealmen) wrote
    > or quoted :
    >
    > > My problem is I defined class Calculator in one file, named
    > >it Caltor.java.

    >
    > See http://mindprod.com/jgloss/gettingstarted.html
    >
    > A public class named Calculator MUST live in a file called
    > Calculator.java precisely, including case.


    Yikes. I missed that you said public class. Yes, it must be in the same
    named .java file. Sorry.
    --
    Gary
     
    Gary Labowitz, Oct 15, 2003
    #11
  12. lonelyplanet999

    Roedy Green Guest

    On Tue, 14 Oct 2003 21:43:17 -0400, "Gary Labowitz"
    <> wrote or quoted :

    >I'll check
    >this later if I get time, but I urge you to try it too.

    I have seen the error message many times.

    For non public classes where you have more than one independent class
    per *.java files, obviously the rule does not apply. However, for
    PUBLIC classes, you had better get it matching including case.

    When javac encounters a call to a method it has not yet compiled, it
    knows which *.java file to look in for the source to construct the
    call. If you name everything properly with packages and class names
    matching filenames, Javac can go to the right place, by checking for
    the existence of that file at each element of the classpath.


    --
    Canadian Mind Products, Roedy Green.
    Coaching, problem solving, economical contract programming.
    See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
     
    Roedy Green, Oct 15, 2003
    #12
  13. "Gary Labowitz" <> wrote in message news:<>...
    > "Roedy Green" <> wrote in message
    > news:...
    > > On 14 Oct 2003 09:55:29 -0700, (dealmen) wrote
    > > or quoted :
    > >
    > > > My problem is I defined class Calculator in one file, named
    > > >it Caltor.java.

    > >
    > > See http://mindprod.com/jgloss/gettingstarted.html
    > >
    > > A public class named Calculator MUST live in a file called
    > > Calculator.java precisely, including case.

    >
    > Yikes. I missed that you said public class. Yes, it must be in the same
    > named .java file. Sorry.



    Finally found 3 errors causing compilation failure:

    1. You are right, I missed declared class Calculator as public.

    2. Also, method getFunctions need to be declared as public, too. Below
    is corrected code for Calculator.java.

    package Caltor;

    public class Calculator {
    private String[] functions = {"+", "-", "*", "/", "="};

    public String getFunctions() {
    String s = functions[0];
    for (int i=1; i<functions.length; i++)
    s += ", " + functions;
    return s;
    }
    }

    3. I found the import statement inside Tester.java couldn't be set to
    import Caltor.* but Caltor.Calculator. I know that java requires only
    one public class be declared in each .java file but I don't know why
    programmer can use import <package>.* to import various classes from
    standard java packages but they can't do it with their own built class
    file. Below is modified Tester.java file.

    import Caltor.Calculator;

    class Tester {
    public static void main (String[] args) {
    Calculator c = new Calculator();
    System.out.println(c.getFunctions());
    }
    }

    The name of subdirectory where Calculator.java resided was not a
    matter. I still kept it named Caltor, no problem :>
     
    lonelyplanet999, Oct 15, 2003
    #13
  14. lonelyplanet999

    Jon Skeet Guest

    lonelyplanet999 <> wrote:
    > 3. I found the import statement inside Tester.java couldn't be set to
    > import Caltor.* but Caltor.Calculator. I know that java requires only
    > one public class be declared in each .java file but I don't know why
    > programmer can use import <package>.* to import various classes from
    > standard java packages but they can't do it with their own built class
    > file.


    They can. If you're getting a problem with that, it suggests you've got
    another copy of the Calculator class somewhere you shouldn't have, or
    you're not compiling in the right way.

    See http://www.pobox.com/~skeet/java/compiling.html

    --
    Jon Skeet - <>
    http://www.pobox.com/~skeet
    If replying to the group, please do not mail me too
     
    Jon Skeet, Oct 15, 2003
    #14
  15. lonelyplanet999

    SahanaDhandapani

    Joined:
    May 13, 2012
    Messages:
    1


    ------------------------------------------------------------------------------------------------


    Please cut the Caltor.java file from javapgm folder and paste it in Caltor folder.
    after that run the Tester.java. I think this will work
     
    SahanaDhandapani, May 13, 2012
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Parvinder
    Replies:
    6
    Views:
    748
    Thomas G. Marshall
    Feb 27, 2005
  2. George P
    Replies:
    3
    Views:
    685
    Alex Martelli
    Sep 11, 2004
  3. plb
    Replies:
    0
    Views:
    397
  4. Gabriel Rossetti
    Replies:
    1
    Views:
    497
    ryles
    Sep 20, 2009
  5. OKB (not okblacke)
    Replies:
    3
    Views:
    528
    OKB (not okblacke)
    Aug 14, 2011
Loading...

Share This Page