This week's book giveaway is in the Performance forum.
We're giving away four copies of The Java Performance Companion and have Charlie Hunt, Monica Beckwith, Poonam Parhar, & Bengt Rutisson on-line!
See this thread for details.
Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

javac questions (package not public tho it seems to be)

 
Marlon Churchill
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm working through Exercise 1.1 of the Sierra and Bates SJCP book. I have an abstract file Fruit.java:


and the class Apple :


Apple.java is in directory Exercise 1.1 and Fruit.java is in Exercise 1.1/food

I try various attempts at compiling and get:
C:\Users\Marlon\workspace\Exercise 1.1>javac Apple.java food\Fruit.java -cp food

Apple.java:1: food.Fruit is not public in food; cannot be accessed from outside
package
import food.Fruit;
^
food\Fruit.java:2: duplicate class: food.Fruit
abstract class Fruit
^
Apple.java:2: cannot access Fruit
bad class file: food\Fruit.java
file does not contain class Fruit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
class Apple extends Fruit {
^

C:\Users\Marlon\workspace\Exercise 1.1>javac Apple.java -cp food
Apple.java:1: package food does not exist
import food.Fruit;
^
Apple.java:2: cannot access Fruit
bad class file: food\Fruit.java
file does not contain class Fruit
Please remove or make sure it appears in the correct subdirectory of the classpa
th.
class Apple extends Fruit {

I'm not sure what is going on. Fruit is abstract so that implies public, correct? I have tried adding public explicitly and it didn't make a difference.
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Marlon. Welcome to The Ranch!

Marlon Churchill wrote:Fruit is abstract so that implies public, correct?

No, it needs the public specifier. There may be other things going on as well, but you definitely need that.

The other problem is probably specifying food as the classpath. That needs to be the root of the hierarchy. Otherwise, the compiler will be looking for a folder called food (to match the package) inside the folder food (to match the classpath).
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not experienced with creating abstract classes, but couldn't (in this case, shouldn't) your abstract class Fruit be specified as public?
 
Marlon Churchill
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Yes, adding public did it. Thought I'd tried that and it didn't work.

Maybe I was thinking of methods in interfaces that don't require the public modifier.
 
Marlon Churchill
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh yes, and just using the command

javac Apple.java

or even

javac Apple.java food/Fruit.java

Leaving of the -cp food helped. Thanks for the tip on where the compiler is looking for packages.

Thanks to all who responded.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic