• Post Reply Bookmark Topic Watch Topic
  • New Topic

class file contains wrong class error , i dont understand it so fandmental  RSS feed

 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all
yes i beggienr thank you for asking ...
i have simple 2 java class's that i can't compile right .
my first java class MyStruct.java i compile it like that :
%JAVA_HOME%\bin\javac -d D:\ Java\test MyStruct.java
the file lookes like this :


and it creates me the class in com/lb/mystruct/ folder

now the second java file looks like this :



and I sittes in D:\ Java\test
when I try to compile it like this :
%JAVA_HOME%\bin\javac -cp D:\Java\test\com\lb\mystruct MyTest.java

im getting the error:


MyTest.java:2: package com.lb.mystruct does not exist
import com.lb.mystruct.*;
^
MyTest.java:10: cannot access MyStruct
bad class file: D:\ Java\test\com\lb\mystruct\MyStruct.class
class file contains wrong class: com.lb.mystruct.MyStruct
Please remove or make sure it appears in the correct subdirectory of the classpath.
MyStruct ms = new MyStruct();
^
2 errors

but I don�t get it I included the class path and all and the class is there why it does not recognaze it?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The clue is in this message that the compiler gives you:

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

Instead of "-cp D:\Java\test\com\lb\mystruct", you should add the base directory that contains the package in the classpath: "-cp D:\Java\test" (remove the part: "com\lb\mystruct").
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hello and thanks for the fast reply
ok i did it but still geting errors :
D:\Java\test>%JAVA_HOME%\bin\javac -cp D:\Java\test MyTest.java

MyTest.java:10: cannot access MyStruct
bad class file: D:\Java\test\MyStruct.java
file does not contain class MyStruct
Please remove or make sure it appears in the correct subdirectory of the classpath.
MyStruct ms = new MyStruct();
^
1 error

what im doing wrong now?
 
sven studde
Ranch Hand
Posts: 148
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what im doing wrong now?

Did you erase your import statement? Post your new code.

but I don�t get it I included the class path and all

The classpath isn't the same as the full path to the file. The classpath is the path to the directory containing the package.
[ October 04, 2006: Message edited by: sven studde ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well the code include the import
and i tried every thing here but still im geting the same error



and the javac is :
%JAVA_HOME%\bin\javac -cp D:\Java\test\com\lb\mystruct MyTest.java
or
%JAVA_HOME%\bin\javac -cp .;D:\Java\test\com\lb\mystruct MyTest.java

and still .. what is wrong here ? its driving me crazy .
tahnks
 
Bobby Roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll see what I can find out for you. packages and classpaths gave me absolute fits when I was first learning them; so I comletely understand your frustration. The error you are getting is one I remember vividly.
[ October 04, 2006: Message edited by: Bobby Roberts ]
 
Bobby Roberts
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried it on my comp and it works as you originally typed it. Except I created the directories tree myself and placed the MyStruct.java file in that directory instead of the D:\Java\test\ directory as you have yours. And this is what is causing your problem.

I'm looking at your error message:

MyTest.java:10: cannot access MyStruct
bad class file: D:\Java\test\MyStruct.java
file does not contain class MyStruct
Please remove or make sure it appears in the correct subdirectory of the classpath.
MyStruct ms = new MyStruct();
^
1 error


The bolded line is your clue.

It appears that the compiler is getting confused on which "MyStruct" to use. The problem is that you have your MyStruct.java file in the same directory that you are trying to compile MyTest.java from. Eventhough you have the correct MyStruct.class in the package, the compiler is looking at MyStruct.java which is in the D:\Java\test\ directory since it happens to have the same name, as your class file you are trying to use "MyStruct". It cannot find what it is looking for in D:\Java\test\MyStruct.java, hence the error: bad class file: D:\Java\test\MyStruct.java

In other words, it is trying to use D:\Java\test\MyStruct.java to find the class information it is looking for. The thing you have to do is move the MyStruct.java file out of that directory and try compiling MyTest.java again. The compiler should then use the correct MyStruct.class file that is in your package.

I ran into this problem many times, and finally got to the point where I created the directories myself when making a package so I'd never run into this problem, because it can easily throw you off.


Once you get your program working try this for kicks.

Take the two files you created, MyStruct.java and MyTest.java and remove the package and import statements from them. Save them both in the same directory, (use D:\Java\NEWTEST\).

Compile ONLY the MyTest.java file. The compiler will automatically compile your MyStruct.java file for you. So when the compile is complete you will have TWO class files. This further explains what the compiler was trying to do in your original problem and why it was blowing up.
[ October 04, 2006: Message edited by: Bobby Roberts ]
 
Meir Yan
Ranch Hand
Posts: 599
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Big thanks !!!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!