通过exec-maven-plugin运行Java代码时出现java.lang.reflect.InvocationTargetException

时间:2017-04-20 13:19:57

标签: java maven

我一直在努力工作几个小时。

我正在尝试使用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运行时成功执行项目,但不是从命令提示符运行

这是我配置exec-maven-plugin v1.5.0

的pom.xml
<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>

问题是我的代码无法加载位于我的资源文件夹中的文件。

以下是我com.services.util包中的EnvHandler类的代码

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 个答案:

答案 0 :(得分:1)

我一直在努力解决这个问题,直到我发现错误是因为在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;

        }
    }