aspose file tools*
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
Author

using the javac -cp command

Doyle Davis
Greenhorn

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 C.java and compile it using the javac C.java 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 A.java file, B.java , and C.java 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 C.java , I got the message:

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

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 A.java 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 . C.java

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
Bartender

Joined: Oct 02, 2003
Posts: 11313
    
  16

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

C.java:7: class B is public, should be declared in a file named B.java


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

To compile do javac C.java

C.java 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: 14149
    
  18

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

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


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38818
    
  23
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
Greenhorn

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: http://aspose.com/file-tools
 
subject: using the javac -cp command