• 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Access Modifier

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I do have following two piece of codes. My both .java files are located under "C:\Knowledge Center\Java\Practice\AssessmentTest" directory on Windows OS.





Actual Result:
C:\Knowledge Center\Java\Practice\AssessmentTest>javac Chimp.java
Chimp.java:5: error: cannot access Rope
               Rope.swing();
               ^
 bad source file: .\Rope.java
   file does not contain class Rope
   Please remove or make sure it appears in the correct subdirectory of the sourcepath.
1 error

Expected Result:
swing swing 10

Why it is throwing compile time error? Do I need to include "package rope" in second piece of code?

 
Sheriff
Posts: 15791
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jnrohit Jain wrote:
Do I need to include "package rope" in second piece of code?


Is there anything that's keeping you from trying that out to see if you figured out the problem yourself?
 
Junilu Lacar
Sheriff
Posts: 15791
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that with Java, declaring that your class is in package x means that the directory your .java file resides in should be named x. You said that your .java files are both located in C:\Knowledge Center\Java\Practice\AssessmentTest. That's going to be a problem, too.
 
Junilu Lacar
Sheriff
Posts: 15791
264
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you can't get that to work, there's a few more "gotchas" that you need to know. If you just want it to see the program run, you can remove the package declarations in both files. This puts your classes in the default (unnamed) package and you should be able to compile and run the programs from whatever directory you happen to have them in.

Note that using the default package is just a quick-and-dirty fix suitable only for trivial programs with only a few .java files involved. You should always specify a package in which your class resides when you go on to write more serious programs that use many classes in many .java files.
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

jnrohit Jain wrote:I do have following two piece of codes. My both .java files are located under "C:\Knowledge Center\Java\Practice\AssessmentTest" directory on Windows OS.

Expected Result:
swing swing 10

Why it is throwing compile time error? Do I need to include "package rope" in second piece of code?



Not necessarily. With what you have. Your Chimp program need to locate your Rope Class, which should be located in the rope directory.
However, in your Chimp program that was not stated. Don't expect java to expand your import rope.*; to located your class there in. You have got to do it yourself.

Secondly, since you have the import static you really need not do any more. will still achieve the same purpose, just like you are using LENGTH.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic