我有以下問題。我在路徑爲src/resources
的文件夾中有圖像。現在我想在JavaFX應用程序中使用它們。Maven JavaFX資源ClassLoader InputStream null
ClassLoader classLoader = getClass().getClassLoader();
InputStream markerInputStream = classLoader.getResourceAsStream("/img/marker.gif");
SurfaceImage surfaceImage = null;
try {
surfaceImage = new SurfaceImage(ImageIO.read(markerInputStream),sector);
} catch (IOException e)
{e.printStackTrace();}
這是堆棧跟蹤:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1348)
at map.map.setMarkerPosition(main.java:184)
at map.map$1.mouseClicked(main.java:151)
at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
at java.awt.Component.processMouseEvent(Component.java:6538)
at java.awt.Component.processEvent(Component.java:6300)
at java.awt.Component.dispatchEventImpl(Component.java:4891)
at java.awt.Component.dispatchEvent(Component.java:4713)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
我不知道那裏的錯誤是我所測試過的路徑/resources/marker.png
只有/marker.png
在pom.xml的這個模塊是接下來一個。
<artifactId>application</artifactId>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>2.5.3</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
</plugins>
<resources>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.fxml</include>
</includes>
</resource>
</resources>
</build>
<dependencies>
<dependency>
<...>
</dependency>
<dependency>
<groupId>gov.nasa</groupId>
<artifactId>worldwind</artifactId>
<version>2.0.0-986</version>
</dependency>
<dependency>
<groupId>com.metsci.glimpse</groupId>
<artifactId>glimpse-extras-worldwind</artifactId>
<version>2.1.2</version>
</dependency>
</dependencies>
</project>
你能幫我嗎?
打開JAR查看內容(即'jar tf your.jar | grep gif')。你的GIF(或者是PNG,你的代碼使用'.gif'你的文本寫了'.png')?它是否在你期望的道路上?可能你需要爲Maven程序集插件的'include'標籤添加'**/* .gif,**/*。png'嗎? – haraldK