• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling code with packages  RSS feed

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have Created the two fi les:
■ C:\temp\packagea\ClassA.java
■ C:\temp\packageb\ClassB.java
-------------------------------------------------

------------------------------------

---------------------------------
Then type this command:
cd C:\temp
and then I compile code using
javac packagea/ClassA.java packageb/ClassB.java
but when i am trying to execute using following command
java packagea.ClassA
it gives error like COULD NOT FIND OR LOAD MAIN CLASS.
My question is command to execute which is mentioned above is correct or why it gives such error ?can anybody explain?
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java, you must declare a public class to compile and run a program. To start the main class that you have, you must declare it public because the compiler requires it. It is a fundamental rule to follow.
 
Sheriff
Posts: 4294
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you don't have a package statement, then you can't use the syntax packagea.ClassA syntax. So add this to ClassA:

 
sachins patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
we can declare class as default it executes fine.
 
sachins patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:If you don't have a package statement, then you can't use the syntax packagea.ClassA syntax. So add this to ClassA:



We can do that.
Explained in book
Oracle certified associate java se8 programmer study guide by jeanne boya
page no-14
 
sachins patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alberto Tejos wrote: . . .
In Java, you must declare a public class to compile and run a program. To start the main class that you have, you must declare it public because the compiler requires it. It is a fundamental rule to follow.



Default for class is also fine .



 
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sachins patil wrote:
Knute Snortum wrote:If you don't have a package statement, then you can't use the syntax packagea.ClassA syntax. So add this to ClassA:



We can do that.
Explained in book
Oracle certified associate java se8 programmer study guide by jeanne boya
page no-14


No, you cannot do that. If you do not declare the class to be part of a package, it is not part of a package. If you look carefully at page 14 you will find the package-declaration.
 
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch

Please in future use code tags; I'll add them just this once, and add indentation and you can see how much better it looks
 
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just can't understand what Alberto meant?(whether if he will care to elaborate more)

The problem I can see with your code is :
You are not having package statement. Include it,first
 
Campbell Ritchie
Marshal
Posts: 56610
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is mistaken.
 
sachins patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:If you don't have a package statement, then you can't use the syntax packagea.ClassA syntax. So add this to ClassA:


Thanks .Package Statement was missing .
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!