2013-11-27 53 views
0

我是用可視化開發簡單的java應用程序。我必須用maven把它打包成jar。我的項目結構是:
SRC
---主要
-------- java的
----------- com.chess
------- - 資源
-----------圖像
可執行jar無法看到圖像

我試圖使用像:Images \ image.jpg但它的缺失。我如何使用圖像?

這是我的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0  http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
<modelVersion>4.0.0</modelVersion> 

<groupId>ChessApp</groupId> 
<artifactId>ChessApp</artifactId> 
<version>1.0-SNAPSHOT</version> 
<packaging>jar</packaging> 

<build> 
    <plugins> 
     <plugin> 
      <artifactId>maven-assembly-plugin</artifactId> 
      <configuration> 
       <descriptorRefs> 
        <descriptorRef>jar-with-dependencies</descriptorRef> 
       </descriptorRefs> 
       <archive> 
        <manifest> 
         <mainClass>com.chess.Main</mainClass> 
        </manifest> 
       </archive> 
      </configuration> 
      <executions> 
       <execution> 
        <phase>package</phase> 
        <goals> 
         <goal>single</goal> 
        </goals> 
       </execution> 
      </executions> 
     </plugin> 
    </plugins> 
</build> 

+0

你在罐子裏手動看過嗎?你如何訪問圖像? –

+0

我試圖訪問像:「Images \\ images.jpg」它將圖像文件夾與com和META-INF文件夾放在一個圖層 – Srw

+0

然後,您需要顯示您如何訪問圖像。你應該使用類加載器。這與maven無關。 –

回答

1

通過您的評論,

layout.setIcon(new ImageIcon("Images\\BlackRockAndBlackBoard.jpg")); 

的ImageIcon將字符串作爲文件名,你需要採取甘德在Get a resource using getResource()

然後使用url來構造ImageIcon

URL imageUrl = YourClassName.class.getResource("/Images/ImageName.jpg"); 
layout.setIcon(new ImageIcon(imageUrl)); 

的URL被允許作爲構造函數的參數,你可以在這裏看到http://docs.oracle.com/javase/7/docs/api/javax/swing/ImageIcon.html#ImageIcon%28java.net.URL%29

+0

是的,這是答案。在jar中選擇「main」類的其他方法有哪些?沒有插件。 – Srw

+0

@Srw,你可以通過使用jar工具來編輯你的manifest.MF,如果需要的話可以重新簽名。如果你有另一個問題,你應該在SO上提出這個問題作爲一個新問題。 –