线程“main”中的异常 java.lang.ClassNotFoundException: sample.Main - 为啥?

Posted

技术标签:

【中文标题】线程“main”中的异常 java.lang.ClassNotFoundException: sample.Main - 为啥?【英文标题】:Exception in thread "main" java.lang.ClassNotFoundException: sample.Main - why?线程“main”中的异常 java.lang.ClassNotFoundException: sample.Main - 为什么? 【发布时间】:2016-02-26 17:12:39 【问题描述】:

我在编译时收到此错误,我不知道为什么。错误日志也没有说明太多。已经搜索过其他答案,它们都与我不使用或建议确保文件名与类名相同的框架有关(是的,都名为“FolderSyncer4”)。参考代码:

FolderSyncer4.java

package sample;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.DirectoryChooser;
import javafx.stage.Stage;
import java.io.*;
import java.nio.file.Path;
import java.util.LinkedList;

public class FolderSyncer4 extends Application 

    final String FOLDER_SYNCER = "FolderSyncer";

    Stage theStage;

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


    @Override
    public void start(Stage primaryStage) throws Exception 
        theStage = primaryStage;

        //TODO do the FXML stuff, hope this works
        //Parent root = FXMLLoader.load(getClass().getResource("FolderSyncerMainWindow.fxml"));

        //Display the "Selecting" scene at first
        theStage.setTitle(FOLDER_SYNCER);
        theStage.show();
    

FolderSyncerMainWindowController.java

package sample;

import javafx.scene.control.Button;
import javafx.scene.control.Label;

import java.io.IOException;

public class FolderSyncerMainWindowController 

    final String FOLDER_SYNCER = "FolderSyncer";
    final String BROWSE = "Browse";
    final String SOURCE = "Source...";
    final String TARGET = "Target...";
    final String COMPARE = "Compare";
    final String CANCEL = "Cancel";
    final String SYNCHRONIZE = "Synchronize";
    final String COMPARING = "Comparing, this may take several minutes.";
    final String SYNCHRONIZING = "Synchronizing, this may take several minutes.";
    final String DONE = "Done.";

    public Label sourceLabel = new Label(SOURCE);


FolderSyncerMainWindow.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.effect.*?>
<?import javafx.geometry.*?>
<?import javafx.scene.control.*?>
<?import java.lang.*?>
<?import javafx.scene.layout.*?>
<?import javafx.geometry.Insets?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.control.Button?>
<?import javafx.scene.control.Label?>

<BorderPane prefHeight="574.0" prefWidth="687.0" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
fx:controller="sample.FolderSyncerMainWindowController">
   <padding>
      <Insets bottom="8.0" left="8.0" right="8.0" top="8.0" />
   </padding>
   <left>
      <HBox prefHeight="492.0" prefWidth="523.0" spacing="8.0" BorderPane.alignment="CENTER">
         <children>
            <TreeView prefHeight="509.0" prefWidth="321.0" HBox.hgrow="ALWAYS" />
            <TreeView prefHeight="509.0" prefWidth="324.0" HBox.hgrow="ALWAYS" />
         </children>
      </HBox>
   </left>
   <top>
      <VBox prefHeight="47.0" prefWidth="702.0" spacing="8.0" BorderPane.alignment="CENTER">
         <children>
            <HBox prefHeight="8.0" prefWidth="702.0" spacing="8.0">
               <children>
                  <Label fx:id="sourceLabel" prefHeight="17.0" prefWidth="44.0">
                     <HBox.margin>
                        <Insets />
                     </HBox.margin>
                  </Label>
                  <TextField editable="false" prefHeight="25.0" prefWidth="543.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="61.0" text="Browse" />
               </children>
               <VBox.margin>
                  <Insets />
               </VBox.margin>
            </HBox>
            <HBox prefHeight="45.0" prefWidth="702.0" spacing="8.0">
               <children>
                  <Label prefHeight="17.0" prefWidth="44.0" text="Target" />
                  <TextField editable="false" prefHeight="25.0" prefWidth="543.0" HBox.hgrow="ALWAYS" />
                  <Button mnemonicParsing="false" prefHeight="25.0" prefWidth="61.0" text="Browse" />
               </children>
            </HBox>
         </children>
         <BorderPane.margin>
            <Insets bottom="8.0" />
         </BorderPane.margin>
      </VBox>
   </top>
   <right>
      <GridPane prefHeight="492.0" prefWidth="140.0" BorderPane.alignment="CENTER">
        <columnConstraints>
          <ColumnConstraints halignment="CENTER" hgrow="SOMETIMES" minWidth="10.0" />
        </columnConstraints>
        <rowConstraints>
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
            <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <effect>
            <DropShadow />
         </effect>
         <BorderPane.margin>
            <Insets left="8.0" />
         </BorderPane.margin>
         <children>
            <Button fx:id="compareButton" mnemonicParsing="false" text="Compare" />
            <Button fx:id="synchronizeButton" mnemonicParsing="false"  text="Synchronize" GridPane.rowIndex="1" />
         </children>
      </GridPane>
   </right>
</BorderPane>

错误日志:

Exception in thread "main" java.lang.ClassNotFoundException: sample.Main
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:122)

Process finished with exit code 1

【问题讨论】:

您是在告诉 IntelliJ 运行课程 sample.Main。但是您的班级改为sample.FolderSyncer4。在 IntelliJ 运行配置中更改要运行的类的名称。 @Jesper 为什么不将其发布为答案? 【参考方案1】:

java -jar xxx.jar sample.FolderSyncer4 应该可以工作。看来你改用sample.Main

【讨论】:

以上是关于线程“main”中的异常 java.lang.ClassNotFoundException: sample.Main - 为啥?的主要内容,如果未能解决你的问题,请参考以下文章

我的代码上的线程“main”java.util.NoSuchElementException 中的异常?

如何修复运行时错误-线程“main”java.util.NoSuchElementException中的异常

HTTPClient 示例 - 线程“main”中的异常 java.lang.NoSuchFieldError: INSTANCE

为啥我在代码中的线程“main”java.lang.StringIndexOutOfBoundsException 错误中收到异常?

线程“main”中的 Java 异常 java.lang.StringIndexOutOfBoundsException 错误

线程“main”中的异常 java.lang.ExceptionInInitializerError (Clojure)