java.lang.NoClassDefFoundError:org/apache/commons/cli/ParseException [英] java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException

查看:250
本文介绍了java.lang.NoClassDefFoundError:org/apache/commons/cli/ParseException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将apache cli添加到我的应用程序中,但是我遇到了问题.这些错误会在我尝试运行时显示:

I want to add apache cli to my application, but I have problem. These errors show when I try to run it:

Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/commons/cli/ParseException
    at java.lang.Class.getDeclaredMethods0(Native Method)
    at java.lang.Class.privateGetDeclaredMethods(Class.java:2701)
    at java.lang.Class.privateGetMethodRecursive(Class.java:3048)
    at java.lang.Class.getMethod0(Class.java:3018)
    at java.lang.Class.getMethod(Class.java:1784)
    at sun.launcher.LauncherHelper.validateMainClass(LauncherHelper.java:544)
    at sun.launcher.LauncherHelper.checkAndLoadMain(LauncherHelper.java:526)
Caused by: java.lang.ClassNotFoundException: org.apache.commons.cli.ParseException
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 7 more

这是我的代码:

CommandLineParser parser = new PosixParser();
Options options = new Options();
options.addOption("a", "abc", true, "First parameter");

try {
    CommandLine commandLine = parser.parse(options, args);
    System.out.println(commandLine.getOptionValue("a"));
} catch (ParseException e1) {
    e1.printStackTrace();
}

我还在pom.xml中添加了以下内容:

I also added in pom.xml this:

<dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.2</version>
</dependency>

但这无济于事://我也首先手动添加了commons-cli-1.3.1.jar和后来的commons-cli-1.2.jar,但这两者都无济于事.

But it doesn't help :/ Also I added manually firstly commons-cli-1.3.1.jar and later commons-cli-1.2.jar but both doesn't help.

@edit

Ps.我将其作为"java -jar filename.jar"运行.

Ps. I'm running it as "java -jar filename.jar".

推荐答案

只需几分钟的更改,我就能执行以下代码:-

With few minute changes I am able to execute this code:-

    CommandLineParser parser = new PosixParser();
    Options options = new Options();
    options.addOption("a", true, "First parameter"); 
    args=new String[]{"-a abc"};

    try {
        CommandLine commandLine = parser.parse(options, args );    
        System.out.println(commandLine.getOptionValue("a"));
    } catch (ParseException e1) {
        e1.printStackTrace();
    }


Output :-  abc

在我的pom.xml中:-

In my pom.xml :-

  <dependency>
    <groupId>commons-cli</groupId>
    <artifactId>commons-cli</artifactId>
    <version>1.2</version>
  </dependency>

commons-cli-1.2.jar对您的代码不可见.

commons-cli-1.2.jar is not visible to your code.

这篇关于java.lang.NoClassDefFoundError:org/apache/commons/cli/ParseException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆