JavaFX and maven: NullPointerException: Location is required

后端 未结 6 1879
别跟我提以往
别跟我提以往 2020-11-28 07:03

I have been trying to get Maven set up with JavaFX. Even though I was unexperienced with Maven and JavaFX, I didn\'t expect it to be so much of a challenge. My Java knowledg

相关标签:
6条回答
  • 2020-11-28 07:26

    I had the same issue with Intelij and Gradle.

    Steps to fix:

    1.Move file

    sample.fxml

    to path

    \src\main\resources\fxml

    1. Set path on this file:

    Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml"));

    0 讨论(0)
  • 2020-11-28 07:30

    my case is installed the javafx sdk to C:\Program Files\Java\openjfx-11.0.2_windows-x64_bin-sdk.

    And root cause is the the space contained in the sdk path name.

    Fixed by install the javafx sdk to folder which name without space, such as C:\javafx-sdk-11.0.2\lib

    0 讨论(0)
  • 2020-11-28 07:31

    Make sure that your sample.fxml is in the src/main/resources/ directory (or a subdirectory). Then you can access the file like this:

    Parent root = FXMLLoader.load(getClass().getClassLoader().getResource("sample.fxml"));
    

    Explanation: During the compile phase all resources and classes get copied to target/classes/. So your fxml file resides in this directory and your class in a subdirectory regarding its package name. If you call getClass().getResource("sample.fxml"); the file will be searched relative to the class file which will be this directory: target/classes/sample/.

    Calling .getResource() on the classloader sets the relative search path to target/classes/ and therefore your file gets found.

    P.S. You could also write:

    Parent root = FXMLLoader.load(getClass().getResource("/sample.fxml"));
    
    0 讨论(0)
  • 2020-11-28 07:40

    As @Absurd-Mind already explained, maven will not copy any resource files (like fxml) which resides under the src directory.

    If you want to keep the fxml files besides the java source files, you can use the maven resource plugin to copy them:

    <build>
        ...
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**/*.fxml</include>
                </includes>             
            </resource>
        </resources>
        ...
    </build>
    
    0 讨论(0)
  • 2020-11-28 07:49

    For those who use gradle as their build system add this to your build.gradle file:

    sourceSets.main.resources {
        srcDirs = ["src/main/java"]; //assume that your java classes are inside this path
        exclude "**/*.java"
    }
    

    Then clean and rebuild your project.

    So what will it do? If you have a view.fxml inside your com.example.myfxapp package, after building your project it will exported to <build dir>/resources/com/example/myfxapp/view.fxml

    0 讨论(0)
  • 2020-11-28 07:50

    For me I had took care of following things -

    1. Make sure fxml file is placed where your main class resides. And then load it using this - (In my case, TechJFX.fxml is the file)

      FXMLLoader loader = new FXMLLoader(getClass().getResource("TechJFX.fxml"));
      
    2. Add opencv-*.jar to your project folder (In my case opencv-300.jar)

    3. Add the jar to the build path using

    Right click on the project folder -> Build Path -> Configure Build Path -> Libraries Tab -> Add External JARS and locate the jar

    Also, under Order and Export -> Tick opencv-300.jar and JRE System Library(if not already ticked)

    1. Copy opencv_java*.dll (In my case opencv_java300.dll) to the same location where you jar is in the project folder.

    2. Add the following method to your main class

       static{ 
              System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
         }
      
    3. Done.

    Hope this helps someone facing similar issue.

    0 讨论(0)
提交回复
热议问题