wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes packages and classpath setting Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "packages and classpath setting " Watch "packages and classpath setting " New topic
Author

packages and classpath setting

hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
iam in f:

// saved under the name of sri.java


package packag.pack.sri.poi.kjdsf;
public class sri
{

public sri()
{
System.out.println("no");
}

}
then i typed f:\> javac -d . sri.java at the command line and obtained the folder under the directory structure of

f:\packag\pack\sri\poi\kjdsf>sri.class


//saved under the name of kanth.java

import packag.pack.sri.poi.kjdsf.sri;//fully qualified name

class kanth
{
kanth()
{
System.out.println("yes ");
}
public static void main(String args[])
{
kanth o = new kanth();
sri obj = new sri();

}

}




then i typed in f:\> javac kanth.java and java kanth.java and obtained
ouput as yes and no .

Q1) now i cutted the folder packag under f: which is f:\packag and pasted it in e: so it became e:\packag\pack\sri\poi\kjdsf\sri.class>

since i moved the directory structure in another drive e: so i did set the classpath as
i typed the command when iam in f: f:\> set classpath=%classpath%;e:\packag

then i tried to compile javac kanth.java.
here iam getting error !!! package does not exist


can ranchers help me out of this ..how to set the classpath ?
is there any different way to set the classpath ?
please suggest me basing on this example ..

Q2)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 we 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
Is this method right???

This is not working for me though i use cp option

can any one say how to use cp option for the above example
and how to set the classpath for the above example
------------------------------------

q3) saved under a.java
package p;
public class a
{
public static void main(String [] args )
{
System.out.println("asdf");
}
}
typed at the command prompt e:\> javac -d . a.java
then i got a.class under p folder as p/a.class
how to see the output when iam in e: itself

i typed like this e:cd p
then e:\p> java a
but i am unable to see the output ? what should be the command to see the output ?


q4)
I also went into f: and used cp option as
f:\ java -cp e:\p.a
IT IS NOT getting .iam unable to see the output ???

Q5) If i have a jar file in d: \jars . which consists of packages then how would i use that in different directory say e:
and access the package in d:\jars\package...how to set classpath for jars across drives and access the packages in that jar ?

what should be the commands for the above 5 questions ? dear ranchers what could be the solutions for these?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
If you use code, your question will look quite beautiful, do not you feel so, see now my answers.
For 1) question you need to set



Do not set e:\packag, as you have imported


So compiler will start finding packag.pack.sri.poi.kjdsf.sri; in e:\packag, that is not there.


SCJP 6
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Q2)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 we 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
Is this method right???

This is not working for me though i use cp option

can any one say how to use cp option for the above example
and how to set the classpath for the above example


I did according to you everything:


This is public class a in my D:\ inside package p;
D:\>javac -d . a.java //compiled with this command



And run like you ran:

F:\>java -cp D:\ p.a
hello I am a

F:\>set classpath=%classpath%;D:\

F:\>java p.a
hello I am a


You can see it is working correctly. Now ask your doubt.



Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
q3) saved under a.java
package p;
public class a
{
public static void main(String [] args )
{
System.out.println("asdf");
}
}
typed at the command prompt e:\> javac -d . a.java
then i got a.class under p folder as p/a.class
how to see the output when iam in e: itself

i typed like this e:cd p
then e:\p> java a
but i am unable to see the output ? what should be the command to see the output ?


I typed following command to run:


p is package name, so it is a part of class name now, you can not separate them, now they are deep extreme lovers, can not live separately.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
q4) I also went into f: and used cp option as
f:\ java -cp e:\p.a
IT IS NOT getting .iam unable to see the output ???


I used this one:


Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Q5) If i have a jar file in d: \jars . which consists of packages then how would i use that in different directory say e:
and access the package in d:\jars\package...how to set classpath for jars across drives and access the packages in that jar ?

what should be the commands for the above 5 questions ? dear ranchers what could be the solutions for these?


See what I did: I used your packag that you copied to e: drive in first example


E:\>jar -cvf hans.jar packag
added manifest
adding: packag/(in = 0) (out= 0)(stored 0%)
adding: packag/pack/(in = 0) (out= 0)(stored 0%)
adding: packag/pack/sri/(in = 0) (out= 0)(stored 0%)
adding: packag/pack/sri/poi/(in = 0) (out= 0)(stored 0%)
adding: packag/pack/sri/poi/kjdsf/(in = 0) (out= 0)(stored 0%)
adding: packag/pack/sri/poi/kjdsf/sri.class(in = 361) (out= 269)(deflated 25%)


It gave me:



I cut this and put it in D:\>
now it is:



F:\>set classpath=%classpath%;D:\hans.jar

F:\>javac kanth.java

F:\>java kanth
yes
no


or


F:\>javac -cp D:\hans.jar kanth.java

F:\>java -cp D:\hans.jar;. kanth
yes
no


note this line

F:\>java -cp D:\hans.jar;. kanth

D:\hans.jar;. the last dot here, it is to tell jvm to find kanth in current directory, if you did not tell, jvm will try to find kanth.class in hans.jar and tell you I cannot find kanth.class anywhere.

By default javac means compiler find .java files in current directory so you do not need to specify this "." when you compile.
like:

F:\>javac -cp D:\hans.jar kanth.java

no need to specify dot here in classpath.

By default JVM mean java command do not find .class files in current directory so you need to specify current directory when you run.


Any doubt now?
nav katoch
Ranch Hand

Joined: May 02, 2008
Posts: 246

Thanks for a wonderful explanation Punit, Naveen.
hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
Thanks punit.Thanksalot .I will let you know if i have any queries related to the current topic
hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
PUNIT ..excellent explanation ..every thing went well except the first answer of yours
After i did set the classpath as said by you .it did compile and i got my class file in f:
everything was ok but when i tried to run it as java kanth
it is saying exception in thread main java.lang.noclassdeffound error: kanth

but if i again type this in command line as set classpath=.;
and typed java kanth (once) i can see partial output now with an exception

and again set classpath as set classpath=%classpath%;e:\
and now javac kanth.java
successfully compiled
and now java kanth

I CAN SEE THE OUTPUT NOW ( HURRAH!) WHATS THE LOGIC behind it first it is not showing now its showing ( whats the logic???)

what should i do to see the output first itself??
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Punit Singh wrote:If you use code, your question will look quite beautiful, do not you feel so, see now my answers.
For 1) question you need to set



Do not set e:\packag, as you have imported


So compiler will start finding packag.pack.sri.poi.kjdsf.sri; in e:\packag, that is not there.



If you are talking about this one, than before setting these two lines:



There could be any different address in your classpath, you can see that by using this command:



And java kanth command was trying to find kanth.class file at that different address, so it was not able to find kanth.class.

If you can confirm what was that different address at that time before setting .;e:\, you can know that at that location there were no kanth.class.

You have to tell java command to find kanth.class is in current directory, otherwise it will not look in current directory.
By setting these two lines:


you are telling java to find kanth.class file in current directory.



hansika motwani
Ranch Hand

Joined: Jul 22, 2008
Posts: 44
now its working fine perfect..no probs .Thanks a lot..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14117
    
  16

Don't forget to change your name as discussed in your other topic.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: packages and classpath setting