• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

using the javac -cp command

 
Doyle Davis
Greenhorn
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12186
34
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There should also be spaces both before and after the dot...
 
Ernie Mcracken
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15441
41
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49813
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
Java Mac OS X Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic