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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "JAR file " Watch "JAR file " New topic
Author

JAR file

Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Source: Devaka Cooray's Exam Simulator.


Here the question is In order to compile A.java, what should be the class path?

This line states that System.out.println(new InsideJar.Info().access()); class A needs Info.class which is inside InsideJar.

So the classpath should be java -cp .: Pack.InsideJar A

But here the answer is java -cp .: Pack.jar A.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
So the classpath should be java -cp .: Pack.InsideJar A


Which type of classpath is this?
You have to give jar file name in your classpath, that is Pack.jar only, so given answer is correct, class A will find all needed classes from Pack.jar. No need to name dependent files explicitly.


SCJP 6
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


Now If I have to compile A.java which uses methods and variables in B.class, then what should be my classpath?


javac -cp classes/source classes/source/A.java

This is the correct answer. But dont we have to specify dir where B.class is located?

javac -cp classes/source/dir classes/source/A.java
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

javac -cp classes/source/dir classes/source/A.java
this is correct


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
what is dir here?
It is just a directory or package of class B, answer will depend on this fact.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509


Source: Devaka Cooray's Simulator.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Then you donot need to add dirc in classpath, only specify address of the directory that contains dirc folder.



here dirc is in /classes/source, so you need to give:


And another clue for you that see the code of A.java:
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
What if the code of A.java did not have the import statement???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:What if the code of A.java did not have the import statement???


Than it will try to find class B in your classpath and will not compile, as there is no such class, as it is actually dirc.B.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
That means A.java knows where B.class resides as it says import dirc.B, so the only the path leading to dirc needs to be specified in the classpath, Am I right, Punit???
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:That means A.java knows where B.class resides as it says import dirc.B, so the only the path leading to dirc needs to be specified in the classpath, Am I right, Punit???


Yes you are right Abhi. Abhi what are you doing, are you making practicals and coming to conclusion or just memorizing what we say to you. If you memorizing than you will forget many things in 1-2 weeks. You should write every practical, you find in your mock exams.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
No, Punit, I am not memorizing. I generally try out all the problems given in Mocks, in textpad. But didnt try out the above example, as I thought I should be able to answer it without trying it out. Stupid naa!!!
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:No, Punit, I am not memorizing. I generally try out all the problems given in Mocks, in textpad. But didnt try out the above example, as I thought I should be able to answer it without trying it out. Stupid naa!!!


I am saying you, these type of questions are most confusing questions, you must have seen those post where I, Ruben and Sachine went very long in solving a confusion and that question was raised by you, from that question Ruben has opened a new thread for again classpath confusion. Again you came back with classpath question, so come on read all recent threads of classpath and run all programs, what is going in your mind, we are running your program and you are not running your program yourself.

See your doubt here
Another doubt related to your question only
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Ok.Punit. Will never repeat.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Ok.Punit. Will never repeat.


I am not saying to not repeat, I just want to say you make all practicals and be clear on every concept, if you do not run these programs, then you are missing an important part of your java.

If you feel boring programs on notepad, then use editor, like eclipse, netbeans, but run command line programs from command lines only. Or you can run all your program from command line, just for writing purpose use editor, as it will save your time.
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
Punit, what i meant when i said I wont repeat is that I will always try out the program .
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
Abhi vijay wrote:Punit, what i meant when i said I wont repeat is that I will always try out the program .


For my case I never used notepad, my all programs are written using Eclipse, and run through eclipse only, I ran only few programs that were requiring command line from the command line. It's your personnel choice.
 
Don't get me started about those stupid light bulbs.
 
subject: JAR file
 
Similar Threads
Javac error
K&B chapter 10 Q5 classpath question
Why javac does not find this Java file?
problem in constructor
Compile Errror-cannot access RuntimeException