How to import class from package files under the same directory

L

lonelyplanet999

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!
 
M

Michael Borgwardt

lonelyplanet999 said:
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
 
D

dealmen

Michael Borgwardt said:
lonelyplanet999 said:
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 ?
 
M

Michael Borgwardt

dealmen said:
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.
 
D

dealmen

Roedy Green said:
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 ?
 
S

Sudsy

dealmen said:
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.
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)
 
G

Gary Labowitz

Roedy Green said:
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.
 
R

Roedy Green

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.
 
L

lonelyplanet999

Gary Labowitz said:
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 :>
 
J

Jon Skeet

lonelyplanet999 said:
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
 
Joined
May 13, 2012
Messages
1
Reaction score
0
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!


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


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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top