Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How to import class from package files under the same directory

Reply
Thread Tools

How to import class from package files under the same directory

 
 
lonelyplanet999
Guest
Posts: n/a
 
      10-10-2003
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[i];
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!
 
Reply With Quote
 
 
 
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-10-2003
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

 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      10-10-2003
On 10 Oct 2003 01:47:36 -0700, http://www.velocityreviews.com/forums/(E-Mail Removed)
(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.
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-10-2003
On Fri, 10 Oct 2003 10:59:29 +0200, Michael Borgwardt
<(E-Mail Removed)> 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.
 
Reply With Quote
 
dealmen
Guest
Posts: n/a
 
      10-14-2003
Michael Borgwardt <(E-Mail Removed)> wrote in message news:<bm5sdh$ipslj$(E-Mail Removed)-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 ?
 
Reply With Quote
 
Michael Borgwardt
Guest
Posts: n/a
 
      10-14-2003
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.

 
Reply With Quote
 
dealmen
Guest
Posts: n/a
 
      10-14-2003
Roedy Green <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>. ..
> On 10 Oct 2003 01:47:36 -0700, (E-Mail Removed)
> (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 ?
 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      10-14-2003
On 14 Oct 2003 09:55:29 -0700, (E-Mail Removed) (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.
 
Reply With Quote
 
Sudsy
Guest
Posts: n/a
 
      10-14-2003
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)

 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      10-15-2003
"Roedy Green" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> On 14 Oct 2003 09:55:29 -0700, (E-Mail Removed) (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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Relative import from script with same name as package OKB (not okblacke) Python 3 08-14-2011 06:48 PM
on package import, have it conditionally import a subpackage Gabriel Rossetti Python 1 09-20-2009 09:18 PM
How to find directory of package residence from within package? Krishna Chaitanya Perl Misc 5 04-03-2009 05:19 PM
single package import v/s the entire package Parvinder Java 6 02-27-2005 02:02 PM
package module import name clash with global package George P Python 3 09-11-2004 01:19 PM



Advertisments