访问 jar 文件中的报告时出现问题

发布于 2024-10-03 10:28:32 字数 382 浏览 0 评论 0原文

我有一个包含多个报告(文件 .jasper)的 jar 应用程序,我获取报告路径的方式是:

getClass().getResource("/reportes/mireporte.jasper").toString();

但是当我执行时,出现下一个错误:FileNotFoundException 尽管当我用 winrar 打开报告时,报告位于 jar 文件中。

我的问题是:

  1. 这是获取 jar 文件中报告路径的方法吗?
  2. 是否可以打开 jar 文件中的报告?

I have a jar application that contains several reports (files .jasper) and the way that I get the path of the report is:

getClass().getResource("/reportes/mireporte.jasper").toString();

but when I execute I get the next error: FileNotFoundException
although the report is within the jar file when I open it with the winrar.

My questions are:

  1. Is this the way to get the path of a report in a jar files?
  2. Is it possible to open reports that are within of a jar file?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

匿名的好友 2024-10-10 10:28:32

为什么要将资源转换为字符串?

尝试使用(取决于您想要如何使用资源):

  • getResource("/report/mireporte.jasper").getFile()

  • getResource("/report/mireporte.jasper").getInputStream()

从“getResource”返回的对象是 URL:

Why are you converting the resource to String ??

Try to use (depending on how you want to use the resource):

  • getResource("/report/mireporte.jasper").getFile()

  • getResource("/report/mireporte.jasper").getInputStream()

Returned object from 'getResource' is URL:

好多鱼好多余 2024-10-10 10:28:32

1) 这是获取 jar 文件中报告路径的方法吗?

是的,调用 getClass().getClassLoader().getResources("mireporte.jasper") - 它会给您一个与名称匹配的资源列表。

2) 是否可以打开 jar 文件中的报告?

是的!使用 getClass().getResourceAsStream("...") 并从中读取!

例子:

public static void main(String... args) throws IOException {
    final ClassLoader cl = GetResource.class.getClassLoader();

    // print all resources (could be more than one)
    Enumeration<URL> resources =
        cl.getResources("foo/bla/tjo/mireporte.jasper");

    while (resources.hasMoreElements())
        System.out.println(resources.nextElement());


    // read one of the files
    Scanner s = new Scanner(
            cl.getResourceAsStream("foo/bla/tjo/mireporte.jasper"));
    try {
        while (s.hasNextLine()) {
            System.out.println(s.nextLine());
        }
    } finally {
        if (s != null)
            s.close();
    }
}

1) Is this the way to get the path of a report in a jar files?

Yes, call the getClass().getClassLoader().getResources("mireporte.jasper") - it will give you a list of resources that matches the name.

2) Is posible to open reports that are within of a jar file?

Yes! Use getClass().getResourceAsStream("...") and read from that one!

Example:

public static void main(String... args) throws IOException {
    final ClassLoader cl = GetResource.class.getClassLoader();

    // print all resources (could be more than one)
    Enumeration<URL> resources =
        cl.getResources("foo/bla/tjo/mireporte.jasper");

    while (resources.hasMoreElements())
        System.out.println(resources.nextElement());


    // read one of the files
    Scanner s = new Scanner(
            cl.getResourceAsStream("foo/bla/tjo/mireporte.jasper"));
    try {
        while (s.hasNextLine()) {
            System.out.println(s.nextLine());
        }
    } finally {
        if (s != null)
            s.close();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文