2016-09-08 21 views
2

我看到這個錯誤的文件,當我嘗試執行我aws lambda function如何讀取從AWS拉姆達的Java類路徑

1) Error injecting constructor, java.lang.NullPointerException 
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:66) 
at in.nikhilbhardwaj.path.route.resources.ServicesResource.class(ServicesResource.java:56) 
while locating in.nikhilbhardwaj.path.route.resources.ServicesResource 
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction.<init>(HelloWorldIntentAction.java:44) 
while locating in.nikhilbhardwaj.path.alexa.intent.HelloWorldIntentAction 
while locating in.nikhilbhardwaj.path.alexa.intent.IntentAction annotated with @com.google.inject.multibindings.Element(setName=,uniqueId=10, type=MAPBINDER, keyType=java.lang.String) 
at in.nikhilbhardwaj.path.alexa.intent.IntentModule.configure(IntentModule.java:17) (via modules: in.nikhilbhardwaj.path.alexa.AlexaStarterApplicationModule -> in.nikhilbhardwaj.path.alexa.intent.IntentModule -> com.google.inject.multibindings.MapBinder$RealMapBinder) 
while locating java.util.Map<java.lang.String, in.nikhilbhardwaj.path.alexa.intent.IntentAction> 
for parameter 0 at in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl.<init>(IntentHandlerServiceImpl.java:16) 
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerServiceImpl 
while locating in.nikhilbhardwaj.path.alexa.intent.IntentHandlerService 
for parameter 0 at in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet.<init>(AlexaStarterSpeechlet.java:26) 
while locating in.nikhilbhardwaj.path.alexa.AlexaStarterSpeechlet 
Caused by: java.lang.NullPointerException 
at java.io.Reader.<init>(Reader.java:78) 
at java.io.InputStreamReader.<init>(InputStreamReader.java:113) 
at in.nikhilbhardwaj.path.route.resources.ServicesResource.initializeServiceIds(ServicesResource.java:88) 
at in.nikhilbhardwaj.path.route.resources.ServicesResource.<init>(ServicesResource.java:68) 
at in.nikhilbhardwaj.path.route.resources.ServicesResource$$FastClassByGuice$$3d6e91ec.newInstance(<generated>) 

我修改我的代碼從classpath,而不是文件系統讀取通過以下this question

這是從那裏引發異常的線(88線中ServicesResource.java) -

Iterable<CSVRecord> records = CSVFormat.RFC4180.withFirstRecordAsHeader() 
    .parse(new InputStreamReader(this.getClass().getResourceAsStream(RESOURCES_ROOT + RESOURCE_CALENDAR), StandardCharsets.UTF_8)); 

並且這些是第我正在使用e常數 -

public static final String RESOURCE_CALENDAR = "calendar.txt"; 
public static final String RESOURCES_ROOT = "/path-nj-us/"; 

我想,我已經打包的,我上傳到aws lambda正確的代碼,當我刪除這些引用的作品。這裏的是什麼zip包含(我省略了不相關的文件) -

➜ distributions git:(master) ✗ jar tf path-alexa-skills-kit.zip 
in/ 
in/nikhilbhardwaj/ 
in/nikhilbhardwaj/path/ 
in/nikhilbhardwaj/path/route/ 
in/nikhilbhardwaj/path/alexa/ 
in/nikhilbhardwaj/path/alexa/AlexaStarterApplicationModule.class 
in/nikhilbhardwaj/path/alexa/AlexaStarterSpeechlet.class 
in/nikhilbhardwaj/path/alexa/AlexaStarterSpeechletRequestStreamHandler.class 
in/nikhilbhardwaj/path/route/model/Constants.class 
in/nikhilbhardwaj/path/route/resources/ 
in/nikhilbhardwaj/path/route/resources/ServicesResource.class 
in/nikhilbhardwaj/path/route/resources/StopsResource.class 
in/nikhilbhardwaj/path/route/resources/StopTimesResource.class 
in/nikhilbhardwaj/path/route/resources/TripsResource.class 
log4j.properties 
path-nj-us/ 
path-nj-us/agency.txt 
path-nj-us/calendar.txt 
path-nj-us/calendar_dates.txt 
path-nj-us/fare_attributes.txt 
path-nj-us/fare_rules.txt 
path-nj-us/feed_info.txt 
path-nj-us/frequencies.txt 
path-nj-us/route_directions.txt 
path-nj-us/routes.txt 
path-nj-us/shapes.txt 
path-nj-us/stop_times.txt 
path-nj-us/stops.txt 
path-nj-us/timetable_stop_order-new.txt 
path-nj-us/timetables-new.txt 
path-nj-us/transfers.txt 
path-nj-us/trips.txt 

我真的很感激任何指針,以弄清楚發生了什麼在這裏失蹤或一般提示繼續調試。這是我寫的第一個aws lambda函數,所以我完全有可能隱藏文檔中的重要內容或不知道要查找什麼。

我已經添加了一些調試代碼並運行它,它看來,資源不可用 -

URL r = this.getClass().getClassLoader().getResource("path-nj-us/calendar.txt"); 
if (r != null && r.getFile() != null) { 
    System.out.println("Resource file name" + r.getFile()); 
} else { 
    System.out.println("Resource not found in classpath"); 
} 

這將打印Resource not found in classpath。我也嘗試了this.getClass().getResource()不變的結果。

我會嘗試發佈到aws forums以查看是否存在此行爲的已知原因或者是否從類路徑中讀取文件是不允許的。

回答

1

只有兩個目錄被awsLambda用戶代碼訪問。

  1. 在/ var /任務
  2. /tmp目錄

所有波長的職能保留在/ var上的源代碼/任務

在你的代碼

,使用此路徑的任何人而不是當前目錄路徑「path-nj-us」。像這樣

URL r = this.getClass().getClassLoader().getResource("/tmp/calendar.txt"); 
if (r != null && r.getFile() != null) { 
    System.out.println("Resource file name" + r.getFile()); 
} else { 
    System.out.println("Resource not found in classpath"); 
} 
+0

感謝您的回答Abdul,但是path-nj-us不是當前的工作目錄。它是我的jar中包含資源的目錄。 – nikhil

+0

我也試過這個,但是我沒有找到所有這些路徑 - /var/task/path-nj-us/calendar.txt /var/task/path-nj-us/calendar.txt/tmp/calendar。 txt和/tmp/path-nj-us/calendar.txt – nikhil