• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Classpath problem when trying to access packages

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 781
Netbeans IDE Ubuntu Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You must save source code in a file with the extension java, not the extension class.
 
ashwathi rao
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic