Sample JavaFX Project - Error: Could not find or load main class sample.Main Follow
Answered
So I just created a new sample JavaFX project from New > Project > JavaFX > JavaFX Application.
Nothing added. Nothing changed.
Clicking on Run > Run Main gives the error `Error: Could not find or load main class sample.Main`.
Any help?
Please sign in to leave a comment.
What if you are using JDK 8 and you still have the same issue. It not hard to reproduce. Just install IntelliJ with all default settings, create a JavaFX program, Click Run. You get this error.
"C:\Program Files (x86)\Java\jdk1.8.0_201\bin\java.exe" -javaagent:C:\Users\steve\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\183.5429.30\lib\idea_rt.jar=61987:C:\Users\steve\AppData\Local\JetBrains\Toolbox\apps\IDEA-U\ch-0\183.5429.30\bin -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_201\jre\lib\rt.jar" sample.Main
Error: Could not find or load main class sample.Main
If you click on Configuration to set the Main class: (by default sample.Main is where main exists). But you can't select it. What is going on.
> 2. Set that directory in Project compiler output:
Structure for the new project doesn't affect the settings of the current project, use the different dialog: https://www.jetbrains.com/help/idea/project-structure-dialog.html .
This scenario is mentioned in the document I've linked: https://stackoverflow.com/a/42660624/104891.
.idea/modules.xml
file references invalid module file nameduntitled104.iml
. Fix the module name manually or create a project from scratch and don't use a template.Try using JDK 8.
See https://stackoverflow.com/a/52470141/104891 if you want to use Java 11.
If the issue persists, attach a sample project to reproduce via https://intellij-support.jetbrains.com/hc/articles/206869619.
Please see https://stackoverflow.com/a/42660624/104891.
Share a sample project if the issue persists.
I have the same problem.
I made 2 steps:
1. Creat directory "out" in your Project.
2. Set that directory in Project compiler output:
File -> Other Settings -> Structure forNew Projects...
Then Browse to directory "out" that just create.
Push Apply and Ok buttons.
On Windows 10, and I run into this every single time that I try to make a JavaFX application in Intellij. I'm using Java 11, following the instructions given at https://www.jetbrains.com/help/idea/javafx.html#add-javafx-lib .
Just using the default "Hello World" starter, after having included the javafx library under project structure-> Libraries, and editing the Run/Debug Configurations to include the appropriate VM arguments {--module-path "C:\Program Files\Java\javafx-sdk-11.0.2\lib" --add-modules javafx.controls,javafx.fxml}, I get java.lang.ClassNotFoundException: sample.Main
Every single time. I have no idea what I could be possibly be doing wrong, given that I replicate this problem with the default setup every time.
Any help understanding what is happening here is much appreciated.
See https://stackoverflow.com/a/42660624/104891 .
I would also try forward slashes here as the backslashes are used as the escape characters:
Can you run other apps that are not JavaFX like a new hello world Java class? If it doesn't help, please share the logs (Help | Collect Logs and Diagnostic Data) and the sample project at https://uploads.jetbrains.com.
I see now that it is doing it for other plain command line apps as well like hello world. Here is an id for a log upload: 2020_11_22_UCQgGTgcRyQQoXhy
I also see in the .idea/modules.xml, it says this, which I'm not sure whether if it is an issue or not:
Thanks Serge, you're a rockstar man!