2017-03-01 20 views
0

我已經使用包含屬性文件的maven彙編創建了一個遠程jar。它從我的IDE工作。但是,在我package之後,我的應用程序無法正常工作。爪哇Guava Resources.getResource不能從遠程jar工作

我在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> 

這裏是我的代碼使用番石榴。

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; 
    } 
+0

刪除了對資源的補充配置,因爲它是默認配置。此外,爲了獲取資源,您需要設置'getResource(「/ ...」)'。 – khmarbaise

+0

現在我得到'resource /ci.properties not found.' – toy

+0

你的jar文件是否正確地包含了資源?你檢查過jar文件的內容嗎? – khmarbaise

回答

0

您是否試過SomeClassThatShouldBeInTheSameJar.class.getResource(<whatever>)Resources.getResource基本上只是一個包裝器,它使用當前線程的上下文類加載器或Resources.class本身的類加載器來查找資源。雖然在很多情況下它會起作用,但在某些情況下,您需要做一些不同的事情。