0

我正在关注Maven by Example中的简单天气教程。当我执行程序时,我得到以下异常。这需要任何特定的类路径设置吗?

聚甲醛

<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.example.maven.weather</groupId>
<artifactId>simple-weather</artifactId>
<version>1.0</version>
<packaging>jar</packaging>

<name>simple-weather</name>
<url>http://maven.apache.org</url>

<licenses>
    <license>
        <name>Apache 2</name>
        <url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
        <distribution>repo</distribution>
        <comments>A business-friendly OSS license</comments>
    </license>
</licenses>

<organization>
    <name>Sonatype</name>
    <url>http://www.sonatype.com</url>
</organization>

<developers>
    <developer>
        <id>jason</id>
        <name>Jason Van Zyl</name>
        <email>jason@maven.org</email>
        <url>http://www.sonatype.com</url>
        <organization>Sonatype</organization>
        <organizationUrl>http://www.sonatype.com</organizationUrl>
        <roles>
            <role>developer</role>
        </roles>
        <timezone>-6</timezone>
    </developer>
</developers>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

<dependencies>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>1.2.14</version>
    </dependency>
    <dependency>
        <groupId>dom4j</groupId>
        <artifactId>dom4j</artifactId>
        <version>1.6.1</version>
    </dependency>
    <dependency>
        <groupId>jaxen</groupId>
        <artifactId>jaxen</artifactId>
        <version>1.1.1</version>
    </dependency>
    <dependency>
        <groupId>velocity</groupId>
        <artifactId>velocity</artifactId>
        <version>1.5</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
  <artifactId>junit</artifactId>
  <version>3.8.1</version>
  <scope>test</scope>
</dependency>

主.java

package com.example.maven.weather;

import java.io.InputStream;

import org.apache.log4j.PropertyConfigurator;

public class Main {

public static void main(String[] args) throws Exception {
    // Configure Log4J
    PropertyConfigurator.configure(Main.class.getClassLoader()
                                   .getResource("log4j.properties"));

    // Read the Zip Code from the Command-line (if none supplied, use 60202)
    String zipcode = "60202";
    try {
        zipcode = args[0];
    } catch( Exception e ) {}

    // Start the program
    new Main(zipcode).start();
}

private String zip;

public Main(String zip) {
    this.zip = zip;
}

public void start() throws Exception {
    // Retrieve Data
    InputStream dataIn = new YahooRetriever().retrieve( zip );

    // Parse Data
    Weather weather = new YahooParser().parse( dataIn );

    // Format (Print) Data
    System.out.print( new WeatherFormatter().format( weather ) );
}
}  

跑:

mvn exec:java -Dexec.mainClass=com.exa
mple.maven.weather.Main
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building simple-weather 1.0
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] >>> exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather >>>
[INFO]
[INFO] <<< exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather <<<
[INFO]
[INFO] --- exec-maven-plugin:1.2.1:java (default-cli) @ simple-weather ---
[WARNING]
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.
java:39)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAcces
sorImpl.java:25)
        at java.lang.reflect.Method.invoke(Method.java:597)
        at org.codehaus.mojo.exec.ExecJavaMojo$1.run(ExecJavaMojo.java:297)
        at java.lang.Thread.run(Thread.java:662)
Caused by: java.lang.NullPointerException
        at org.apache.log4j.PropertyConfigurator.doConfigure(PropertyConfigurato
r.java:433)
        at org.apache.log4j.PropertyConfigurator.configure(PropertyConfigurator.
java:336)
        at com.example.maven.weather.Main.main(Main.java:12)
        ... 6 more
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.902s
[INFO] Finished at: Thu May 30 14:23:06 IST 2013
[INFO] Final Memory: 4M/15M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:1.2.1:java (d
efault-cli) on project simple-weather: An exception occured while executing the
Java class. null: InvocationTargetException: NullPointerException -> [Help 1]
[ERROR]
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e swit
ch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR]
[ERROR] For more information about the errors and possible solutions, please rea
d the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionE
xception
4

2 回答 2

0

您的应用程序似乎找不到 log4j.properties 您使用标准的 maven 项目结构吗?log4j.properties 位于哪里?

于 2013-05-30T09:17:26.480 回答
0

'resources' 文件夹应位于主文件夹 (src/main/resources) 而不是 src/resources 中。

于 2014-01-06T10:53:28.900 回答