File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes -classpath questions - confused Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "-classpath questions - confused" Watch "-classpath questions - confused" New topic
Author

-classpath questions - confused

Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
I don't know why I am getting so confused about this, but help would be appreciated. I have the following example:

Given 2 files:



And the following directory stucture:



If the current directory is test and the file pkg/Kit.class is in KitJar.jar, which command line will produce the output: bc? (choose all that apply)


a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jat:. UseKit a b c



Can someone please explain to me carefully which answers above are correct/incorrect, and exactly why? My brain is just freezing on this particular topic!!!


K. Tsang
Bartender

Joined: Sep 13, 2007
Posts: 2409
    
    7

Have you try our the program/commands? Running each answer option should give you better understanding what the program is doing.


K. Tsang JavaRanch SCJP5 SCJD/OCM-JD OCPJP7 OCPWCD5
Rachel Glenn
Ranch Hand

Joined: Oct 24, 2012
Posts: 95
K. Tsang wrote:Have you try our the program/commands? Running each answer option should give you better understanding what the program is doing.


I am working in a windows environment, not UNIX, so I don't think I can try them unfortunately.
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
Consider this format:
java -classpath dir filename

dir is the directory where the supporting classes are.
filename is the class file you will execute.

a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jar:. UseKit a b c

First of all, UseKit is in your current directory, test. So, it is ok to specify filename as UseKit.
It needs pkg.Kit or KitJar.jar to support UseKit.

A) By default, the current directory is test and pkg.Kit is in test directory. It works.
B) The classpath is com and current directory, test. In test directory, there is a pkg.Kit class. So it works.
C) The classpath is com/KitJar.jar, inside which the pkg.Kit resides. It works.
D) The classpath is com/KitJar.jar and current directory. It works in the same way as B) and C).
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
Himai Minh wrote:Consider this format:
java -classpath dir filename

dir is the directory where the supporting classes are.
filename is the class file you will execute.

a. java UseKit abc

b. java -classpath com:. UseKit a b c

c. java -classpath com/KitJar.jar UseKit a b c

d. java -classpath com/KitJar.jar:. UseKit a b c

First of all, UseKit is in your current directory, test. So, it is ok to specify filename as UseKit.
It needs pkg.Kit or KitJar.jar to support UseKit.

A) By default, the current directory is test and pkg.Kit is in test directory. It works.
B) The classpath is com and current directory, test. In test directory, there is a pkg.Kit class. So it works.
C) The classpath is com/KitJar.jar, inside which the pkg.Kit resides. It works.
D) The classpath is com/KitJar.jar and current directory. It works in the same way as B) and C).


Finding a Jar file is a little different form find a class file, you must include the name of the Jar file in the end of the path.
In addition, if the classpath is specified as com/KitJar.jar, you have to add the current directory "." back.
Alexandre Leveille
Greenhorn

Joined: Jan 26, 2013
Posts: 12

First: you should be able to do this on Windows except that you will use the character ";" instead of ":" to delimit classpath entries.

Also I would give you one tip: make two versions of the Kit class. For instance, in the jar, package the class with a return "jar " + a + b and in the class file use a return "class " + a + b. To do so, create and compile the first version of the class, then create the jar file with this .class file. When you have your jar file, modify the .java file so it returns something else.

Next, on with the classpath basics:
. means current directory. If your current directory is test, and you look for UseKit.class, then the . will allow you to look in the current directory and find UseKit.class
: is used to delimit classpath entries on Unix & al. Use ; on Windows. For instance, use ".;com;tst" to look into the current directory and inside com and tst.
Also have a look at wikipedia.
When specifying a directory, class files inside that directory are used, but jar files won't. Jar files must be explicitly specified ("named").


Now for your questions:

a. java UseKit abc
Will cause a runtime exception, args[1] and args[2] are out of bound.


b. java -classpath com:. UseKit a b c
Will try to find te required classes inside the com directory first and then will try to find them in current directory "."

Heres, what the JVM will do:
1- The JVM looks for UseKit.class in the com directory - no such file
2- The JVM looks for UseKit.class in the current directory - class found
3- UseKit.class imports pkg.UseKit.
4- The JVM looks for pkg/Kit.class in the com directory - the subdirectory pkg does not exists.
5- The JVM looks for pkg/Kit.class in the current directory - the subdirectory pkg does exists and contains Kit.class - class found

The JVM now have everything it needs : ./UseKit.class and ./pkg/Kit.class and will give you the desired output.


c. java -classpath com/KitJar.jar UseKit a b c
Will try to find to required classes inside the Kit.jar file in the com directory.

Heres, what the JVM will do:
1- The JVM looks for UseKit.class in the KitJar.jar file - no such file
2- No more entry in the classpath, the JVM throws a java.lang.NoClassDefFoundError: UseKit


d. java -classpath com/KitJar.jar:. UseKit a b c
Will try to find to required classes inside the Kit.jar file in the com directory and then will try to find them in current directory "."

1- The JVM looks for UseKit.class in the KitJar.jar file - no such file
2- The JVM looks for UseKit.class in the current directory - class found
3- UseKit.class imports pkg.UseKit.
4- The JVM looks for pkg/Kit.class in the KitJar.tar file - in the KitJar.jar the subdirectory pkg does exists and contains Kit.class - class found

The JVM now have everything it needs : ./UseKit.class and ./com/KitJar.jar/pkg/Kit.class and will give you the desired output



Here's what you need to remember:
- The JVM will try to find its classes in each of the directory and jar file specified in the class *in the order they are specified in the classpath*
- The jar files need to be explicitly named in the classpath. If you want to use A.jar and B.jar, you MUST see them in the classpath.
- packages are "translated" to directory. The class com.java.Example must be inside "com/java". Your classpath must contains the folder *above* the com directory. The logic is simple: by positioning itself in the directory "above" com, the JVM will find the com folder, then the java folder, then the Example.class file.
- When no classpath is specified, the default classpath is used. This default classpath is in the environment variable $CLASSPATH on Unix and %CLASSPATH% on Windows.

Hope this helps.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: -classpath questions - confused