'Cannot run main class file from console but runs from Intellij

I'm a beginner programmer working on my checkers game and I happen to have a problem which seems impossible to overcome. It's probably something obvious and stupid but I've searched for days to get answer and still got nothing.

The problem is I can build and run my application without any problems in IDE (IntelliJ 2021.3.2) but when I try to launch it from console like this: PS D:\Programiki\Java\polish-draughts\target\classes> java com.polishdraughts.PolishDraughtsWindowed

I get this -> Error: Could not find or load main class com.polishdraughts.PolishDraughtsWindowed Caused by: java.lang.NoClassDefFoundError: javafx/application/Application

Does that mean it doesn't see required dependencies in classes folder? Are they required to be there? If so, how do I force IntelliJ to include them in target/classes? I don't understand why it runs in IDE using play button but doesn't want to start in powershell. Console version of this app, before it included any external dependencies was running from console just fine but now its the same problem. Is my module-info.java file wrong, dependencies don't get exported where they should or what is going on here? In pom.xml i have not specified any scope so it should be "compile" by default according to documentation. I'm totally clueless at this point. I tried to change external libraries to be dunked into lib folder but that didn't change anything. Tried invalidating cache, removing .idea folder, creating new project from scratch and copying just src content over, reloading libraries using "reload project"/"generate sources and update project". Removing iml file (now its completely gone and it doesn't seem to change anything - still app runs perfectly fine in IDE). Maybe I'm launching it wrong?? Please help...

Project file structure



Solution 1:[1]

You need to have all the required libraries on the classpath of the JVM.

When you run your JavaFX application in Intellij, and click on the 'Console' you'll see a line that starts /usr/lib/jvm/java-17-openjdk-amd64/bin/java (or wherever your Java installation is). You may need to click on an ellipsis (...) to see the full line.

The line will look something like:

/usr/lib/jvm/java-17-openjdk-amd64/bin/java -Dfile.encoding=UTF-8 -classpath /home/tgdavies/.m2/repository/org/openjfx/javafx-controls/17.0.1/javafx-controls-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-graphics/17.0.1/javafx-graphics-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-base/17.0.1/javafx-base-17.0.1.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-fxml/17.0.1/javafx-fxml-17.0.1.jar -p /home/tgdavies/dev/scratch/javafxdemo/target/classes:/home/tgdavies/.m2/repository/org/openjfx/javafx-controls/17.0.1/javafx-controls-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-base/17.0.1/javafx-base-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-fxml/17.0.1/javafx-fxml-17.0.1-linux.jar:/home/tgdavies/.m2/repository/org/openjfx/javafx-graphics/17.0.1/javafx-graphics-17.0.1-linux.jar -m com.example.javafxdemo/com.example.javafxdemo.HelloApplication

Of course the details will be different for your project. This command line will allow you to run your application.

In PowerShell, this works for me (surrounding parameters containing \ with " characters: C:\Users\tgdavies\.jdks\openjdk-17.0.1\bin\java.exe "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\lib\idea_rt.jar=55078:C:\Program Files\JetBrains\IntelliJ IDEA 2021.3\bin" "-Dfile.encoding=UTF-8" "-classpath" "C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-controls\17.0.1\javafx-controls-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-graphics\17.0.1\javafx-graphics-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-base\17.0.1\javafx-base-17.0.1.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-fxml\17.0.1\javafx-fxml-17.0.1.jar" -p "C:\Users\tgdavies\IdeaProjects\winjavafx\target\classes;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-controls\17.0.1\javafx-controls-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-fxml\17.0.1\javafx-fxml-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-base\17.0.1\javafx-base-17.0.1-win.jar;C:\Users\tgdavies\.m2\repository\org\openjfx\javafx-graphics\17.0.1\javafx-graphics-17.0.1-win.jar" -m com.example.winjavafx/com.example.winjavafx.HelloApplication

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1