Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages from KB

 
mohammed khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
not able to execute below 2 programs ?how to execute it .

package cert;
public class Beverage
{
}


package exam.stuff;
import cert.Beverage;
class Tea extends Beverage

{
public static void main(String argd[])
{
System.out.println("hello world");
}

}


I am able to compile above 2 programs but when i am executing i am geting exception in thread main. so how can i execute this Tea program
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check if the current directory is in your classpath. I think that's your problem.

More information over the classpath can be found here

Setting classpath
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in the parent directory of exam :
java -cp . exam.stuff.Tea
 
mohammed khan
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks it worked but for all programs regarding packages will it work
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You have to understand how java is looking for your classes. When you run your program java looks for the main class, into your CLASSPATH.

For example, if you have :
c:\java\exam\stuff\Tea.class
and you want to run it from d:\, you have to tell java that the top package is in c:\java. There are different ways to do it, one of which being to pass the classpath as a command line argument :
d:\java -cp c:\java exam.stuff.Tea
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic