File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes using the javac -cp command Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using the javac -cp command" Watch "using the javac -cp command" New topic

using the javac -cp command

Doyle Davis

Joined: Feb 13, 2011
Posts: 4

I have been trying get a better understanding of the word "public" when it is used in front of "class"
The program shown below will not compile with the word "public" in front of the word "class" but when I remove the word "public" from each class, save the program as and compile it using the javac command , it compiles and produces the simple result.

Several of you have been very helpful by pointing out that classes are organized in packages and that you declare a package using the "package" keyword at the top of the class. Since in my case, I don't have this keyword, all three classes A,B, and C are in the so called "default package" (or in the same folder as I understand this to mean).

So if I understand this, public classes have to be in their own file. In other words, I should create an file, , and file (which is the one shown below). So I did this and created a folder called "src" . Inside this folder are the three java files. When I navigate to the folder and issue the command javac , I got the message:

Doyle-Daviss-iMac:src doyledavis$ javac class A is public, should be declared in a file named
public class A{
^ class B is public, should be declared in a file named
public class B{
I then tried issuing the command javac (Is there a space between javac and -cp ?) and got the message
Doyle-Daviss-iMac:src doyledavis$ javac
javac: file not found:

I think I am almost there in understanding this. Any suggestions will be greatly appreciated.
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

If you declare a public class A then it need to be in the file So you can only have 1 public class per actual file.

And yes there needs to be a space in between. The correct syntax is:
javac -cp .

But that is the default setting if no CLASSPATH environment variable is set.

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11955

There should also be spaces both before and after the dot...

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Ernie Mcracken
Ranch Hand

Joined: Feb 13, 2011
Posts: 33 class B is public, should be declared in a file named

Classes declared public need to be in separate .java files when you compile them. So you need files and

To compile do javac contains your main method so this is the target for the compiler. You shouldn't need to use -cp.

You're on a gravy train with biscuit wheels Roy.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15084

You say you put class A and B in their own source files and, but those compiler error messages tell you that you actually have classes A and B defined in the source file called

Check the content of your source file again. Make sure public classes A and B are not defined in there.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46362
You mentioned the "default" package (which I think is officially called the unnamed package). At this stage, you can put all your work in the unnamed package, and forget about declaring package names until much later.
Doyle Davis

Joined: Feb 13, 2011
Posts: 4

Thanks to everyone who helped me understand this. I followed your suggestions and it works ! I now have a better understanding of the word "public" and why you can only have 1 public class per actual file.
I agree. Here's the link:
subject: using the javac -cp command
It's not a secret anymore!