bad class file error!!!Please help me

I got a problem that is very strange. I have two source files saved in the same directory: E:\Java Programmes

First one: A.java

package util;

publicclass A{

publicint add(int x,int y){

return (x + y);

}

}

Second one: B.java

import util.*;

publicclass B{

publicstaticvoid main(String args[]){

A a =new A();

System.out.println("Result: " + a.add(3, 5));

}

}

I compiled these two files and got problem as follow:

*compiling file A.java is ok, no problem.

E:\Java Programmes>javac -d "E:\Java Programmes" A.java

*but got problem with B.java

E:\Java Programmes>javac B.java

B.java:5: cannot access A

bad class file: .\A.java

file does not contain class A

Please remove or make sure it appears in the correct subdirectory of the classpa

th.

A a = new A();

^

1 error

*The strange thing is that when i modify the file B.java as follow, it can run well

import util.A;

publicclass B{

publicstaticvoid main(String args[]){

A a =new A();

System.out.println("Result: " + a.add(3, 5));

}

}

Output = Result: 8.

I keep wondering why it is like that. Because the statement "import util;*; " does not work well while the statement import "util.A ; " can work well. Please, anyone, explain it to me. Thannks a lot.

[2538 byte] By [beckham12a18a] at [2007-10-1 18:29:18]
# 1

> I compiled these two files and got problem as

> follow:

>

> *compiling file A.java is ok, no problem.

> E:\Java Programmes>javac -d "E:\Java Programmes"

> A.java

>

This should create E:\Java Programmes\util\A.class. Correct?

>

> *but got problem with B.java

> E:\Java Programmes>javac B.java

> B.java:5: cannot access A

> bad class file: .\A.java

> file does not contain class A

> Please remove or make sure it appears in the correct

> subdirectory of the classpath.

So it appears that your classpath contains the current directory, "E\Java Programmes" The compiler is finding the A.java file and is trying to use it. But the A.java file is not in a directory named util.

> I keep wondering why it is like that. Because the

> statement "import util;*; " does not work well while

> the statement import "util.A ; " can work well.

> Please, anyone, explain it to me. Thannks a lot.

When you use "import util.*; " and the compiler finds a reference to a class named A, it does not know that you mean util.A class. It looks for A, finds the A.java file and tries to use it. When you use "import util.A; " it knows to look for a util\A.class or util\A.java file.

If you want to separate the source code files from the .class files, you should not have the source code files in the Classpath. If you have .java files in the Classpath, put them into the correct directory structure.

atmguya at 2007-7-11 13:24:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 2

> When you use "import util.*; " and the compiler finds

> a reference to a class named A, it does not know that

> you mean util.A class. It looks for A, finds the

> A.java file and tries to use it. When you use

> "import util.A; " it knows to look for a util\A.class

> or util\A.java file.

I beg to disagree with you. Since the import statement "import util.*" mean import all classes in util package. Do you agree with me this? For example, "import java.util.*" means including all classes in util package of java, right?. Therefore, import "util.*" in my program might include class A in the file in which it is called. I can not understand why the compiler notified that "cannot access class A"? when the file A.class is placed in the "E:\Java Programmes\util" directory.

beckham12a18a at 2007-7-11 13:24:59 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 3
By the way, some one can try my program in your computer, if you get it to work well, please show me how to do. Thanks a lot?
beckham12a18a at 2007-7-11 13:25:00 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 4

> I beg to disagree with you. Since the import

> statement "import util.*" mean import all classes in

> util package. Do you agree with me this?

import.* is a way to tell the compiler that some of the references to classes in your code might be found in that package. When you have public A a_object = new A();

the javac compiler does not explicitly know that the class is util.A. It has to search through the Classpath directories to find a suitable .class or .java file. In your case, the Classpath includes E:\Java Programmes. When the compiler looks in that directory, it apparently finds the A.java file before it finds the util\A.class file. The A.java is suitable as far as the compiler is concerned. It then tries to compile the source code, but when it does, it finds that the file contains util.A, not plain A as expected. The javac compiler is not smart enough to keep looking for a better .class or .java file.

When you use "import util.A;" the compiler knows that A.java is not suitable since it is not in a util directory.

If you move the A.java file into the util directory, you can make it work. Or, move the A.java file into a directory which is not in the Classpath.

atmguya at 2007-7-11 13:25:00 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 5

Thanks a lot. I stick to your instructions placing A.java file in the "util" directory and i got it. My proram now can work well. By the way, can i ask : as you said compiler looks at the Classpath to find .class file. And to deal with this, i set my Classpath using conditional compling as follow:

E:\Java Programmes>javac -classpath "E:\Java Programmes\util" B.java

By this above statement, i want compiler to look for .class files in util directory, but the statement produced compiling error. Can you show me how to use the above command correctly?

beckham12a18a at 2007-7-11 13:25:00 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...
# 6

javac searches the Classpath for a suitable .class file. By suitable, I mean that the file path relative to the Classpath matches the package name. All classes in Java have a fully qualified class name. The fully qualified name is the package name plus the class name. The .class file must reside in a directory structure matching the package name, relative to the Classpath.

In your case, the fully qualified class name is util.A. So somewhere on your computer (or in a jar file), there must be a directory named util and the A. class file must be in it. The util directory must be in a directory that is in the Classpath.E:\Java Programmes>javac -classpath "E:\Java Programmes" B.javatells javac to look inside e:\Java Programmes when looking for classes and packages. For a class named util.A javac will look in E:\Java Programmes for a util directory that contains A.class.

atmguya at 2007-7-11 13:25:00 > top of Java-index,Developer Tools,Debugging and Profiling Tool APIs...