title: Class not found exception for FXMLLoader method load
body: <p>Given the following code:</p>
  
  <pre><code>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")));
  
  }
  </code></pre>
  
  <p>FXMLLoader.load methods seems to error despite getResource correctly produces the expected URL. </p>
  
  <p>The stack trace is long, but the source error is:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>What could this error mean? What is wrong with the code that causes the error?</p>
  
  <p>BTW: the session.fxml is in the same folder as this code, so there is no issue on the getResource() method.</p>
  
  <p>Thanks</p>
  
  <p>EDIT: To respond to Kleopatra's comment, here is the full source code for the fxml file and code that is to call it:</p>
  
  <p>FXML:</p>
  
  <pre><code>  &lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
  &lt;!--
    ~ Open Source Software published under the Apache Licence, Version 2.0.
    --&gt;
  
  &lt;?import javafx.geometry.Insets?&gt;
  &lt;?import javafx.scene.control.Accordion?&gt;
  &lt;?import javafx.scene.control.Button?&gt;
  &lt;?import javafx.scene.control.Label?&gt;
  &lt;?import javafx.scene.control.SplitPane?&gt;
  &lt;?import javafx.scene.control.Tab?&gt;
  &lt;?import javafx.scene.control.TabPane?&gt;
  &lt;?import javafx.scene.control.TextField?&gt;
  &lt;?import javafx.scene.control.TitledPane?&gt;
  &lt;?import javafx.scene.layout.AnchorPane?&gt;
  &lt;?import javafx.scene.layout.BorderPane?&gt;
  &lt;?import javafx.scene.layout.HBox?&gt;
  
  &lt;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"&gt;
      &lt;center&gt;
          &lt;AnchorPane&gt;
              &lt;children&gt;
                  &lt;TabPane tabClosingPolicy="UNAVAILABLE" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
                     &lt;tabs&gt;
  
         &lt;Tab text="N-Queens Puzzle"&gt;
                         &lt;content&gt;
                             &lt;SplitPane fx:id="splitWordList" dividerPositions="0.65"&gt;
                                 &lt;items&gt;
                                     &lt;AnchorPane&gt;
                                         &lt;children&gt;
                                             &lt;BorderPane fx:id="mainWordPane" styleClass="mainWordPane" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0"&gt;
                                                 &lt;center&gt;
  
                                              &lt;/center&gt;
                                                 &lt;bottom&gt;
                                              &lt;/bottom&gt;
                                                 &lt;top&gt;
                                                     &lt;Label fx:id="noSolutionLabel" style="-fx-font: 24 arial; -fx-label-padding: 5px" text="No solution found!" visible="false" /&gt;
  
                                                 &lt;/top&gt;
                                     &lt;bottom&gt;
                                        &lt;HBox alignment="CENTER" prefHeight="45.0" prefWidth="448.0" spacing="15.0" BorderPane.alignment="CENTER"&gt;
                                           &lt;children&gt;
                                              &lt;Label text="Number of Queens" /&gt;
                                              &lt;TextField fx:id="queens" prefHeight="39.0" prefWidth="90.0" text="8" /&gt;
                                           &lt;/children&gt;
                                           &lt;BorderPane.margin&gt;
                                              &lt;Insets /&gt;
                                           &lt;/BorderPane.margin&gt;
                                        &lt;/HBox&gt;
                                     &lt;/bottom&gt;
                                             &lt;/BorderPane&gt;
                                         &lt;/children&gt;
                                     &lt;/AnchorPane&gt;
                                     &lt;AnchorPane&gt;
                                         &lt;children&gt;
                                  &lt;Accordion layoutX="-8.0" prefHeight="450.0" prefWidth="250.0"&gt;
                                    &lt;panes&gt;
                                      &lt;TitledPane animated="false" text="Hill Climbing"&gt;
                                        &lt;content&gt;
                                          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt;
                                                 &lt;children&gt;
                                                    &lt;Button id="hillClimbingBtn" layoutX="40.0" layoutY="108.0" mnemonicParsing="false" onAction="#runHillClimbing" prefWidth="162.0" text="Run Hill Climbing" /&gt;
                                                    &lt;TextField id="hcIterations" fx:id="hcIterations" layoutX="15.0" layoutY="41.0" prefHeight="39.0" prefWidth="218.0" text="50000" /&gt;
                                                    &lt;Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" /&gt;
                                                 &lt;/children&gt;
                                              &lt;/AnchorPane&gt;
                                        &lt;/content&gt;
                                      &lt;/TitledPane&gt;
                                      &lt;TitledPane animated="false" text="Simulated Annealing"&gt;
                                        &lt;content&gt;
                                          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt;
                                                 &lt;children&gt;
                                                    &lt;Button id="simulatedAnnealingBtn" layoutX="12.0" layoutY="227.0" mnemonicParsing="false" onAction="#runSimulatedAnnealing" prefWidth="225.0" text="Run Simulated Annealing" /&gt;
                                                    &lt;Label layoutX="23.0" layoutY="1.0" text="Max Number of Iterations" /&gt;
                                                    &lt;Label layoutX="23.0" layoutY="67.0" text="Temperature" /&gt;
                                                    &lt;Label layoutX="23.0" layoutY="133.0" text="Cooling Factor" /&gt;
                                                    &lt;TextField id="saIterations" fx:id="saIterations" layoutX="12.0" layoutY="28.0" text="50000" /&gt;
                                                    &lt;TextField id="saTemperature" fx:id="saTemperature" layoutX="14.0" layoutY="94.0" text="120" /&gt;
                                                    &lt;TextField id="saCoolingFactor" fx:id="saCoolingFactor" layoutX="12.0" layoutY="160.0" text="0.95" /&gt;
                                                 &lt;/children&gt;
                                              &lt;/AnchorPane&gt;
                                        &lt;/content&gt;
                                      &lt;/TitledPane&gt;
                                      &lt;TitledPane animated="false" text="Local Beam Search"&gt;
                                        &lt;content&gt;
                                          &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt;
                                                 &lt;children&gt;
                                                    &lt;Button id="localBeamSearchBtn" layoutX="20.0" layoutY="167.0" mnemonicParsing="false" onAction="#runLocalBeamSearch" text="Run Local Beam Search" /&gt;
                                                    &lt;TextField id="lbIterations" fx:id="lbIterations" layoutX="12.0" layoutY="38.0" text="50000" /&gt;
                                                    &lt;TextField id="lbStates" fx:id="lbStates" layoutX="12.0" layoutY="119.0" text="5" /&gt;
                                                    &lt;Label layoutX="14.0" layoutY="14.0" text="Max Number of Iterations" /&gt;
                                                    &lt;Label layoutX="12.0" layoutY="84.0" text="Number of States" /&gt;
                                                 &lt;/children&gt;
                                              &lt;/AnchorPane&gt;
                                        &lt;/content&gt;
                                      &lt;/TitledPane&gt;
                                        &lt;TitledPane animated="false" layoutX="10.0" layoutY="88.0" text="Genetic Algorithm"&gt;
                                           &lt;content&gt;
                                              &lt;AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0"&gt;
                                                 &lt;children&gt;
                                                    &lt;Button id="geneticAlgorithmBtn" layoutX="24.0" layoutY="219.0" mnemonicParsing="false" onAction="#runGeneticAlgorithm" text="Run Genetic Algorithm" /&gt;
                                                    &lt;Label layoutX="24.0" layoutY="77.0" text="Mutation Probability" /&gt;
                                                    &lt;Label layoutX="24.0" layoutY="145.0" text="NumberOfGenerations" /&gt;
                                                    &lt;TextField id="gaGenerations" fx:id="gaGenerations" layoutX="15.0" layoutY="172.0" text="50000" /&gt;
                                                    &lt;TextField id="gaMutationProbability" fx:id="gaMutationProbability" layoutX="14.0" layoutY="106.0" text="0.20" /&gt;
                                                    &lt;Label layoutX="22.0" layoutY="6.0" text="Initial Population Size" /&gt;
                                                    &lt;TextField id="gaInitialPopulationSize" fx:id="gaInitialPopulationSize" layoutX="14.0" layoutY="36.0" text="10" /&gt;
                                                 &lt;/children&gt;
                                              &lt;/AnchorPane&gt;
                                           &lt;/content&gt;
                                        &lt;/TitledPane&gt;
                                    &lt;/panes&gt;
                                  &lt;/Accordion&gt;
                                         &lt;/children&gt;
                                     &lt;/AnchorPane&gt;
                                 &lt;/items&gt;
                             &lt;/SplitPane&gt;
                         &lt;/content&gt;
                     &lt;/Tab&gt;
                 &lt;/tabs&gt;
              &lt;/TabPane&gt;
          &lt;/children&gt;
      &lt;/AnchorPane&gt;
  &lt;/center&gt;
  </code></pre>
  
  <p></p>
  
  <p>and here is the code that is aiming to call it (both fxml and code in same directory):</p>
  
  <pre><code> 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();
  
  
      }
  </code></pre>
  
  <p>}</p>