getClass()。getResource() [英] File loading by getClass().getResource()

查看:124
本文介绍了getClass()。getResource()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经遵循使用 getClass.getResource(path)加载资源文件的方式。代码片段在这里:

  String url =Test.properties; 

System.out.println(打印路径之前);
System.out.println(Path2:+ getClass()。getResource(url).getPath());

FileInputStream inputStream = new FileInputStream(new File(getClass()。getResource(url).toURI()));
i_propConfig.load(inputStream);
inputStream.close();

我已经在eclipse中配置了层次结构(在源代码下有一个名为SwingDemo的文件夹,在SwingDemo有我的java文件以及资源文件)...


  1. src

    • SwingDemo


      1. CustomDialog.java

      2. Test.properties



当我在eclipse上运行这些时,一切都运行正常。但是一旦我尝试从cmd行运行应用程序空指针异常正在发生。



命令行部署层次结构如下:



文件夹: D:\Work\Java Progrms\SwingDemo



层次结构:


  1. SwingDemo

    • CustomDialog.java

    • Test.properties


首先我将 SwingDemo 文件夹从命令行(javac CustomDialog.java )。然后我再一步回到Java程序文件夹(如我所提到的.java类中的程序包),并使用着名的

  java SwingDemo.CustomDialog 

当我使用新的FileInputStream(路径)时,我曾经遵循类似的步骤)以前。
这样做后,我得到空指针异常..



我认为 getClass()。getResource(url)无法从特定目录加载文件。这就是为什么我将资源放在与我的java文件相同的目录中。它在Eclipse中运行良好。但是为什么当我从命令行运行时,这会给出错误。

解决方案

getClass()。getResource )使用类加载器加载资源。这意味着该资源必须位于要加载的类路径中。



当使用Eclipse进行此操作时,放入源文件夹的所有内容都由Eclipse编译: / p>


  • .java文件被编译成进入bin目录的.class文件(默认情况下)

  • 其他文件被复制到bin目录(尊重包/文件夹hirearchy)



使用Eclipse启动程序时,bin目录因此在类路径中,并且由于它包含Test.properties文件,该文件可以由类加载器使用 getResource() getResourceAsStream()



如果它不能从命令行工作,那是因为该文件不在类路径中。 p>

请注意,您不应该执行

  FileInputStream inputStream = new FileInputStream(new文件(的getClass()的getResource(URL).toURI())); 

加载资源。因为只有在从文件系统加载文件时才可以工作。如果您将应用程序打包到一个jar文件中,或者通过网络加载类,它将无法正常工作。要获取InputStream,只需使用

  getClass()。getResourceAsStream(Test.properties)

最后,如文档所示,

  Foo.class.getResourceAsStream(Test.properties)

将加载测试。属性文件位于与Foo类相同的包中。

  Foo.class.getResourceAsStream(/ com / foo / bar / Test.properties)

将加载位于软件包 com.foo.bar中的Test.properties文件


I have followed the way of loading the resource file by using getClass.getResource(path). The snippet of code is here :

String url = "Test.properties";

System.out.println("Before printing paths..");
System.out.println("Path2: "+ getClass().getResource(url).getPath());

FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));
i_propConfig.load(inputStream);
inputStream.close();

I have configured it in eclipse with the hierarchy (Under source there is a folder called SwingDemo. In SwingDemo there is my java file as well as the resource file)...

  1. src
    • SwingDemo

      1. CustomDialog.java
      2. Test.properties

When I am running this on eclipse everything is running fine. But as soon as I attempt to run the apps from cmd line null pointer exception is occuring..

Command Line deployment hierarchy is as follows:

Folder : D:\Work\Java Progrms\SwingDemo

Hierarchy:

  1. SwingDemo
    • CustomDialog.java
    • Test.properties

First of all I compiled this file inside SwingDemo folder from command line (javac CustomDialog.java). Then I move one step back to Java Programs folder (as I mentioned the package inside .java class) and run the apps by using the famous

java SwingDemo.CustomDialog

I used to follow similar steps when I used new FileInputStream("path") previously. After doing this fashion I am getting null pointer exception..

I think getClass().getResource(url) cannot load file from a specific directory. That's why I put the resource in same directory as that of my java file. It ran fine in Eclipse. But why this is giving error when I run from Command Line.

解决方案

getClass().getResource() uses the class loader to load the resource. This means that the resource must be in the classpath to be loaded.

When doing it with Eclipse, everything you put in the source folder is "compiled" by Eclipse:

  • .java files are compiled into .class files that go the the bin directory (by default)
  • other files are copied to the bin directory (respecting the package/folder hirearchy)

When launching the program with Eclipse, the bin directory is thus in the classpath, and since it contains the Test.properties file, this file can be loaded by the class loader, using getResource() or getResourceAsStream().

If it doesn't work from the command line, it's thus because the file is not in the classpath.

Note that you should NOT do

FileInputStream inputStream = new FileInputStream(new File(getClass().getResource(url).toURI()));

to load a resource. Because that can work only if the file is loaded from the file system. If you package your app into a jar file, or if you load the classes over a network, it won't work. To get an InputStream, just use

getClass().getResourceAsStream("Test.properties")

And finally, as the documentation indicates,

Foo.class.getResourceAsStream("Test.properties")

will load a Test.properties file located in the same package as the class Foo.

Foo.class.getResourceAsStream("/com/foo/bar/Test.properties")

will load a Test.properties file located in the package com.foo.bar.

这篇关于getClass()。getResource()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆