Win a copy of Modern JavaScript for the Impatient this week in the Server-Side JavaScript and NodeJS forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

pathing - what is the reasoning behind this ?

 
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have myfile.java with a package statement (package X; ) . I put it in a folder X and compile it from X. Then, to run the code i have to go one folder above X and use the complete package name to run the class file. But if i run it from X , i get an error. Any explanation for this ?
Is this the reason ??? When we are inside X , jvm searches X and it finds a myfile.class with a package statement. So, it does not load this file because it expects it to be a part of another folder X (inside the current X) .

I made a folder X (call it X^) inside X and put myfile.class into X^ . Then from X >>> java X.myfile OR java -classpath . X.myfile WORKS . Just java myfile fails.

PS : One can memorize such things. But how much can one learn this way ? Reasoning will eliminate the need to memorize.





 
Ranch Hand
Posts: 394
Eclipse IDE Oracle Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rahul, you dont need to memorise, just 'burn-it-in' that:
When you declare a class in a package, invocation must take place in the 'immediate-super' directory.

Here is the logical reason:

-Lets say the name of your class is 'Rahul' and you declare it in a package called 'My'
-At this point java recognises your class as 'MyRahul' and NOT 'Rahul'.
I know your next question will be why??? The following may explain:

At work place in real life as a programmer:
If I name my class 'Rahul' and you name yours 'Rahul' in the file system and we
need to use your class, how do we tell java which 'Rahul' to run???.

I hope this helps.
 
Rahul Sudip Bose
Ranch Hand
Posts: 637
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Ikpefua Jacob-Obinyan wrote:
Rahul, you dont ..... to run???.



Your arguments sound logical to me I hope there is nothing more to this issue that we have overlooked.

good luck
rb


 
Ranch Hand
Posts: 300
Eclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Hi Rahul , Java class loading is based upon classpath and every class is identified by its package name + class name and that's the reason you can have two class with same name in two different package. you might find my post http://javarevisited.blogspot.com/2011/01/how-classpath-work-in-java.html";" target="_new" rel="nofollow"> How Classpath works in Java interesting
 
If you open the box, you will find Heisenberg strangling Shrodenger's cat. And waving this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic