wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Classpath problem when trying to access packages Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Classpath problem when trying to access packages" Watch "Classpath problem when trying to access packages" New topic
Author

Classpath problem when trying to access packages

ashwathi rao
Greenhorn

Joined: Mar 26, 2009
Posts: 12
I have the following structure...I tried to execute the HelloWorldApp.class file in exam directory by overiding the classpath but always I keep getting the beverage helloworldapp output nomatter what path I set.I tried a lot.It give an error saying class not found.If I take out the import statement from Study it executes the exam helloworldapp but either ways I found the classpath is not being overidden but it just identifies the "." for the current directory.It is not accessing the file I want it to like in SCJP6 by K&B Chapter 10 Developement Test Question 9. Also I gave the java command from the c:\swathi\practice\classes it did not work. According to book it actually should please help me out .

I really appreciate your time and help.Thanks in advance to all of you on the ranch.

c:\swathi\practice\classes\exam\Study.class

package exam;
import exam.beverage.HelloWorldApp;
public class Study
{
public static void main(String args[])
{
System.out.println("exam/Study");
new HelloWorldApp();

}
}

---------------------------------------------------------
c:\swathi\practice\classes\exam\beverage\HelloWorldApp.class

package exam.beverage;
public class HelloWorldApp {

public HelloWorldApp()
{
System.out.println("Beverage Constructor Hello World!");
}


public static void main(String[] args)
{
System.out.println("Hello World!"); // Display the string.

new HelloWorldApp();
// h1.HelloWorld();
}


}
------------------------------------------
c:\swathi\practice\classes\exam\HelloWorldApp.class




package exam;
public class HelloWorldApp {

public HelloWorldApp()
{
System.out.println("Exam Constructor Hello World!");
}


public static void main(String[] args)
{
System.out.println("Hello World!"); // Display the string.

new HelloWorldApp();
// h1.HelloWorld();
}


}
-------------------------------------------------------------

ashwathi
James Sabre
Ranch Hand

Joined: Sep 07, 2004
Posts: 781

Assuming that your class is name Study and that it is in package exam and that the class is compiled to
c:\swathi\practice\classes\exam\Study.class then you need


Note the class to execute includes it's package and not the classpath (-cp argument) indicates where the start of the package hierarchy.


Retired horse trader.
 Note: double-underline links may be advertisements automatically added by this site and are probably not endorsed by me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

You must save source code in a file with the extension java, not the extension class.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
ashwathi rao
Greenhorn

Joined: Mar 26, 2009
Posts: 12
This is my diorectory structure:
I have created










I had used c:\swathi\practice\source javac -d ../classes exam.Study.java to create the class files under c:\swathi\practice\classes\ exam with same directory structure above follwoing the package structure



Now as per norms we should be running the files from classes so I used c:\swathi\practice\classes java exam.Study

So this started using the HelloWorldsApp file in beverage since its importing it from beverage. But I want the Study.java to use the HelloWorldApp file in the exam package. I tried overriding it

c:\swathi\practice\classes> java -classpath c:\swathi\practice\classes exam.Study

c:\swathi\practice> java -classpath c:\swathi\practice\classes exam.Study

either ways its executing the beverage.HelloWorldApp file.

1)Is the import statement problematic or what is the issue?

2)If I delete the import statemnt Study is using exam/HelloWorldApp file am unable to force it to use beverage.HelloWorldApp file.

3)Am I executing the command from wrong directory I mean It should work from classes package right.Once we define a package for class files like package classes we can call jvm from classes package right or should we alsways use source package even while running them. I mean once class files are created from where are we usually supposed to call/run the class file source or classes package..


I really appreciate all your time and help.I have also added the same directory structure a source and classes attachments....please review them if the above picture is not clear.


Thank you all ranch.....for sparing your precious time for me.


ashwathi
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Classpath problem when trying to access packages
 
Similar Threads
Problem with JDK at command prompt
Problem setting classpath
Need Help getting Started
Exception in thread "main" java.lang. NoClassDefFoundError: HelloWorldApp
Noob needs help with Hello World