简体   繁体   English

如何从具有已编译的groovy类的jar中获取资源?

[英]How to getResource from jar with compiled groovy classes?

I have the groovy script. 我有时髦的脚本。 I compiled it with groovyc and jar'ed classes. 我用groovyc和jar'ed类进行了编译。 So script1.class is in jar's root. 因此script1.class在jar的根目录中。 And I put some file foo.bar in jas's root too. 我也将文件foo.bar放在jas的根目录中。 How do I get that file with getResource? 如何使用getResource获取该文件? Which ClassLoader should I use? 我应该使用哪个ClassLoader? And how do I get this ClassLoader? 以及如何获得该ClassLoader?

Use a groovy class which exists in the same package where the resource is placed. 使用与资源放置在同一包中的常规类。

def path = 'foo.txt'
def inputStream = Script1.class.getResourceAsStream(path)

The sample code above, allows you to access the file named foo.txt which is placed in the same package in which Script.class exists. 上面的示例代码允许您访问名为foo.txt的文件,该文件位于Script.class存在的同一程序包中。 You need to manipulate the stream as per your need. 您需要根据需要操纵流。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM