Win a copy of Terraform in Action this week in the Cloud forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

compiling code with packages

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 74393
335
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is mistaken.
 
sachins patil
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 .
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic