首页 \ 问答 \ this.getClass()。getClassLoader()。getResource(“...”)和NullPointerException(this.getClass().getClassLoader().getResource(“…”) and NullPointerException)

this.getClass()。getClassLoader()。getResource(“...”)和NullPointerException(this.getClass().getClassLoader().getResource(“…”) and NullPointerException)

我在eclipse helios中创建了一个包含单个子模块的最小maven项目。

在src / test / resources文件夹中,我放了一个文件“install.xml”。 在文件夹src / test / java中,我创建了一个包含单个类的单个包:

  @Test
  public void doit() throws Exception {
    URL url = this.getClass().getClassLoader().getResource("install.xml");
    System.out.println(url.getPath());

  }

但是当我运行代码作为junit 4单元测试时,我只是得到一个NullPointerException。 以前,这已经有一百万次了。 有任何想法吗?

我遵循了本指南:

http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/

但仍然得到相同的错误。


I have created a minimal maven project with a single child module in eclipse helios.

In the src/test/resources folder I have put a single file "install.xml". In the folder src/test/java I have created a single package with a single class that does:

  @Test
  public void doit() throws Exception {
    URL url = this.getClass().getClassLoader().getResource("install.xml");
    System.out.println(url.getPath());

  }

but when I run the code as a junit 4 unit test I just get a NullPointerException. This has worked fine a million of times before. Any ideas?

I have followed this guide:

http://www.fuyun.org/2009/11/how-to-read-input-files-in-maven-junit/

but still get the same error.


原文:https://stackoverflow.com/questions/3803326
更新时间:2022-06-08 06:06

最满意答案

您应该将env.rb文件放在initializers文件夹中。 如果您不想将env.rb文件推送到heroku,可以将env.rb文件添加到.gitignore文件中。


You should put the env.rb file in initializers folder. You can add env.rb file to .gitignore file if you don't want to push it to heroku.

相关问答

更多
  • 您应该将env.rb文件放在initializers文件夹中。 如果您不想将env.rb文件推送到heroku,可以将env.rb文件添加到.gitignore文件中。 You should put the env.rb file in initializers folder. You can add env.rb file to .gitignore file if you don't want to push it to heroku.
  • 基本上,您希望使用环境变量进行字符串插值并展开主目录。 我不知道一个简单的方法来做后者,但如果你使用Spring进行设置,你可以使用它的PropertyPlaceholderConfigurer来替换字符串中的占位符。 默认情况下,环境变量包含在占位符替换集中。 更新:由于这是来自用户,您仍然可以使用Spring辅助类: String stringToBeInterpolated = ....; Properties properties = System.getProperties(); Property ...
  • [更新] 虽然“老回答”下的解决方案可以解决一般问题,但本部分是在您的评论澄清后回答您的具体问题。 您应该能够设置环境变量,就像您在问题中指定的一样。 例如,我有一个使用HTTP基本认证的Heroku应用程序。 # app/controllers/application_controller.rb class ApplicationController < ActionController::Base protect_from_forgery before_filter :authenticate ...
  • 当然,我自己也倾向于每次都“弯曲”一个框架,总有一种方法,但并不总是最好的解决方案。 我并没有在这里给出一个完整的实现,只是指出你的方向,这可能对你有用。 您可以扩展Laravel的基本应用程序类Illuminate\Foundation\Application ,其中包含在应用程序引导期间加载的$environmentFile变量存储环境文件,或者可能覆盖函数loadEnvironmentFrom($file)或environmentFile() 。 整个逻辑取决于你。 所以基本上你需要做的只是为了能够使 ...
  • 如果你的命令只是一个shell命令,你可以在子shell中运行你的命令,如下所示: ( . .env ; echo "$TEST" ) source或. 内置包含空格的赋值没有问题。 它将在当前shell环境中的.env文件中设置变量。 在调用外部程序的可能性更大的情况下,您还必须在env文件中为每个赋值添加“export”,如下所示: export TEST="hello world" 这是必要的,因为source不会像env那样导出指定的变量,即它们仅在子shell中设置,而不是在子shell内启动 ...
  • 我一直认为.env文件只是设置服务器的一部分。 git会忽略.env文件,所以一旦设置完毕,你可以推送/拉动你内心的内容,它会单独保留.env文件。 到目前为止,至少我是如何做到的。 I always thought that the .env file was just part of setting up your server. The .env file is ignored by git, so once it's setup, you can push/pull to your heart's ...
  • 宝石提供了一个发电机: $ rails generate figaro:install 生成器创建一个config / application.yml文件并修改.gitignore文件以防止将文件检入到git存储库中。 您可以将环境变量作为键/值对添加到config / application.yml中 : GMAIL_USERNAME: Your_Username 环境变量将作为ENV变量在您的应用程序中的任何位置可用: ENV["GMAIL_USERNAME"] 这为您提供了在代码中使用相同变量的 ...
  • 与dotenv文档中所述的dotenv ,您实际上需要使用.env文件中的export关键字来使参数可用于环境,例如 export FOO=foo 唯一的例外是,如果参数已经是一个环境变量。 例如,如果它已经在~/.zshrc导出,或者它已经是开始时获得的zsh环境的一部分(例如PATH或HOME )。 当更改到目录时,所有dotenv都会自动获取任何.env文件。 没有额外的“魔法”。 这意味着.env需要是一个有效的zsh脚本,并且其内容在当前shell会话的上下文中运行(基本上就像手动键入它一样)。 ...
  • 这有一个很棒的宝石,名为rails_config : github repo 但是,如果您只想配置此变量并且只需要此变量,则可以在rails已具有的环境配置文件中创建常量。 就像是: 在config / environments / production.rb中 # production.rb Rails.configuration.my_awesome_changing_domain = "somedomain.me" 在config / environments / development.rb中 # ...
  • cross-env NODE_ENV =测试节点-e \“require('./ setup-env')()\”&& jasmine-ts ** / *。spec.ts 这创建了一个NODE_ENV设置为测试的进程 然后,您创建一个需要'setup-env'的不同进程,然后退出该进程 然后你运行只有NODE_ENV = test设置的jasmine-ts cross-env NODE_ENV=test node -e \"require('./setup-env')()\" && jasmine-ts ** ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。