File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes javac questions (package not public tho it seems to be) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "javac questions (package not public tho it seems to be)" Watch "javac questions (package not public tho it seems to be)" New topic
Author

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

Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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.


Let the Washing Machine Tribe conquer all!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4490
    
    8

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

Joined: Oct 24, 2010
Posts: 563
I'm not experienced with creating abstract classes, but couldn't (in this case, shouldn't) your abstract class Fruit be specified as public?


Always learning Java, currently using Eclipse on Fedora.
Linux user#: 501795
Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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

Joined: May 08, 2011
Posts: 12
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: javac questions (package not public tho it seems to be)