• 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
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

CLASSPATHS; please critique my explanation for correct/incorrect answers

 
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey,

I think I understand why Sierra/Bates, Chapter 10, Question 3 is C, and the other options are incorrect. Will appreciate if you critique my understanding of this subject, please see below quote:

Given the default classpath:

/foo

And this directory structure:

foo / test / xcom / A.class
foo / test / xcom / B.java

And these two files:

package xcom;
public class A { }

package xcom;
public class B extends A { }

Which allows B.java to compile? (Choose all that apply.)

A. Set the current directory to xcom then invoke

javac B.java

B. Set the current directory to xcom then invoke

javac -classpath . B.java

C. Set the current directory to test then invoke

javac -classpath . xcom/B.java

D. Set the current directory to test then invoke

javac -classpath xcom B.java

E. Set the current directory to test then invoke

javac -classpath xcom:. B.java





A: B.java depends on A.class to compile. Since A.class is part of xcom package, you need to be one directory above xcom because compiler won't see A.class
B. Similar explanation for why option A is incorrect. Setting the classpath to current directory and xcom makes to difference because A.class is part of xcom package
C. This is CORRECT. Move up to test directory, set the classpath to current directory by typing in "." and then type the relative path of the program to compile, i.e, xcom/B.java
D. Even though you are in test directory and set classpath, it won't work because xcom is a relative path, but relative to what? There can be numerous directories named xcom throughout your filesystem
E. Ok, at least the syntax for setting classpath is correct, but it will first look at xcom, and then "." which is the current directory. This would be correct if you attempt to compile "xcom/B.java", because B is part of xcom, rather than B.java, because compiler won't understand what B.java is
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

D. Even though you are in test directory and set classpath, it won't work because xcom is a relative path, but relative to what? There can be numerous directories named xcom throughout your filesystem


Actually this is not the reason it doesn't compile. When you go into test directory and run
javac -classpath xcom B.java

It works in the same way as the commands in option A and B. Although you are in the test directory, you are setting the class path to /test/xcom, so it doesn't matter in which directory you are when you run this command. It will work same as if you are running the command javac B.java from xcom directory...
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
@Ankit:

I was looking over my explanation for why E is incorrect and STILL need some assistance.

Tried the following on my system:

D:\foo\test>javac -classpath xcom:. B.java

Received the following error, as I expected:

javac: file not found: B.java
Usage: javac <options> <source files>
use -help for a list of possible options



I would think that

D:\foo\test>javac -classpath xcom:. xcom/B.java

would be same as option C because you are setting classpath to current path and invoking xcom/B.java. Yet I get the following error:

xcom\B.java:3: cannot find symbol
symbol: class A
public class B extends A{}
^
1 error


Please explain why this does NOT work. Also explain why the reversing the order of directories does not work, i.e.


D:\foo\test>javac -classpath .:xcom xcom\B.java

Produces the error

xcom\B.java:3: cannot find symbol
symbol: class A
public class B extends A{}
^
1 error
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you are on windows, the path separator is ; not : , so try these (both these commands will work)

javac -classpath xcom;. xcom/B.java
javac -classpath .;xcom xcom\B.java
 
Sandra Bachan
Ranch Hand
Posts: 434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are awesome!!!
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
so what about like that? why cannot work?
javac -classpath xcom xcom\B.java
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"blablabla bloblo" please check your private messages for some administrative matter...
 
Enjoy the full beauty of the english language. Embedded in this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic