-1

I have maven project and I'm trying to use commons cli library to handle input data:

import org.apache.commons.cli.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Cli {
    private static final Logger log = Logger.getLogger(Cli.class.getName());
    private String[] args = null;
    private Options options = new Options();

    public Cli(String[] args) {

        this.args = args;

        options.addOption("h", "help", false, "show help.");
        options.addOption("i", "inputs", true, "Enter the input file name.");
        options.addOption("o", "output", true, "Enter the output file name.");
    }

    public String[] parse() {
        CommandLineParser parser = new BasicParser();
        String[] returnArguments = new String[2];
        CommandLine cmd = null;
        try {
            cmd = parser.parse(options, args);

            if (cmd.hasOption("h"))
                help();

            if (cmd.hasOption("i")) {
                log.log(Level.INFO, "The initial file will be " + cmd.getOptionValue("i"));
                returnArguments[0] = cmd.getOptionValue("i");
            } else {
                log.log(Level.SEVERE, "Missing input file name option");
                help();
            }

            if (cmd.hasOption("o")) {
                log.log(Level.INFO, "The output file will be " + cmd.getOptionValue("o"));
                returnArguments[1] = cmd.getOptionValue("o");
            } else {
                log.log(Level.SEVERE, "Missing output file name option");
                help();
            }
        } catch (ParseException e) {
            log.log(Level.SEVERE, "Failed to parse command line properties", e);
            help();
        }
        return returnArguments;
    }

    private void help() {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("Main", options);
        System.exit(0);
    }
}

I set arguments in Intellij Idea: -i inputs.zip -o output.zip It works, but what command should I use in console?

I tried: java -jar NewMyZip-1.jar -i inputs.zip -o output.zip and got the exception:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cl
i/ParseException
        at main.MyZip.main(MyZip.java:12)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseExcepti
on
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        ... 1 more

Does anybody know what is the problem and how to solve it ?

Den
  • 3
  • 1
  • 3
  • `java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException` means that, although you are importing `org.apache.commons.cli.*` in the class, it isn't getting included in the jar you create. – Andreas Jan 05 '15 at 17:14
  • Java jars are no DLL's, if ever you see this kind of error, Go ahead and lookup the class in the expected jar. You can open that in 7zip or winzip. You will find so much interesting info there. – randominstanceOfLivingThing Jan 05 '15 at 17:19
  • @Andreas how can I inxlude **org.apache.commons.cli.*** in my jar file?? – Den Jan 05 '15 at 23:48

1 Answers1

1

Is your NewMyZip-1.jar containing commons-cli ? Otherwise make sure you include commons-cli jar in your classpath:

java -cp PATH_TO_JAR/commons-cli.jar -jar NewMyZip-1.jar -i inputs.zip -o output.zip
panagdu
  • 2,133
  • 1
  • 21
  • 36