Search code examples
javaclasspathscjp

The use of ":" and "." in a call to java with classpath declaration


This is a scjp mock exam question.

Suppose I have the following two files:

package pkg;

public class Kit {
    public String glueIt (String a, String b) {return a+b;}
}

import pkg.*;

class UseKit {
    public static void main(String[]args) {
        String s = new Kit().glueIt(args[1],args[2]);
        System.out.println(s);
    }
}

And the following directory structure:

test
   |--UseKit.class
   |
   com
     |--KitJar.jar

The current directory is test and the file pkg/Kit.class is in KitJar.jar

According to the answer, the java invocation that produces the output b c is

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

Please explain the use of the operators ":" and "."


Solution

  • : is the separator for entries in a Java classpath. . means "current directory". So the classpath com/KitJar.jar:. means to look for Java class files in two locations: com/KitJar.jar and the current directory.