• Post Reply Bookmark Topic Watch Topic
  • New Topic

classpath questions  RSS feed

 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone.classpath problems seem vry difficult. someone please
explain each of the options and justify the answers.
Also explain in detail why other than D and F are not correct?
This question is taken from practice exams k&B
Given that the current directory is bigApp, and the following directory structure:
bigApp
|-- classes
|-- com
|-- wickedlysmart
|-- BigAppMain.class

And the code :

package com.wickedlysmart;
public class BigAppMain {
public static void main(String[] args) {
System.out.println("big app");
}
}
Which will invoke BigAppMain? (Choose all that apply.)
A. java classes/com.wickedlysmart.BigAppMain
B. java classes com/wickedlysmart/BigAppMain
C. java classes.com.wickedlysmart.BigAppMain //even if I remove package statement I get NoclassDefFounderror.why?
D. java -cp classes com.wickedlysmart.BigAppMain
E. java -cp /classes com.wickedlysmart.BigAppMain
F. java -cp .:classes com.wickedlysmart.BigAppMain
G. java -cp classes/com/wickedlysmart com.wickedlysmart.BigAppMain


thnks in advance
Answers: D and F
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16059
88
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A is wrong because you're mixing a directory name (classes) with a class name (com.wickedlysmart.BigAppMain).

B is wrong because package names must be separated by dots, not slashes (or, you do not specify a file path, but a fully-qualified class name).

C is wrong because "classes" is not a package. Note that BigAppMain is in the package com.wickedlysmart - not classes.com.wickedlysmart.

D is correct: you put the "classes" directory in the classpath, and then tell Java to look for the class com.wickedlysmart.BigAppMain from there.

E is wrong, because "classes" is not a subdirectory of the root directory on your file system.

F is correct: you put the current directory "." and the directory "classes" in the classpath.

G is wrong because you must put the base directory "classes" in the classpath, not the complete directory "classes/com/wickedlysmart".

These questions assume you're using a Unix-like operating system, since / and : are used as the directory and classpath separator characters. On Windows you'd have to use \ and ; instead.

This is not really difficult if you understand the rules:

- The java command expects a fully-qualified class name, not a file path or filename.
- The base directory of the package structure must be in your classpath.
 
Sawan Mishra
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thnks Jesper
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!