问题描述
我试图在 netbeans 中运行我的 Java 类中的一些代码片段,而 netbeans 似乎始终无法识别应用程序类(无论代码片段如何),并且每当我尝试时都会抛出一个空的 Browse JavaFX Application Classes 窗口构建或运行应用程序。
这是我目前正在尝试运行的代码:
package examplereaderusingfilereader;
import java.io.Reader;
import java.io.FileReader;
public class ExampleReaderUsingFileReader {
public static void main(String[] args) {
//creates an array of character
char[] array = new char[100];
try {
//creates a reader using the FileReader
Reader input = new FileReader("input.txt");
//checks if reader is ready
System.out.println("Is there data in the stream? " + input.ready());
//Reads characters
input.read(array);
System.out.println("Data in the stream");
System.out.println(array);
//closes the reader
input.close();
} catch(Exception e) {
e.getStackTrace();
}
}
}
解决方法
尝试添加 e.printStackTrace();
也许您有没有这样的文件或目录
您需要做的就是使用“应用程序”扩展您的主类。像这样修改你的主类和主函数
public class ExampleReaderUsingFileReader extends Application {
@Override
public void start(Stage primaryStage) throws Exception{
// Your snippet code here
Scene scene = new Scene(new AnchorPane());
//create your fxml file on the same folder of main if not exist
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
}
}