• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

package and classpath

 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21190
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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.
 
Raju Champaklal
Ranch Hand
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it isnt running from the current directory
 
Ninad Kulkarni
Ranch Hand
Posts: 802
Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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
Posts: 521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok got it...thanks a lot Ninad....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic