Can't get path to resource

2019-09-01 07:07发布

问题:

I am trying to use Scala macro annotations with this library.

I have a schema file at src/main/resources/avsc/FriendRequestAcceptedGson.avsc

I am trying to refer to it in my code as follows:

@AvroTypeProvider("/avsc/FriendRequestAcceptedGson.avsc")
@AvroRecord
case class FriendRequestAcceptedGson()   

This is the error I get:

[error] /home/lee/Code/bigdata-friends/java/etl/src/main/scala/com/mxit/bd/friends/etl/Runner.scala:64: exception during macro expansion: [error] java.io.FileNotFoundException: /avsc/FriendRequestAcceptedGson.avsc (No such file or directory) [error] at java.io.FileInputStream.open(Native Method) [error] at java.io.FileInputStream.<init>(FileInputStream.java:146) [error] at org.codehaus.jackson.JsonFactory.createJsonParser(JsonFactory.java:504) [error] at org.apache.avro.Schema$Parser.parse(Schema.java:922) [error] at com.julianpeeters.avro.annotations.util.SchemaParser$.getSchema(SchemaParser.scala:20) [error] at com.julianpeeters.avro.annotations.AvroTypeProviderMacro$.impl(TypeProviderMacro.scala:23) [error] @AvroTypeProvider("/avsc/FriendRequestAcceptedGson.avsc")

回答1:

Resources are compiled into your JAR file. You must find them with a ClassLoader, not in the file system. The java.io.FIle class only knows how to look in the file system. Now, if you're trying to open the source file (which may not be present when the JAR is deployed), it will depend on where your current working directory is relative to the file. Probably it is at the root so the path you need to use is the full relative one: src/main/resources/avsc/FriendRequestAcceptedGson.avsc



标签: scala