2013-05-30 55 views
0

我正在關注Maven by Example中的簡單天氣教程。當我執行程序時,我會得到以下例外。這是否需要任何特定的類路徑設置?Maven簡單天氣教程

POM

<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>[email protected]</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> 

Main.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 

回答

0

好像你的應用程序找不到log4j.properties 你使用標準的maven項目結構嗎? log4j.properties位於何處?

+0

使用mvn archetype創建項目:generate。所以目錄結構是標準的。 log4j.properties位於src/main。 – gammay

+0

將其移至src/main/resources。所有資源文件(如.properties)應該位於'resources'文件夾中,否則maven將無法找到它。 因此,你的項目結構應該是: SRC - 主 --java --resources ---log4j.properties 希望,它將幫助。 – tmn4jq

+0

這工作,謝謝!我會接受答案。但現在我有另一個問題。希望你不介意在這裏問。我正在運行一個代理服務器。如何爲mvn exec指定代理? – gammay

0

'resources'文件夾應位於主文件夾(src/main/resources)而不是src/resources中。