在本地运行应用程序时无法从资源文件夹加载文件 [英] Unable to load file from resources folder when running app locally

查看:127
本文介绍了在本地运行应用程序时无法从资源文件夹加载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java 8,我有一个AWS lambda函数,该函数在src/main/resources文件夹中具有一个平面文件.在AWS上运行时,我没有任何问题.但是当我在本地运行时,我的应用找不到文件.

Using Java 8, I have an AWS lambda function that has a flat file in the src/main/resources folder. When running on AWS, I'm having no issues. But when I run locally, my app can't find the file.

这是我的代码的样子:

    ClassLoader classLoader = getClass().getClassLoader();
    URL url =  classLoader.getResource( "./resources/forms/myform.pdf" );

当它作为AWS Lambda运行但无法在本地运行时,此选项可以很好地进行加载.在本地运行时,url变量为null.当我在AWS上运行代码时,ClassLoader为java.net.URLClassLoader,而在本地为sun.misc.Launcher$AppClassLoader.我不确定这是否重要.

This works fine for loading it when it runs as an AWS lambda, but does not work locally. When running locally, the url variable is null. When I run the code on AWS, the ClassLoader is java.net.URLClassLoader while locally it is sun.misc.Launcher$AppClassLoader. I'm not sure if this matters.

这是我的文件结构,如果有帮助的话:

Here is my file structure if it helps:

我正在使用我的ControllerTest类.我没有使用AWS默认创建的LambdaFunctionHandlerTest.这是我ControllerTest所做的全部事情:

I'm using my ControllerTest class. I'm not using the LambdaFunctionHandlerTest that was created by default by AWS. Here is all that my ControllerTest does:

public class ControllerTest {

    public static void main( String[] args ) {

        MyController controller = new MyController();
        controller.create();

    }

}

有什么想法可以使它在本地和AWS上运行吗?这是Eclipse中的Maven项目,我认为我在测试功能中执行控制器的方式不正确. eclipse中的运行配置是否需要更改为默认设置?

Any ideas how I can get this to work locally and on AWS? It's a maven project in Eclipse and I'm thinking the way I'm executing my controller in my test function is incorrect. Does the run configuration in eclipse need to be altered from the defaults?

更新:将我的路径更改为forms/myform.pdf修复了本地运行的问题.但是,现在作为AWS lambda函数运行时,它找不到文件.这是在AWS中创建的文件夹结构:

Update: Changing my path to forms/myform.pdf fixed the issue running locally. However, it now fails to find the file when running as an AWS lambda function. Here is the folder structure that is created in AWS:

推荐答案

您可以在此处查看Maven标准目录布局: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

You may check the Maven Standard Directory Layout here: https://maven.apache.org/guides/introduction/introduction-to-the-standard-directory-layout.html

所有测试类以及测试资源将被编译并放置在target/test-classes目录中.对于您的情况,getClassLoader().getResource会将路径返回到要存储form/myform.pdf的target/test-classes目录.

All your test classes along with test resources will be compiled and placed inside target/test-classes directory. In your case getClassLoader().getResource will return the path to target/test-classes directory where forms/myform.pdf is to be stored.

默认情况下,测试类将从您的test-classes目录中找到资源.如果在test-classes目录中找不到它们,则将搜索classes文件夹.

By default, the test class will find resources from your test-classes directory. If they are not found in the test-classes directory, the classes folder will be searched.

这篇关于在本地运行应用程序时无法从资源文件夹加载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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