Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

JAR file

 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
javac -cp classes/source/dir classes/source/A.java
this is correct
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is dir here?
It is just a directory or package of class B, answer will depend on this fact.
 
Abhi vijay
Ranch Hand
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Source: Devaka Cooray's Simulator.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What if the code of A.java did not have the import statement???
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok.Punit. Will never repeat.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 509
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Punit, what i meant when i said I wont repeat is that I will always try out the program .
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic