• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

using the javac -cp command

 
Greenhorn
Posts: 4
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13082
67
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There should also be spaces both before and after the dot...
 
Ranch Hand
Posts: 33
Mac Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 76866
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac OS X Netbeans IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
it's a teeny, tiny, wafer thin ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic