首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >理解jar命令行

理解jar命令行
EN

Stack Overflow用户
提问于 2020-02-11 22:17:52
回答 1查看 52关注 0票数 0

下面是将要执行的一些java命令:

java -jar myJARfile.jar -huff-c input_file output_file

爪哇-jar myJARfile.jar -huff-d input_file output_file

爪哇-jar myJARfile.jar -lzw-c input_file output_file

爪哇-jar myJARfile.jar -lzw-d input_file output_file

我想知道如何访问每个参数-huff|lzw、-d|c和我主板上的文件,这样我就可以执行相应的代码了。他们都被认为是args吗?我会使用args、args1、args2、args3来访问它们吗?或者-huff|lzw-d|c不被认为是are,如果是的话,我如何访问它们?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-02-12 04:54:31

这就是使用main()方法的args参数的用途。对此进行测试的一种方法是:

代码语言:javascript
复制
public static void main(String args[]) {
    // Just for Testing args[]...
    for (String str : args) {
        System.out.println(arg);
    }
    System.exit(0);
    // --------------------------

    // --- Your method code here ---
}

对于不同的参数,您需要决定它们是否需要按照特定的顺序排列。如果没有,则需要为此编写代码,例如,这里有一种处理这种情况的方法:

代码语言:javascript
复制
// Class member variables (possible default values should be considered)...
private static String inputFile = "";              // Input File Path and File Name  [enclosed within quotation marks if whitespaces are in path]
private static String outputFile = "";             // Output File Path and File Name  [enclosed within quotation marks if whitespaces are in path]
private static String compressionType = "";        // Alternatives: HUFFMAN  (-huff) or  LZW        (-lzw).
private static String direction = "COMPRESS";      // Alternatives: COMPRESS (-c)    or  DECOMPRESS (-d).

// The main() method...
public static void main(String args[]) {
    // Process Command-Line Arguments provided in any order...
    for (String argument : args) {
        switch (argument.toLowerCase()) {
            case "-huff":
                compressionType = "HUFFMAN";
                continue;
            case "-lzw":
                compressionType = "LZW";
                continue;
            case "-c":
                direction = "COMPRESS";
                continue;
            case "-d":
                direction = "DECOMPRESS";
                continue;
        }

        // Source (input) and Destination (output) files...
        if (new File(argument).exists() && new File(argument).isFile()) {
            inputFile = argument;
        }
        else {
            outputFile = argument;
        }
    }
    // ------------------------------------------------

    System.out.println();
    System.out.println("Input File:       " + inputFile);
    System.out.println("output File:      " + outputFile);
    System.out.println("Compression Type: " + compressionType);
    System.out.println("Direction:        " + direction);

    // Do what you want with the variables contents ....

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60178221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档