• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Classpath and java command

 
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi all,
If i have a directory set as default classpath,and in that directory i have a class file,and a jar file.The class file uses the classes of jar file to compile and run.
Then while compiling and running by commands javac and java respectively,should i include the classpath for the jar file?
Actually i was confused regarding a question in k&B book----
The Question is:


11. Given the following directory structure:

test-|
|- GetJar.java
|
|- myApp-|
|-Foo.java
And given the contents of GetJar.java and Foo.java:
3. public class GetJar {
4. public static void main(String[] args) {
5. System.out.println(myApp.Foo.d);
6. }
7. }
3. package myApp;
4. public class Foo { public static int d = 8; }

If the current directory is "test", and myApp/Foo.class is placed in a JAR file called MyJar.jar
located in test, which set(s) of commands will compile GetJar.java and produce the output 8?
(Choose all that apply.)
A. javac -classpath MyJar.jar GetJar.java
java GetJar
B. javac MyJar.jar GetJar.java
java GetJar
C. javac -classpath MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar
D. javac MyJar.jar GetJar.java
java -classpath MyJar.jar GetJar

Now,as the correct answer is A,my question is ,why are we including the classpath for .jar while compiling and not while running,if we need to include files from .jar while compiling?
and why is option C not correct if i include the classpath while running?

GREAT CONFUSION.Please help me out

THANKS in advance....
 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Incredible, we made the same question at the same time...and have the same doubts!
 
Sudhanshu Mishra
Ranch Hand
Posts: 238
1
Eclipse IDE Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
Please have a look at my question.
I need to understand it urgently.
Eagerly waiting for the answer....
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Sudhanshu Mishra wrote:Hi all,
Please have a look at my question.
I need to understand it urgently.
Eagerly waiting for the answer....




Well, I had some issues with compiling programs at work that required imports of a jar file so I'm assuming its along this reasoning...some classes within the jar file are needed for compilation if used within the class file you are trying to compile. Say for instance you are trying to use a database such as db4o and you are trying to create an instance for a DAO. You will need to import something...I believe the jar would be needed to get this important information. If you dont add those jars you would get some sort of Symbol Not Found during compilation, and you wouldnt be able to build.

This could be wrong...but thats my guess
 
Drove my Chevy to the levee but the levee was dry. A wrung this tiny ad and it was still dry.
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic