• 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

Finding class files using the -cp command

 
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 been writing a lot of code to learn about the different ways of finding the class files using -classpath command. Although the explanation given in the book might be just enough for the exam, I am still curious to completely understand the different ways javac and java command uses the classpath command to find the class files.

Suppose the directory structure is given as below:

A.java

and Sample.java is given as below

Given the current directory is com, the output when the program is run should be Inside class A. Can anybody explain as to how can I get the required output using the classpath command?
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are asking how to compile and run your code, you'll first need to place an import statement in Sample.java to tell the compiler that A class is in AA package. Then from com directory you can compile Simple.java with a normal javac command

javac Simple.java

javac searches in the current directory by default. So it will be able to locate the AA package...
 
Panindra Chekoori
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the program can use the import statement to compile and run the program but is it possible to do the same using -cp commmand without including the import statement ? I have tried compiling the program using the following command

javac -classpath . Sample.java

My interpretation of the above command is as follows: Since I am already in the current directory com, I can use . to ask the compiler to search the current directory for class files. In this case it would be AA/A.class. After executing the command,the compiler throws an error saying that class A cannot be found. The study guide explains that the last directory in the path must the super-directory of the root directory for the package. In the give program the super-directory is com and root directory is AA.
 
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I agree with whatever Ankit said.

Disclaimer : Based on what-ever knowledge I possess, I am giving this answer.

Note: 1) You have placed the class 'A' inside the package 'AA'. So you can't access the class 'A' in Simple.java directly.
Either you have to import it from the package 'AA' or use the fully qualified class name as AA.A

2) Since the class 'A' is in the package 'AA', the folder which consists the AA package should be in the class path, i.e. nothing but 'com' which is the current working directory as per your post.
So actually you don't need the classpath

3) If you want to access a class from a different package in your class, you need to import it or use fully qualified name. I think you can't get the class using 'Classpath'.
As you know Classpath just tells the compiler to look in the path for the classes

With this said, there are two ways to get the output "Inside class A"

Solution 1: just import the class A



com>javac Sample.java
com>java Sample
Inside Class A

Solution 2: Use the fully qualified class name AA.A



com>javac Sample.java
com>java Sample
Inside Class A


 
Maybe he went home and went to bed. And took this tiny ad with him:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic