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.