当前位置: 首页 > 知识库问答 >
问题:

通过exec-maven-plugin运行Java代码时的Java.lang.Reflect.InvocationTargetException

黄昊
2023-03-14

我已经纠结了好几个小时了。

我正试图用maven运行Java类,但遇到以下错误

java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:294)
    at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Properties.java:418)
    at java.util.Properties.load0(Properties.java:337)
    at java.util.Properties.load(Properties.java:325)
    at com.services.utils.EnvHandler.getProperty(EnvHandler.java:26)
    at com.services.agentsMonitor.MonitorAgentsTrigger.main(MonitorAgentsTrigger.java:77)


Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.5.0:java (default-cli) on project SupportUtilities: 
An exception occured while executing the Java class. null: 
InvocationTargetException: NullPointerException 

问题是,当从eclipse运行而不是从命令提示符运行时,我能够成功地执行项目

这是我的pom.xml,在这里我配置了exec-maven-pluginv1.5.0

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.services</groupId>
  <artifactId>SupportUtilities</artifactId>
  <version>1.0-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <mockito.version>1.9.0</mockito.version>
    <platform.messaging-jms-client.version>3.6.2.2</platform.messaging-jms-client.version>
    <tibco.jms.client.version>7.0.1</tibco.jms.client.version>
    <omslite.version>0.0.1-SNAPSHOT</omslite.version>
    <logmon-parent-pom>2.2.2</logmon-parent-pom>
    <fasterxml.jackson.jsonSchema>2.4.0</fasterxml.jackson.jsonSchema>
    <fasterxml.jackson.databind>2.4.0</fasterxml.jackson.databind>
    <fasterxml.jackson.core>2.4.0</fasterxml.jackson.core>
    <ojdbc6.version>12.1.0.2.0</ojdbc6.version>
    <commons-lang.version>2.1</commons-lang.version>
    <log4j.version>1.2.16</log4j.version>
    <json-simple.version>1.1</json-simple.version>
    <apache.commons.io.version>1.4.0</apache.commons.io.version>
    <netty.version>3.2.10.Final</netty.version>
    <javax.jms.version>1.1</javax.jms.version>
    <org.slf4j.version>1.6.2</org.slf4j.version>
    <joda-time.version>2.9</joda-time.version>
    <sling.commons.json.version>2.0.12</sling.commons.json.version>
    <jsch.version>0.1.51</jsch.version>
    <xercesImpl.version>2.8.0</xercesImpl.version>
    <httpclient.version>4.5.1</httpclient.version>
    <javax.mail.version>1.5.0-b01</javax.mail.version>
    <poi-ooxml.version>3.9</poi-ooxml.version>
    <JSON4J.version>1.1.0</JSON4J.version>
    <exec-maven-plugin.version>3.1</exec-maven-plugin.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>com.oracle</groupId>
        <artifactId>ojdbc6</artifactId>
        <version>${ojdbc6.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-lang</groupId>
        <artifactId>commons-lang</artifactId>
        <version>${commons-lang.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.googlecode.json-simple</groupId>
        <artifactId>json-simple</artifactId>
        <version>${json-simple.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>com.springsource.org.apache.commons.io</artifactId>
        <version>${apache.commons.io.version}</version>
    </dependency>
    <dependency>
        <groupId>org.jboss.netty</groupId>
        <artifactId>netty</artifactId>
        <version>${netty.version}</version>
    </dependency>
    <dependency>
        <groupId>tibco.jms</groupId>
        <artifactId>tibco_jms</artifactId>
        <version>${tibco.jms.client.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.jms</groupId>
        <artifactId>jms</artifactId>
        <version>${javax.jms.version}</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>${org.slf4j.version}</version>
    </dependency>
    <dependency>
        <groupId>com.walmart.platform.messaging</groupId>
        <artifactId>messaging-jms-client</artifactId>
        <version>${platform.messaging-jms-client.version}</version>
    </dependency>
    <dependency>
        <groupId>joda-time</groupId>
        <artifactId>joda-time</artifactId>
        <version>${joda-time.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.sling</groupId>
        <artifactId>org.apache.sling.commons.json</artifactId>
        <version>${sling.commons.json.version}</version>
    </dependency>
    <dependency>
        <groupId>com.jcraft</groupId>
        <artifactId>jsch</artifactId>
        <version>${jsch.version}</version>
    </dependency>
    <dependency>
        <groupId>xerces</groupId>
        <artifactId>xercesImpl</artifactId>
        <version>${xercesImpl.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>${httpclient.version}</version>
    </dependency>
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>${javax.mail.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.poi</groupId>
        <artifactId>poi-ooxml</artifactId>
        <version>${poi-ooxml.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>JSON4J</artifactId>
        <version>${JSON4J.version}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>${fasterxml.jackson.core}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>${fasterxml.jackson.databind}</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.module</groupId>
        <artifactId>jackson-module-jsonSchema</artifactId>
        <version>${fasterxml.jackson.jsonSchema}</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>exec-maven-plugin</artifactId>
            <version>1.5.0</version>
        </plugin> 
        <!-- <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>${exec-maven-plugin.version}</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin> -->
    </plugins>

  </build>

</project>
public Properties getProperty(String env, String mgmtSystem) throws IOException {

        Properties prop = new Properties();
        InputStream propFile = null;


        /*
         * Loading based on OMS/IMS
         */

        if("OMS".equalsIgnoreCase(mgmtSystem)) {
            /*
             * Loading the properties file based on environment qa/qa2/prod
             */
            if("qa".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/OMS/Credentials_qa.properties");   //I have error here
                System.out.println("PropFile->" + propFile.toString());
                prop.load(propFile);
                return prop;

            }
            else if("qa2".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/OMS/Credentials_qa2.properties");
                prop.load(propFile);
                return prop;

            }else if("prod".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/OMS/Credentials_prod.properties");
                prop.load(propFile);
                return prop;

            }
        } else if("IMS".equalsIgnoreCase(mgmtSystem)) {
            /*
             * Loading the properties file based on environment qa/qa2/prod
             */
            if("qa".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/IMS/Credentials_qa.properties");     
                prop.load(propFile);
                return prop;

            }
            else if("qa2".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/IMS/Credentials_qa2.properties");
                prop.load(propFile);
                return prop;

            }else if("prod".equalsIgnoreCase(env)) {

                propFile = EnvHandler.class.getResourceAsStream("/com/Services/agentsMonitor/IMS/Credentials_prod.properties");
                prop.load(propFile);
                return prop;

            }
        }


        return null;

}
--src/main/java
     --com.services.agentsMonitor
         --MonitorAgentsForTrigger.java
     --com.services.logsMonitor
         --MonitorLogsForConnectionIssues.java
     --com.services.utils
         --EnvHandler.java
--src/main/resources
     --com.services.agentsMonitor
         --OMS
             --Credentials_qa.properties
             --Credentials_qa2.properties
             --Credentials_prod.properties  
         --IMS
             --Credentials_qa.properties
             --Credentials_qa2.properties
             --Credentials_prod.properties        

共有1个答案

曾涵育
2023-03-14

我为此苦苦挣扎了好几天,直到我发现错误是因为在windows中运行时,reourceUrl不被视为区分大小写,而在unix计算机上运行时被视为区分大小写,所以资源中提到的是com/services而不是/com/services

if("OMS".equalsIgnoreCase(mgmtSystem)) {
        /*
         * Loading the properties file based on environment qa/qa2/prod
         */
        if("qa".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/OMS/Credentials_qa.properties");   //I have error here
            System.out.println("PropFile->" + propFile.toString());
            prop.load(propFile);
            return prop;

        }
        else if("qa2".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/OMS/Credentials_qa2.properties");
            prop.load(propFile);
            return prop;

        }else if("prod".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/OMS/Credentials_prod.properties");
            prop.load(propFile);
            return prop;

        }
    } else if("IMS".equalsIgnoreCase(mgmtSystem)) {
        /*
         * Loading the properties file based on environment qa/qa2/prod
         */
        if("qa".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/IMS/Credentials_qa.properties");     
            prop.load(propFile);
            return prop;

        }
        else if("qa2".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/IMS/Credentials_qa2.properties");
            prop.load(propFile);
            return prop;

        }else if("prod".equalsIgnoreCase(env)) {

            propFile = EnvHandler.class.getResourceAsStream("/com/services/agentsMonitor/IMS/Credentials_prod.properties");
            prop.load(propFile);
            return prop;

        }
    }
 类似资料:
  • 我想通过exec maven插件运行2个主配置文件。在我的生产中,我只使用“prod”配置文件,在我的持续集成中,我希望使用“prepod”配置文件和“prod”配置文件。 在产品 mvn-pprod 在连续集成中: mvn-PpreProd, prod 当我运行maven命令时,我有这个日志(com.mycompany.RunMyProd.main()运行2次): [信息]——exec mave

  • 我有一个问题使用Maven exec插件执行Java工具。我试图将输出目录指定为javah,其中头文件要放置,但我得到了错误: [信息]---exec maven插件:1.5.0:exec(创建jni头)@jni测试osgi---错误:未知选项:-d/home/kerry[Error]命令执行失败。 这是POM的相关章节: 如果我执行从命令行执行,则没有问题。 我是做错了什么,还是Maven ex

  • 问题内容: 我的pom文件列表 然而,我得到 是(点在这里) 也指向正确的Java版本 在这台机器上,我正在使用(return ) 在我的中,我定义了: 在运行时,我发现实际上我在使用Java 6 在哪里可以定义?我的.zshrc有多次source(d)。 问题答案: 检查您的maven安装中的脚本,以查看其如何构建命令。也许您或其他人在其中进行了硬编码而忘记了它。

  • 我当前的java版本是7: 我有命令,其中设置为8: 现在的问题是。当我在项目中运行我的一个配置文件(有生成文件的maven exec插件)时: 我得到了例外: 所以它似乎仍然需要我的系统java。为什么当mvn版本打印java8时?当我更改我的系统java时,它可以正常工作。这是maven的一些功能,也可能是我使用的插件问题? 更新: 调试maven exec插件时,我看到这一行: 所以问题是如

  • 问题内容: 我有一个应用程序,该应用程序应该使用一些shell命令将文件从sdcard复制到/ system / media /。它将需要root用户,并且我正在对root用户的设备进行测试。我正在使用运行时来执行Shell命令,但无法正常工作。这是我的运行时所需要的 但是我的日志只显示其中两个没有被拒绝 这两个看起来是sysrw和sysro命令,但是当我触发此代码时,该应用程序仍会请求root权

  • 如果没有,有人知道如何通过NetBeans使用-e或-x开关运行Maven吗?我假设它是通过右键点击POM,然后运行目标,然后在textfield中输入一些东西。