简体   繁体   English

Java 11 jar命令是否支持classpath参数?

[英]Does the Java 11 jar command support the classpath parameter?

I am currently studying for the OCP Java 11 certification and I am currently playing around with the basic JDK commands.我目前正在学习 OCP Java 11 认证,目前正在使用基本的 JDK 命令。

In the study guide there's a review question mentioning that the jar command also supports the -cp option (the classpath).在学习指南中有一个评论问题提到jar命令还支持-cp选项(类路径)。 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.我知道-C选项,提到要归档的文件所在的路径。 Also, java and javac do accept -cp .此外, javajavac确实接受-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?如果classpath参数确实有效,-cp和-C有什么区别? I am a bit confused.我有点困惑。

Thanks.谢谢。

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).正如您所指出的,文档没有提到 class 路径,所以没有理由假设-cp指定它(撇开我什至不知道jar可以使用 ZA2F2ED4F8EBC2CBB14C21A29DC40 路径) Or does it?或者是吗? Or can other options be combined to -cp ?或者可以将其他选项组合到-cp吗? How can we be sure?我们如何确定? By looking at the code!通过查看代码!

There are two classes involved in parsing command line options: Main and GNUStyleCommandLineOptions .解析命令行选项涉及两个类: MainGNUStyleCommandLineOptions Let's start with the latter.让我们从后者开始。

In GNUStyleOptions the method parseOptions parses the arguments:GNUStyleOptions方法parseOptions解析 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 .请注意,此代码不包含将组合选项分开的功能,而getOption也不包含 - 它只是在预定义的可识别选项数组中查找字符串。 Since -p / --module-path is defined here, -c can't be combined with it to -cp .由于-p / --module-path在这里定义, -c不能与它结合到-cp

Just for fun, let's check Main .只是为了好玩,让我们检查一下Main There's parseArgs , which does understand combined flags and switches over each individual letter .parseArgs ,它确实理解组合标志并切换每个单独的字母 This is where you'll find -c and also a -P , so -cP might work.这是您可以找到-c-P的地方,因此-cP可能会起作用。 I'll leave this as an exercise to the reader.我将把这个作为练习留给读者。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM