如何获取源目录中包含的文件?

时间:2011-12-19 20:51:30

标签: java eclipse maven

我的Maven / Eclipse项目有一个非常简单的目录布局:

.
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   ├── org
    │   │   │   └── tkassembled
    │   │   │       └── maven
    │   │   │           └── jasperreports
    │   │   │               └── JasperReportsApplication.java
    │   │   └── sampleReport.xml
    │   └── resources
    └── test
        ├── java
        └── resources

11 directories, 3 files

正如您所看到的,我在Java源目录的基础上有sampleReport.xml文件,但是当我尝试打开它时,我得到一个FileNotFoundException:

Exception in thread "main" java.io.FileNotFoundException: sampleReport.xml (No such file or directory)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:137)
    at org.tkassembled.maven.jasperreports.JasperReportsApplication.main(JasperReportsApplication.java:34)

这是我的pom.xml

<?xml version="1.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>org.tkassembled.maven</groupId>
    <artifactId>jasperreports-test</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>JasperReports Testing</name>

    <dependencies>
        <!-- SLF4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-api</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>log4j-over-slf4j</artifactId>
            <version>1.6.4</version>
        </dependency>
        <!-- Logback -->
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- JasperReports -->
        <dependency>
            <groupId>jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>3.5.3</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

这是我的Java应用程序:

package org.tkassembled.maven.jasperreports;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URI;
import java.net.URL;

import net.sf.jasperreports.engine.JasperCompileManager;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JasperReportsApplication {

    private static final Logger logger = LoggerFactory.getLogger(JasperReportsApplication.class);

    /*
     * There are basically four steps to a finished product:
     *      <X> Design a report with a JRXML file.
     *      <X> Compile the report to a binary, serialized JasperReport and save to disk.
     *      <X> Fill the compiled report with data.
     *      <X> Export it.
     */
    public static void main(String[] args) throws Exception {
        File jrxml = new File("sampleReport.xml");
//      URL url = JasperReportsApplication.class.getResource("sampleReport.xml");
//      File jrxml = new File(url.getFile());
        assert jrxml.exists();
        assert jrxml.isFile();

        File compileTarget = File.createTempFile("output", "jasper");

        FileInputStream jrxmlInput = new FileInputStream(jrxml);
        FileInputStream jasperInput = new FileInputStream(compileTarget);
        FileOutputStream jasperOutput = new FileOutputStream(compileTarget);

//      compile the report
        JasperCompileManager.compileReportToStream(jrxmlInput, jasperOutput);

//      lol

    }
}

如何在源目录的根目录中找到此文件?

3 个答案:

答案 0 :(得分:11)

当Maven打包它时会把它放在项目的.jar中,它将无法作为File访问。

可以使用

作为Stream进行访问
    JasperReportsApplication.class.getResourceAsStream("/sampleReport.xml")

此文件实际上应该在src/main/resources目录而不是src/main/java目录中,以遵循Maven约定,也就是说它不是Java源代码。

答案 1 :(得分:1)

默认情况下,非源文件应该位于src / main / resources文件夹中。

答案 2 :(得分:0)

您希望将此文件包含在资源文件夹中(默认情况下为src/main/resources)。您还可以change the default location