Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

packages doubt

 
hansika motwani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) if i have something like this
package p ;

class a
{

}

package p;

class b
{

}
if i save it as a.java and
if i compile as javac -d . a .java then iam getting 2 class files under p .if i compile as javac -d . b.java then iam getting only one
what s the reason behind it ..is it sufficient to compile only 1 to obtain the rest ...


2) if there is a package in d:
and i want to access it from f:
then can any one say the command to set the classpath
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I think in both the cases you will get only 1 class file. Have you used class B in class A??

2) do you want to set the environment variable classpath or want to set it only while using the javac and java command.

If you want to set the environment variable, then use

set classpath=<path>

on windows and

export classpath=<path>

on linux(I am not sure about linux as I don't use it). If you want to set the classpath with the javac and java command use the -classpath or -cp option with these command but remember to use a .(dot) in the path when using the java command...
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think he asked the question wrong. If I understood correctly, he wants to access the contents of a package from another package. This is done with imports.
 
Ankit Garg
Sheriff
Posts: 9527
32
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ivan Vrtacnik:
I think he asked the question wrong. If I understood correctly, he wants to access the contents of a package from another package. This is done with imports.


Whatever the question was supposed to be, Hansika is an Indian Female name . So she asked the question wrong .
 
Ivan Vrtacnik
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I readily admit to knowing less about Indian personal names than you ,
so my apollogies to Hansika!
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. Firstly, i think compiling a.java using javac -d . a.java would give only one class file in directory "p".

Reconfirm your results after doing the following steps:
Delete all the contents in your current directory from where you are executing the javac command and put a.java in the current directory. Then run the javac -d . a.java command again.

2. Secondly, if you have the package in D: drive as "d:\p\a.class"
where "p" is the package name. Then from command line you can execute the a.class using the fully qualified name of the class which is "p.a"

F:\>java -cp D:\ p.a

or you can set D:\ in the classpath environment variable and say

F:\>java p.a

Note: the -cp option used in the command line overrides the settings of the classpath environment variable.
 
hansika motwani
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
iam not convinced by the answers..please help me out


actually if we have two classes like this
class a
{
}

class b
{
}
if i save this whole program as a.java or b.java (public class) and compile it
then iam getting two class files a.class and b.class..why so .when i compile it how am i getting two class files ? what s the reason behind it ..

does that mean if a program contain n classes and i compile any of them ill get the class files of the rest also..
The same is true reagarding packages too
if i say package pack;above all then all the .class files are stored under that package..when iam not compiling them ...how am i getting ..whats the reason?

2) 2. Secondly, if you have the package in D: drive as "d:\p\a.class"
where "p" is the package name. Then from command line you can execute the a.class using the fully qualified name of the class which is "p.a"

F:\>java -cp D:\ p.a

or you can set D:\ in the classpath environment variable and say

F:\>java p.a

Note: the -cp option used in the command line overrides the settings of the classpath environment variable.


iam not getting this ..is this right..
 
Harvinder Thakur
Ranch Hand
Posts: 231
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was assuming that you were using *two* separate class files to store class *a* and class *b* i.e. a.java and b.java respectively. The reason being looking at the following code posted earlier there were two package statements.
And it is not possible to have more than one package statement in the same file. Hence my assumption that you were referring to two separate source code files.


Originally posted by Hansika Motwani:
2) 2. Secondly, if you have the package in D: drive as "d:\p\a.class"
where "p" is the package name. Then from command line you can execute the a.class using the fully qualified name of the class which is "p.a"

F:\>java -cp D:\ p.a

or you can set D:\ in the classpath environment variable and say

F:\>java p.a

Note: the -cp option used in the command line overrides the settings of the classpath environment variable.

iam not getting this ..is this right..


The above statement without relating to your actual question (which is different from what i earlier interpreted)is perfectly fine. Please specify where you are doubtful.

Now coming to your actual question:



Yes if the above two classes are in the same source file then the compiler will generate two class files.



The above source code if saved in a.java and compiled as javac a.java will give three class files including *a$inner.class*. If you declare an enum even that will be compiled into a separate class file.

So you don't have to explicitly compile class *b* included in a source file saved by the name a.java as you don't have any means of doing that. Compiler does that for you and compiles class *b* as a separate binary.

Hope it makes sense to you...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic