2016-12-29 107 views
0

我创建JavaFX应用程序,并一直在使用它旁边的Maven的框架,所以很自然发现,我把sample.fxml在资源文件夹,例如: enter image description hereJavaFX的:无效的URL或资源不与Maven

然而,即使我的代码,以达到实际上就是以前的项目中,它的工作的一个碳拷贝,在这种情况下它拒绝接受一个FXML文件存在于该位置:

public class Main extends Application { 

    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     Parent root = FXMLLoader.load(getClass().getResource("/fxml/sample.fxml")); 
     primaryStage.setTitle("Hello World"); 
     primaryStage.setScene(new Scene(root)); 
     primaryStage.show(); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 

它吐出出:

Exception in thread "main" java.lang.RuntimeException: Exception in Application start method 
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: javafx.fxml.LoadException: 
/C:/Users/David/IdeaProjects/FXRpg/target/classes/fxml/sample.fxml 

    at javafx.fxml.FXMLLoader.constructLoadException(FXMLLoader.java:2601) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2579) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3214) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104) 
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097) 
    at sample.Main.start(Main.java:13) 
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863) 
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326) 
    at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191) 
    ... 1 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL: Invalid URL or resource not found 
    at javafx.scene.image.Image.validateUrl(Image.java:1118) 
    at javafx.scene.image.Image.<init>(Image.java:620) 
    at sample.Controller.initialize(Controller.java:31) 
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2548) 
    ... 17 more 
Caused by: java.lang.IllegalArgumentException: Invalid URL or resource not found 
    at javafx.scene.image.Image.validateUrl(Image.java:1110) 
    ... 20 more 

的POM文件并没有改变任何事情之一:

<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>groupId</groupId> 
    <artifactId>JavaFxApplication</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.apache.maven.plugins</groupId> 
       <artifactId>maven-compiler-plugin</artifactId> 
       <configuration> 
        <source>1.8</source> 
        <target>1.8</target> 
       </configuration> 
      </plugin> 
     </plugins> 
    </build> 
    <dependencies> 
     <dependency> 
      <groupId>org.mongodb</groupId> 
      <artifactId>mongo-java-driver</artifactId> 
      <version>3.0.2</version> 
     </dependency> 
    </dependencies> 
</project> 
+0

如何你开始了吗? 你能检查一下target/classes/fxml是否包含sample.fxml吗? 如果没有,那么maven没有编译它:)(IntelliJ可能会把它放入/退出) –

+0

在pom中是否有值得发布的东西,就像你重新定义资源文件夹一样? – weston

+0

详细答案在这里: [如何在资源文件夹中引用javafx fxml文件?](http://stackoverflow.com/questions/19602727/how-to-reference-javafx-fxml-files-in-resource-folder) –

回答

0

如由@Robert共享链接的详细答案,路径似乎是装载机正确的,好像你的sample.fxml不是。所以如果你打算在这些步骤

FXMLLoader loader = new FXMLLoader(); 
loader.setLocation(getClass().getResource("/main.fxml")); 
Parent content = loader.load(); 

分裂你的资源的加载部分,你会发现失败是在load而不是在getResource。可能是调试它的正确方法。另外要看看.fxml有什么问题,需要查看文件内容。

来源 - How to reference javafx fxml files in resource folder?

0

现在我很好奇。我已经在IntelliJ:Maven,fmxl子目录中构建了项目,就像问题一样。以下Main.java工作。

public class Main extends Application { 

@Override 
public void start(Stage primaryStage) throws Exception{ 
    Parent root = FXMLLoader.load(getClass().getResource("fxml/sample.fxml")); 
    primaryStage.setTitle("Hello World"); 
    primaryStage.setScene(new Scene(root)); 
    primaryStage.show(); 
} 

public static void main(String[] args) { 
    launch(args); 
} 

}

如果我提出的资源文件,我得到一个不同的错误信息:

Caused by: java.lang.NullPointerException: Location is required. 
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207) 

以及物品是否完整:该POM

<?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>JavaFXApplication</groupId> 
<artifactId>JavaFXApplication</artifactId> 
<version>1.0-SNAPSHOT</version>