Учитывая следующий код:
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
private Logger LOG = Logger.getLogger(Main.class.getName());
@Override
public void start(Stage primaryStage) throws Exception {
URL url = Main.class.getResource("session.fxml");
System.out.println("Value URL = " + url);
FXMLLoader.load(Main.class.getResource("session.fxml"));//errors here
//Scene scene = new Scene(FXMLLoader.load(Main.class.getResource("session.fxml")));
}
Методы FXMLLoader.load кажутся ошибочными, несмотря на то, что getResource правильно генерирует ожидаемый URL.
Трассировка стека длинная, но ошибка источника:
Caused by: java.lang.ClassNotFoundException: SessionController
at java.net.URLClassLoader.findClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
at java.lang.ClassLoader.loadClass(Unknown Source)
at javafx.fxml.FXMLLoader$ValueElement.processAttribute(FXMLLoader.java:920)
... 22 more
Что может означать эта ошибка? Что не так с кодом, вызывающим ошибку?
Кстати: session.f xml находится в той же папке, что и этот код, поэтому проблема с методом getResource () отсутствует.
Спасибо
РЕДАКТИРОВАТЬ: Чтобы ответить на комментарий Клеопатры, вот полный исходный код для файла f xml и код, который должен вызвать его:
F XML:
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Open Source Software published under the Apache Licence, Version 2.0.
-->
<?import javafx.geometry.Insets?>
<?import javafx.scene.control.Accordion?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.SplitPane?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.control.TitledPane?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.BorderPane?>
<?import javafx.scene.layout.HBox?>
<BorderPane prefHeight="500.0" prefWidth="700.0" xmlns="http://javafx.com/javafx/8.0.112" xmlns:fx="http://javafx.com/fxml/1" fx:controller="SessionController">
<center>
<AnchorPane>
<children>
<TabPane tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<tabs>
<Tab text="N-Queens Puzzle">
<content>
<SplitPane fx:id="splitWordList" dividerPositions="0.65">
<items>
<AnchorPane>
<children>
<BorderPane fx:id="mainWordPane" styleClass="mainWordPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
<center>
</center>
<bottom>
</bottom>
<top>
<Label fx:id="noSolutionLabel" style="-fx-font: 24 arial; -fx-label-padding: 5px" text="No solution found!" visible="false" />
</top>
<bottom>
<HBox alignment="CENTER" prefHeight="45.0" prefWidth="448.0" spacing="15.0" BorderPane.alignment="CENTER">
<children>
<Label text="Number of Queens" />
<TextField fx:id="queens" prefHeight="39.0" prefWidth="90.0" text="8" />
</children>
<BorderPane.margin>
<Insets />
</BorderPane.margin>
</HBox>
</bottom>
</BorderPane>
</children>
</AnchorPane>
<AnchorPane>
<children>
<Accordion layoutX="-8.0" prefHeight="450.0" prefWidth="250.0">
<panes>
<TitledPane animated="false" text="Hill Climbing">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="hillClimbingBtn" layoutX="40.0" layoutY="108.0" mnemonicParsing="false" onAction="#runHillClimbing" prefWidth="162.0" text="Run Hill Climbing" />
<TextField id="hcIterations" fx:id="hcIterations" layoutX="15.0" layoutY="41.0" prefHeight="39.0" prefWidth="218.0" text="50000" />
<Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Simulated Annealing">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="simulatedAnnealingBtn" layoutX="12.0" layoutY="227.0" mnemonicParsing="false" onAction="#runSimulatedAnnealing" prefWidth="225.0" text="Run Simulated Annealing" />
<Label layoutX="23.0" layoutY="1.0" text="Max Number of Iterations" />
<Label layoutX="23.0" layoutY="67.0" text="Temperature" />
<Label layoutX="23.0" layoutY="133.0" text="Cooling Factor" />
<TextField id="saIterations" fx:id="saIterations" layoutX="12.0" layoutY="28.0" text="50000" />
<TextField id="saTemperature" fx:id="saTemperature" layoutX="14.0" layoutY="94.0" text="120" />
<TextField id="saCoolingFactor" fx:id="saCoolingFactor" layoutX="12.0" layoutY="160.0" text="0.95" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" text="Local Beam Search">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="localBeamSearchBtn" layoutX="20.0" layoutY="167.0" mnemonicParsing="false" onAction="#runLocalBeamSearch" text="Run Local Beam Search" />
<TextField id="lbIterations" fx:id="lbIterations" layoutX="12.0" layoutY="38.0" text="50000" />
<TextField id="lbStates" fx:id="lbStates" layoutX="12.0" layoutY="119.0" text="5" />
<Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" />
<Label layoutX="12.0" layoutY="84.0" text="Number of States" />
</children>
</AnchorPane>
</content>
</TitledPane>
<TitledPane animated="false" layoutX="10.0" layoutY="88.0" text="Genetic Algorithm">
<content>
<AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
<children>
<Button id="geneticAlgorithmBtn" layoutX="24.0" layoutY="219.0" mnemonicParsing="false" onAction="#runGeneticAlgorithm" text="Run Genetic Algorithm" />
<Label layoutX="24.0" layoutY="77.0" text="Mutation Probability" />
<Label layoutX="24.0" layoutY="145.0" text="NumberOfGenerations" />
<TextField id="gaGenerations" fx:id="gaGenerations" layoutX="15.0" layoutY="172.0" text="50000" />
<TextField id="gaMutationProbability" fx:id="gaMutationProbability" layoutX="14.0" layoutY="106.0" text="0.20" />
<Label layoutX="22.0" layoutY="6.0" text="Initial Population Size" />
<TextField id="gaInitialPopulationSize" fx:id="gaInitialPopulationSize" layoutX="14.0" layoutY="36.0" text="10" />
</children>
</AnchorPane>
</content>
</TitledPane>
</panes>
</Accordion>
</children>
</AnchorPane>
</items>
</SplitPane>
</content>
</Tab>
</tabs>
</TabPane>
</children>
</AnchorPane>
</center>
и вот код, который намеревается вызвать его (и f xml, и код в одном каталоге):
package algo;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.net.URL;
import java.util.logging.*;
/**
* Created by mrsfy on 27-Mar-17.
*/
public class Main extends Application {
public static void main(String[] args) {
launch(args);
}
private Logger LOG = Logger.getLogger(Main.class.getName());
@Override
public void start(Stage primaryStage) throws Exception {
//Main.class.getResource("session.fxml");
URL url = Main.class.getResource("session.fxml");
System.out.println("Value URL = " + url);
try {
FXMLLoader.load(Main.class.getResource("session.fxml"));
} catch (Exception e) {
System.out.println(e.toString());
}
//Scene scene = new Scene(FXMLLoader.load(Main.class.getResource("session.fxml")));
//primaryStage.setScene(scene);
//primaryStage.setHeight(675);
//primaryStage.setWidth(753);
//primaryStage.setResizable(false);
//primaryStage.show();
}
}