*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes package and classpath 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 "package and classpath" Watch "package and classpath" New topic
Author

package and classpath

Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
suppose there is a file

package com.javaranch.www
class Scjp
{
}

we know that we can place this file anywhere on the system...and if we compile if using javac Scjp it wont create a directory structure com/javaranch.www

what i want to ask is whenever there is a use of package in the exam...will the java file be placed in the directory strcutre as given by the package? or will the class file be always placed in some directory with the same directory as given by the package name?

i hope am clear what i want to ask


scjp 1.6 91%, preparing for scmad
"Time to get MAD now.. we will get even later"....by someone unknown
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

I don't think it will matter in any question where the .java file is placed. Most of the questions start from line no 5 or 10, so actually you won't see any package or import statement in many questions. And I don't think there would be any question whose behavior will depend on where a .java or .class file is placed. If there are any question from objective 7 (10th chapter of K&B book i.e. Development), then you would be told where the .class file is placed...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
but when the class file is put in suppose a directory raju...should i assume that the class file will be stored in directory structure according to the package? because we can compile the file from the current directory and still put the class file there itself without asking the compiler to create the directory structure for us
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18709
    
  40

Class files need to be placed in directories based on how they are packaged. And the classpath specifies the roots, and not the exact directory where the class files are located. The location is determined by the root plus the package definition. If you don't do this correctly, things may not work.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ok......so when i see a file with a package structure i should assume that the class file is put in the same directory structure as the package name.......
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

@ Raju

See the following



after compilation Test.class file will be created and it is known by its binary name.
It's binary name always be com.name.Test so class file should be in the directory structure as com/name/Test.class.


SCJP 5.0 - JavaRanch FAQ - Java Beginners FAQ - SCJP FAQ - SCJP Mock Tests - Tutorial - JavaSE7 - JavaEE6 -Generics FAQ - JLS - JVM Spec - Java FAQs - Smart Questions
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521


this file compiles so a class file is created but it doesn't run if i invoke it using java A
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

@ Raju

You should invoke it using java nokia.A not only A (because its binary name is nokia.A) and your classpath is set to direcoty which contains nokia directory that is root of your package as Henry already explained to you.
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
it isnt running from the current directory
Ninad Kulkarni
Ranch Hand

Joined: Aug 31, 2007
Posts: 787

@ Raju
Do you have nokia directory in your current directory having A.class file in nokia directory? and Do you set your classpath to current directory ? current directory in classpath is indicated by '.'
Raju Champaklal
Ranch Hand

Joined: Dec 10, 2009
Posts: 521
ok got it...thanks a lot Ninad....
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: package and classpath