Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/matlab/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 为项目添加maven支持会导致图像加载/路径使用混乱_Java_Image_Maven_Netbeans_Resources - Fatal编程技术网

Java 为项目添加maven支持会导致图像加载/路径使用混乱

Java 为项目添加maven支持会导致图像加载/路径使用混乱,java,image,maven,netbeans,resources,Java,Image,Maven,Netbeans,Resources,我有一个项目,我搬到了maven,因为它使我很容易处理依赖项,在我完成构建工作后,我的执行停止在从我的一个包加载资源(映像)时。我迷失了,因为没有maven,在旧构建中一切都很好 我加载图像 primStage.getIcons().add(new Image(OpenChannel_Dynamic_Downloader.class .getResourceAsStream(Info.Resource.OCPI)));

我有一个项目,我搬到了maven,因为它使我很容易处理依赖项,在我完成构建工作后,我的执行停止在从我的一个包加载资源(映像)时。我迷失了,因为没有maven,在旧构建中一切都很好

我加载图像

   primStage.getIcons().add(new Image(OpenChannel_Dynamic_Downloader.class
                                    .getResourceAsStream(Info.Resource.OCPI)));
此处指定了资源位置

public static final String OCPI = "/openchannel_dynamic_downloader/resources/images/openChannelPlaceholderIcon.png";
我的整个包结构是一样的,所有的名字都是一样的

我得到的例外是:

Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Input stream must not be null
    at javafx.scene.image.Image.validateInputStream(Image.java:1110)
    at javafx.scene.image.Image.<init>(Image.java:694)
    at openchannel_dynamic_downloader.application.OpenChannel_Dynamic_Downloader.lambda$showLoginWindow$10(OpenChannel_Dynamic_Downloader.java:233)
    at com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191)
    at java.lang.Thread.run(Thread.java:745)
线程“JavaFX应用程序线程”java.lang.NullPointerException中的异常:输入流不能为null 位于javafx.scene.image.image.validateInputStream(image.java:1110) 在javafx.scene.image.image.(image.java:694) 在openchannel\u dynamic\u downloader.application.openchannel\u dynamic\u downloader.lambda$showLoginWindow$10(openchannel\u dynamic\u downloader.java:233) 位于com.sun.javafx.application.PlatformImpl.lambda$null$174(PlatformImpl.java:295) 位于java.security.AccessController.doPrivileged(本机方法) 位于com.sun.javafx.application.PlatformImpl.lambda$runLater$175(PlatformImpl.java:294) 位于com.sun.glass.ui.invokelateDispatcher$Future.run(invokelateDispatcher.java:95) 在com.sun.glass.ui.win.WinApplication.\u runLoop(本机方法) 位于com.sun.glass.ui.win.WinApplication.lambda$null$149(WinApplication.java:191) 运行(Thread.java:745) 问题行是第一个被截取的代码。以前有人见过这个问题吗?当我输入maven时,我必须改变我如何使用路径吗?我对整个maven来说都是新手,所以如果你能帮我解决这个问题,我会很高兴,因为我希望它不会出现问题

这是我的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>com.bisciak</groupId>
    <artifactId>OpenChannel_Dynamic_Downloader_MavenEnabled</artifactId>
    <version>0.1</version>
    <packaging>jar</packaging>

    <name>OpenChannel_Dynamic_Downloader_MavenEnabled</name>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <mainClass>openchannel_dynamic_downloader.application.OpenChannel_Dynamic_Downloader</mainClass>
    </properties>

    <organization>
        <!-- Used as the 'Vendor' for JNLP generation -->
        <name>Tomas Bisciak's software</name>
    </organization>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <version>2.6</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>unpack-dependencies</goal>
                        </goals>
                        <configuration>
                            <excludeScope>system</excludeScope>
                            <excludeGroupIds>junit,org.mockito,org.hamcrest</excludeGroupIds>
                            <outputDirectory>${project.build.directory}/classes</outputDirectory>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>1.2.1</version>
                <executions>
                    <execution>
                        <id>unpack-dependencies</id>

                        <phase>package</phase>
                        <goals>
                            <goal>exec</goal>
                        </goals>
                        <configuration>
                            <executable>${java.home}/../bin/javafxpackager</executable>
                            <arguments>
                                <argument>-createjar</argument>
                                <argument>-nocss2bin</argument>
                                <argument>-appclass</argument>
                                <argument>${mainClass}</argument>
                                <argument>-srcdir</argument>
                                <argument>${project.build.directory}/classes</argument>
                                <argument>-outdir</argument>
                                <argument>${project.build.directory}</argument>
                                <argument>-outfile</argument>
                                <argument>${project.build.finalName}.jar</argument>
                            </arguments>
                        </configuration>
                    </execution>
                    <execution>
                        <id>default-cli</id>
                        <goals>
                            <goal>exec</goal>                            
                        </goals>
                        <configuration>
                            <executable>${java.home}/bin/java</executable>
                            <commandlineArgs>${runfx.args}</commandlineArgs>
                        </configuration>
                    </execution>
                </executions>  
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArguments>
                        <bootclasspath>${sun.boot.class.path}${path.separator}${java.home}/lib/jfxrt.jar</bootclasspath>
                    </compilerArguments>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.16</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>${java.home}/lib/jfxrt.jar</additionalClasspathElement>
                    </additionalClasspathElements>
                </configuration>
            </plugin>
        </plugins>
    </build>
    <dependencies>
        <dependency>
            <groupId>org.controlsfx</groupId>
            <artifactId>controlsfx</artifactId>
            <version>8.40.10</version>
        </dependency>
        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <version>1.4.190</version>
        </dependency>
        <dependency>
            <groupId>net.java.openjfx.backport</groupId>
            <artifactId>openjfx-78-backport</artifactId>
            <version>1.8.0-ea-b96.1</version>
        </dependency>
        <dependency>
            <groupId>commons-validator</groupId>
            <artifactId>commons-validator</artifactId>
            <version>1.4.1</version>
        </dependency>
        <dependency>
            <groupId>com.1stleg</groupId>
            <artifactId>jnativehook</artifactId>
            <version>2.0.2</version>
        </dependency>
    </dependencies>



</project>

4.0.0
com.bisciak

使用Maven时,所有资源都应放在
src/main/resources
下。所谓资源,我指的是应该打包到最终JAR中的每个文件,即应该出现在类路径中的每个文件

由于您正在使用
Class.getResourceAsStream()
访问图像,因此图像必须位于该文件夹下


因此,您应该将资源移动到
src/main/resources
下。图像的最终位置是:
src/main/resources/openchannel\u dynamic\u downloader/resources/images/openChannelPlaceholderIcon.png

当使用MavenJavaFx时,我更喜欢以这种方式加载图像:
1) 将图像放入
src/main/resources/images/image.png

2) 创建
javafx.scene.image.image
对象:

ClassLoader classLoader = getClass().getClassLoader();
String imageUrl = classLoader.getResource("images/image.png").toExternalForm();
Image image = new Image(imageUrl);