3

I am currently studying for the OCP Java 11 certification and I am currently playing around with the basic JDK commands.

In the study guide there's a review question mentioning that the jar command also supports the -cp option (the classpath). Is this true? I am not aware of such thing, neither did I find the information in the official docs.

I know about the -C option, mentioning the path where the files to archive are located. Also, java and javac do accept -cp.

I am starting to believe it is an error in the study guide, but I wanted to double check first.

Is this valid?

jar -cf newJar.jar -cp /sample/dir .

This surely is:

jar -cf newJar.jar -C /sample/dir .

If the classpath parameter is indeed valid, what's the difference between -cp and -C? I am a bit confused.

Thanks.

Adrian Pop
  • 215
  • 1
  • 3
  • 12
  • Did you run it and try yourself? – user May 10 '20 at 19:53
  • 1
    Yes. I have JDK 8 installed on the laptop I am using right now and my personal laptop is not within reach thus the question. This is the output for JDK 8: jar -cvf newJar.jar -classpath dir/ . -classpath : no such file or directory – Adrian Pop May 10 '20 at 20:00
  • There may well be a typo in your study guide, or you have misread it (worth double checking). There is a `-p` option - but that is short for `--module-path` (and `jar --help` says *-p, --module-path Location of module dependence for generating the hash*). So, `-cp` would be `-c` and `-p` for create an archive specifying a module path. – Elliott Frisch May 10 '20 at 20:16
  • Nope, I didn't go into modules yet. Or (better said) my intent wasn't to use a module path. So I guess that's the explanation then. I must mention that I get the same output when using -classpath: `jar -cvf newJar.jar -classpath dir/ . -classpath : no such file or directory` – Adrian Pop May 10 '20 at 20:28
  • 1
    What would be the expected effect of specifying a class path to the `jar` command? – Holger May 12 '20 at 10:31
  • @Holger I first assumed it is used to specify the location of the classes to be archived, but there's `-C` used for exactly that. – Adrian Pop May 12 '20 at 16:38

1 Answers1

1

As you point out, the docs don't mention the class path, so there's no reason to assume that -cp specifies it (leaving aside that I don't even know what jar could use the class path for). Or does it? Or can other options be combined to -cp? How can we be sure? By looking at the code!

There are two classes involved in parsing command line options: Main and GNUStyleCommandLineOptions. Let's start with the latter.

In GNUStyleOptions the method parseOptions parses the arguments:

static int parseOptions(Main jartool, String[] args) throws BadArgs {
    int count = 0;

    // [...]

    // [ iterate over each argument ]
    for (; count < args.length; count++) {
        // [...]
        // [ get the option's name ]
        String name = args[count];
        // [...]
        // [ look up the option ]
        Option option = getOption(name);
        // [...]
    }

    return count;
}

Note that this code contains no functionality to tear combined options apart and getOption doesn't either - it just looks up strings in a predefined array of recognized options. Since -p/--module-path is defined here, -c can't be combined with it to -cp.

Just for fun, let's check Main. There's parseArgs, which does understand combined flags and switches over each individual letter. This is where you'll find -c and also a -P, so -cP might work. I'll leave this as an exercise to the reader.

Nicolai Parlog
  • 47,972
  • 24
  • 125
  • 255