Java Guava Resources.getResource不';我不能在很远的地方工作
我已经使用maven assemble创建了一个far jar,其中包括属性文件。它在我的IDE中工作。然而,在我Java Guava Resources.getResource不';我不能在很远的地方工作,java,maven,jar,guava,spark-submit,Java,Maven,Jar,Guava,Spark Submit,我已经使用maven assemble创建了一个far jar,其中包括属性文件。它在我的IDE中工作。然而,在我打包后我的应用程序无法运行 我的所有属性文件都在src/main/resources中,我可以通过使用jar tf farjar.jar确认我在根文件夹中看到了属性文件 然而,当我从那个胖罐子运行我的程序时,我得到了这个错误 Exception in thread "main" java.lang.IllegalArgumentException: resource ci.prope
打包后
我的应用程序无法运行
我的所有属性文件都在src/main/resources
中,我可以通过使用jar tf farjar.jar
确认我在根文件夹中看到了属性文件
然而,当我从那个胖罐子运行我的程序时,我得到了这个错误
Exception in thread "main" java.lang.IllegalArgumentException: resource ci.properties not found.
at com.google.common.base.Preconditions.checkArgument(Preconditions.java:119)
at com.google.common.io.Resources.getResource(Resources.java:191)
at io.conde.config.ConfigReader.read(ConfigReader.java:23)
at io.conde.SparrowHalToS3.main(SparrowHalToS3.java:46)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.apache.spark.deploy.SparkSubmit$.org$apache$spark$deploy$SparkSubmit$$runMain(SparkSubmit.scala:736)
at org.apache.spark.deploy.SparkSubmit$.doRunMain$1(SparkSubmit.scala:185)
at org.apache.spark.deploy.SparkSubmit$.submit(SparkSubmit.scala:210)
at org.apache.spark.deploy.SparkSubmit$.main(SparkSubmit.scala:124)
at org.apache.spark.deploy.SparkSubmit.main(SparkSubmit.scala)
这是我的pom
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*</include>
</includes>
</resource>
</resources>
<plugins>
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.4.1</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>assembly</goal>
</goals>
</execution>
</executions>
</plugin>
您是否尝试过一些应该放在samejar.class.getResource()中的类
Resources.getResource
基本上只是一个包装器,它使用当前线程的上下文类加载器或Resources.class
本身的类加载器来查找资源。虽然在许多情况下它可以工作,但在某些情况下,您需要执行一些不同的操作。删除了资源的补充配置,因为它是默认配置。此外,要获取资源,您需要设置getResource(“/…”)
。现在我得到resource/ci.properties未找到。
您的jar文件是否正确包含资源?你检查过jar文件的内容了吗?
public Properties read(String environment) throws IOException {
final URL url = Resources.getResource(format("%s.properties", environment));
final ByteSource byteSource = Resources.asByteSource(url);
InputStream inputStream = null;
try {
inputStream = byteSource.openBufferedStream();
properties.load(inputStream);
return properties;
} catch (final IOException ioException) {
LOG.error(ioException.getMessage());
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (final IOException ioException) {
LOG.error(ioException.getMessage());
}
}
}
return null;
}