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

doubt in classpath

 
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have the following directory structure


My Current Directory is DirA. when I give

c:\DirA>javac - cp DirB B.java

Compiler says error cant read B.java. But when I use -cp with java, i am getting the o/p of the B.java.

c:\DirA>java -cp DirB B

what is the problem here??

Thanks
Srividhya
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When you invoke javac, you need to provide a Java source file to be compiled. So from DirA, you can use...

javac DirB/B.java

It does not work when you try...

javac -cp DirB B.java

...because this is telling javac to compile B.java from the current directory (DirA), which fails because that's not where B.java is. The classpath flag only tells Java to find any additional classes it needs under DirB.

So from DirA, if you wanted to specify a classpath of DirB and compile B.java, you could use...

javac -cp DirB DirB/B.java

(PS: Are you sure you want to put your own subdirectories under a "bin" directory?)
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc can you explain more on this statement

c:/DirA>javac -cp DirB DirB/B.java

here does it mean it will look for class file under DirB, if so what are the class file it will look for. Then it will compile B.java which is under DirB.

C:/DirA>java -cp DirB/B why cant we give like this, even this means look for B class file under DirB directory right???

Sorry if I am disturbing you asking the same question.

Srividhya
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry if I was confusing. I was just trying to show the difference between specifying a classpath and providing a path to the file you want to compile. So if you needed to do both, it could be done like this...

But in your case, you probably don't need a classpath, so you can just use...

C:/DirA>javac DirB/B.java

As for the second part of your question, C:/DirA>java -cp DirB/B will not work because you are not telling Java what class to run. All you are doing is providing a classpath of "DirB/B", where "B" would be interpreted as another directory.
 
Srividhya Kiran
Ranch Hand
Posts: 166
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Marc for patiently clarifying my doubt I understood.
 
First, you drop a couch from the plane, THEN you surf it. Here, take this tiny ad with you:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic