gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 06:47:07 26 4
gpt4 key购买 nike

我已经为此苦苦挣扎了几个小时。

我正在尝试使用 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-plugin v1.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>

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

下面是我的 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

最佳答案

我已经为此苦苦挣扎了好几天,直到我发现错误是因为在 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-plugin 运行 Java 代码时出现 java.lang.reflect.InitationTargetException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43520767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com