Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

B.java:7: cannot access A--bad class file: .\A.java

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi to all,

i am facing the following error while accessing methods of another class.

Error



B.java:7: 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



my program code






i compiled A.java with the 'javac -d . A.java and B.java with javac B.java



While compiling B.java i am getting the above error.

Please can any one tell how to resolve my problem.

Waiting for your valuable replys.

Regards,
Narasimha Raju.Naidu
 
Marshal
Posts: 70302
283
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not certain, but it sounds as though your file and directory structure might not be quite right. You might not have the naming of the A and B files quite right, too.

Try compiling A first with the -d tag:

javac -d . A.java

then

javac -d . B.java

or both together

javac -d . A.java B.java

That might help.
 
Narasimha Raju Naidu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your valuable reply. right now i am not in front of my system. i will. if it suceeded then how to compile my program B.java


regards,
 
Narasimha Raju Naidu
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi friends,

Still i am getting the same error.

can any one resolve

Regards
[ April 16, 2008: Message edited by: Narasimha Raju Naidu ]
 
Ranch Hand
Posts: 70
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Narasimha,

Did your java file "A" is inside a correct package structure?
It should be in a directory called "dawn" which itself a subdirectory to "pan"

(i.e) "A" file should be in pan\dawn\A.java

The java file "B" should be outside to "pan" directory

Put your files like that way and try it
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi,

I think the given error is because of * placed after package name.[packagename.*;].

If you place a particular class name insted of *, it compiles successfully.

Reason is , when we use -d option, it moves only .class file to the package hierarchy but leaves .java file at original location.

So because we are using *, compiler dont know the class to use at starting, and thus looks for required class file in current directory insted of package. In current directory it gets .java file for that class, so it skips searching and compiles .java [ hope without -d option] and place the .class file at current directory [without creation of hierarchy] which is not reght as we have decleared package in source file.

To overcome this problem we either need to move .java file to same package hierarchy created after compilation of it using -d option,
or move to other location / delete the source file after compiling it using -d option.

If anyone have more useful way to deal with this, please do post.
 
author
Posts: 23883
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ninad Sabnis wrote:
If anyone have more useful way to deal with this, please do post.



Since this topic is more than a year old, I think it is safe to assume that the OP has figured out how to compile a program by now. And BTW, welcome to the Ranch.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Mr.Narasimha Raju Naidu

i too got this error.
for this i did one thing that is simply i had cut and pasted those .java files into the package which are created by JVM.
paste your .java files into the packages.

do like this
pan
dawn
A.java
okay
 
A wop bop a lu bop a womp bam boom! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic