Home > Archive > Java Help > May 2004 > Bad class file
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
|
|
| PageV 2004-05-20, 12:35 pm |
| I get
DoubleArr.java:11: cannot access Arrays2
bad class file: .\Arrays2.java
file does not contain class Arrays2
Please remove or make sure it appears in the correct subdirectory of the
classpath.
Arrays2.fill(d, new Arrays2.RandDoubleGenerator());
The program is
import com.bruceeckel.util.*;
public class DoubleArr
{
public static void main(String args[])
{
double[] d = new double[10];
Arrays2.fill(d, new Arrays2.RandDoubleGenerator());
Arrays2.print(d);
}
}
It works if I rename Arrays2.java to something else. Why is the compiler
looking at a java file instead of a class file? Am I supposed to put the
java files in the same directory as the class files? This is what I had to
do with previous programs to avoid this error. I would rather leave the
java files in the top directory and let the compiler create the directories
for the class files.
Ralph
| |
| Ryan Stewart 2004-05-20, 8:32 pm |
| "PageV" <vralphv@yahoo.com> wrote in message
news:2h4052F8r0utU1@uni-berlin.de...
> I get
> DoubleArr.java:11: cannot access Arrays2
>
> bad class file: .\Arrays2.java
>
> file does not contain class Arrays2
>
> Please remove or make sure it appears in the correct subdirectory of the
> classpath.
>
> Arrays2.fill(d, new Arrays2.RandDoubleGenerator());
>
> The program is
> import com.bruceeckel.util.*;
>
> public class DoubleArr
> {
> public static void main(String args[])
> {
> double[] d = new double[10];
> Arrays2.fill(d, new Arrays2.RandDoubleGenerator());
> Arrays2.print(d);
>
> }
> }
> It works if I rename Arrays2.java to something else. Why is the compiler
> looking at a java file instead of a class file? Am I supposed to put the
> java files in the same directory as the class files? This is what I had
to
> do with previous programs to avoid this error. I would rather leave the
> java files in the top directory and let the compiler create the
directories
> for the class files.
>
> Ralph
>
The .java file has to be within the correct directory structure, defined by
its package, for compilation. It does *not* have to be in the exact same
directory as the .class file, but the .class file also has to be within the
correct directory structure. If, as I suspect, Arrays2 is a class in the
com.bruceeckel.util package, then the class file must be at
..\com\bruceeckel\util\Arrays2.class relative to where you're trying to
compile. Since you apparently haven't compiled Arrays2 yet, it's looking in
that directory for the Arrays2.java file and not finding it.
| |
| PageV 2004-05-21, 12:31 am |
|
"Ryan Stewart" <zzanNOtozz@gSPAMo.com> wrote in message
news:RLadnS0nY7LCojDdRVn-vw@texas.net...
> "PageV" <vralphv@yahoo.com> wrote in message
> news:2h4052F8r0utU1@uni-berlin.de...
compiler[color=darkred]
the[color=darkred]
> to
> directories
> The .java file has to be within the correct directory structure, defined
by
> its package, for compilation. It does *not* have to be in the exact same
> directory as the .class file, but the .class file also has to be within
the
> correct directory structure. If, as I suspect, Arrays2 is a class in the
> com.bruceeckel.util package, then the class file must be at
> .\com\bruceeckel\util\Arrays2.class relative to where you're trying to
> compile. Since you apparently haven't compiled Arrays2 yet, it's looking
in
> that directory for the Arrays2.java file and not finding it.
>
Array2.class is in .\com\bruceeckel\util. Array2.java is in .\
I am wondering why the compiler looks at Array2.java at all.
Shouldn't it find Array2.class, which it does do if I rename or remove
Array2.java?
I don't want to manually create folders for java files, -D D:\classes
creates the directories for the class files for me. I was able to compile
files in the top directory.
Ralph
| |
| Bjorn Abelli 2004-05-21, 4:31 am |
|
"PageV" wrote...
>
> Array2.class is in .\com\bruceeckel\util. Array2.java is in .\
> I am wondering why the compiler looks at Array2.java at all.
The compiler doesn't *know* that Array2 is supposed to be
com.bruceeckel.util.Array2. In your code it's only named as Array2, which
*could* mean that it is in the default package (.\) or in java.lang (which
is "imported by default").
> Shouldn't it find Array2.class, which it does do
> if I rename or remove Array2.java?
As it doesn't know if it is in the default package, java.lang or
com.bruceeckel.util, it looks for it in all three places.
As it finds Array2.java in .\ first, but without corresponding .class in the
same directory, it simply believes that it has to compile it.
Then it discovers that the current location of the .java file doesn't
correspond to what is said inside, that it belongs to package
com.bruceeckel.util, and hence an error occurs.
As it already found a possible .java-file, it doesn't look further to see if
there is an Array2.class or Array2.java in the other directories, but as you
noticed, if it *doesn't* find it in the "default package", it continues to
search, and finds the class-file in the correct directory.
Here is another explanation of the implications:
http://www.kevinboone.com/classpath.html
// Bjorn A
| |
| PageV 2004-05-21, 10:31 pm |
|
"Bjorn Abelli" <DoNotSpam.bjorn_abelli@hotmail.com> wrote in message
news:2h5pq1F89r6lU1@uni-berlin.de...
>
> "PageV" wrote...
>
> The compiler doesn't *know* that Array2 is supposed to be
> com.bruceeckel.util.Array2. In your code it's only named as Array2, which
> *could* mean that it is in the default package (.\) or in java.lang (which
> is "imported by default").
>
>
> As it doesn't know if it is in the default package, java.lang or
> com.bruceeckel.util, it looks for it in all three places.
>
> As it finds Array2.java in .\ first, but without corresponding .class in
the
> same directory, it simply believes that it has to compile it.
>
> Then it discovers that the current location of the .java file doesn't
> correspond to what is said inside, that it belongs to package
> com.bruceeckel.util, and hence an error occurs.
>
> As it already found a possible .java-file, it doesn't look further to see
if
> there is an Array2.class or Array2.java in the other directories, but as
you
> noticed, if it *doesn't* find it in the "default package", it continues to
> search, and finds the class-file in the correct directory.
>
> Here is another explanation of the implications:
> http://www.kevinboone.com/classpath.html
>
>
> // Bjorn A
>
Thanks
Ralph
|
|
|
|
|