views:

184

answers:

2

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 "."

+14  A: 

: 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.

mipadi
The colon `:` is by the way linux/unix specific. In Windows the semicolon `;` is used.
BalusC
":" is correct for *nix machines; it's ";" for Windows.
duffymo
Furthermore, it's good practice to get path and directory separators in a platform independent way: `String pathSeparator = System.getProperty("path.separator");` `String directorySeparator = System.getProperty("file.separator");`.
Asaph
@Asap Sure, but tell me how do you do that on the command line? :)
Pascal Thivent
@Asaph: Yes, but that won't work if you're invoking from the command-line. If you're invoking from the command-line, you kind of have to know what the proper separator is. :)
mipadi
+2  A: 

The accepted answer is correct but it could have mentioned that the classpath separator is actually platform dependent as pointed out in comments.

For more information, including an explanation of class path wildcards, and a detailed description on how to clean up the CLASSPATH environment variable, see the Setting the Class Path technical note (and/or Setting the Class Path for the *nix version).

Pascal Thivent