This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes development (package and directories) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "development (package and directories)" Watch "development (package and directories)" New topic
Author

development (package and directories)

Adolfo Eloy
Ranch Hand

Joined: Mar 21, 2009
Posts: 146

Hello all.

I'm confused about the relation between packages and directory structures.

considering the following directory structure:

/home/adolfo/myProject/source/com/wickedlysmart/MyClass.java
/home/adolfo/myProject/classes/com/wickedlysmart/MyClass.java

And the MyClass source code as follows:



when I'm into the /home/adolfo/myProject and I run as follows:
java -cp classes com.wickedlysmart.MyClass

I get the output "I see dead threads" normally.

So, regarding this scenario, if I'm into the /home/adolfo/myProject/classes/com/wickedlysmart
Why I can't execute my class with the following commands:

java com.wickedlysmart.MyClass

or

java MyClass

Thanks.


Adolfo Eloy
Software Developer
OCPJP 6
Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3984
    
  18

Where should Java start looking for your class?

.../classes/com/wickedlysmart/com/wickedlysmart/MyClass.class ?

And what if you have a class named MyClass.java in the default package, located in .../myProject/classes ? If you run java MyClass, should it run MyClass in the current directory, or the one located in the default package?


The mind is a strange and wonderful thing. I'm not sure that it will ever be able to figure itself out, everything else, maybe. From the atom to the universe, everything, except itself.
Javin Paul
Ranch Hand

Joined: Oct 15, 2010
Posts: 294


Hi Adolfo Eloy,

Java start looking for user defined classes from environment variable "CLASSPATH" until you specifiy -cp or -classpath option while running java , which overrides
default classpath and is a preferred way for setting classpath for individual application.

default value for CLASSPATH in java is current directory "."

so if your classpath is set to current directory "." and you give command "java com.wickedlysmart.MyClass" , it will look for MyClass in directory ./com/wickedlysmart/ and if it finds the class it will execute .

but if you give name only "MyClass" than it would be incorrect name for that class , so it won't run.

but it all depends upon what is value or CLASSPATH in your environment .



SQL Query Interview Question
 
Have you checked out Aspose?
 
subject: development (package and directories)
 
It's not a secret anymore!