Cannot run main class file from console but runs from Intellij - java

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...

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

Related

JavaFX "Error: JavaFX runtime components are missing, and are required to run this application" when trying to run fx in vscode

I'm working on a project with a couple friends and we're using vscode so that we can use git efficiently (it's for a school project so IDE is non-negotiable, don't tell me to use a different one) and we're having an issue where neither of them can run this javafx application that I've got in there for testing right now. I was having trouble running it at first and was getting the same error, but I fixed it this morning and now any javafx file I put in there will run fine for me, as long as I have the correct vmargs in the json. However, they're both getting this error when they try to run:
Error: JavaFX runtime components are missing, and are required to run this application
It doesn't say which components are missing or how to fix it which is annoying, but I'm kind of at a loss for what to do. I was having this error, then I added vmArgs to the json to load the proper modules, and it fixed the issue. One thing I've noticed is that when they try to run the file on their end, the json adds new configurations without them telling it to. Even if they delete it, it comes back. But this doesn't happen for me. What am I missing to fix this?
you can run from cmd/terminal with:
cd to ur jar directory
and run:
java -jar --module-path /path/to/javafx --add-modules=javafx.controls(or whatever jars u need from the fx lib) filename.jar

How to save Java Project from IntelliJ IDEA to Git repo so it will build on another PC after cloning?

I'm building a simple calculator app from a tutorial/class on Pluralsight (Java Fundamentals class), which starts with a Main.java file, and adds a class file later, MathEquation.java.
I created a Git repo to track my progress, and uploaded to Github: JavaFundamentalsClass. I used GitIgnore.IO to find a Java IntelliJ .gitignore file, and everything seemed to be working fine on the original PC I was using.
However, after switching over to a different PC, with a fresh install of IDEA, I cloned down the repo from Github, and found that the project would not build and/or run. I get this error:
"C:\Program Files\RedHat\java-1.8.0-openjdk-1.8.0.191-1\bin\java.exe" -Dfile.encoding=windows-1252 -jar ""
Error: Unable to access jarfile
Process finished with exit code 1
I suspect that there is something in the .gitigore file that is keeping some of the project, dependency, or build information from being saved in the repo that keeps IDEA from having all the info it needs to build and run the Main.java correctly.
Can anyone help? You should be able to download the project from the repo linked above in it's currently broken state, including the .gitignore file.
Additional Info
I wiped my repo clean and recloned and I forgot, originally it doesn't even give me the run button (it is grayed out). I had to do a procedure with "Add Configuration" from a search I did for that to show up to get the error above.
Also, I did install java from the OpenJDK, rather than oracle. I figured with Oracle cutting off commercial development maybe start the switch now as I'm learning, but maybe that's the issue (normal Java on my other PC).
It's working, but...
First thing - Intellij needs to know where Java is. It appears that if the SDK/JDK is not installed prior to Intellij, you will need to tell it where Java is after installing. Or if you use OpenJDK instead of Oracle Java SDK. You can do that at View/Open Library Settings, under Platform Settings / SDKs (select the JDK home path).
Then, after setting up Java, I am able to get everything working in Intellij IDEA if I find the Main.java file, right-click, and choose "Run main.main()". This runs the program okay, and also creates a Main configuration in the Run/Debug configuration area, and finally the "Run" button becomes available.
So I can get it working, but if I wipe all the files and clone it fresh from the remote repo, I have to go through running the Main.java file directly again to recreate the configuration.
So, this still doesn't answer my original question. Why doesn't this configuration get saved in the repo? What file is this information saved in? Is there something in the .gitignore file that is keeping this information from being saved to the repo?
Look like you have a different java version on your second machine.
First of all, check your java version
Update the configuration of your project, right now it pointing to java 1.8
https://github.com/LightCC/JavaFundamentalsClass/blob/master/.idea/misc.xml
I think it might be because of java not installed on the other PC you were trying. If it is installed just check whether the path mentioned has java executable file.
The first part is ensuring Java is setup, and that Intellij IDEA knows where Java is, per the other answers, comments, and addendums to the question.
The second part is setting up the run/debug configuration, per the "It's working but.." section of the question. This can be created automatically by right-clicking the file that has the main class to be run (usually Main()...), and selecting the "Run Main.main()" option (replacing Main and main() with the file and function which needs to be run)
The final piece is that to get the run/debug configurations to save to the repo, you either need to:
Share the workspace.xml file (i.e. make sure this is not in the .gitignore file). However, there is a lot of user-specific stuff in that file, so it should normally be excluded from the repo.
Or, edit the configuration and checkmark the "Share" box in the upper right-hand corner (just right of the "Name" field). This will put the configuration into a separate folder inside the .idea folder, which should not be excluded from the repo by .gitignore.

Java Eclipse - Exporting issue

I have currently finished my java project in eclipse and now I would like to export it as an executable .jar. The strange thing is, that my program is working fine, when I run it in eclipse, but, unfortnuately, it is not working when I export it.
What should happen:
Well, I have set up a GlobalMouseListener with system-hook-2.5.jar. When I run it as an executable .jar I don't get any input from the listener.
Library is included (I'm working with system-hook-2.5.jar). Is this a problem? It is not the first time exporting a Java Project for me.
Any suggestions, what I could try?
It is always a good idea to start the program via console
Type in
java -jar <Filename>
I got an exception, that causes some problems.

Error: Could not find or load main class sample.Main

I tried troubleshooting this problem for about 4 hours. Nothing worked.
I am using Java and Intellij and JavaFX.
What do I want to do?
I have created a JavaFX programm on Windows.
I want to execute this .jar on my Raspberry Pi.
Whenever I want to execute this jar I get this error in the console:
Error: Could not find or load main class sample.Main
sample is the package and Main is my class with the main method.
The programm works JUST FINE under windows. (In and outside of IntelliJ).
What steps did I already do?
Java is up-to-date on my pi. java -version returns 1.8. So we are good here.
I have imported my used libraries (Pi4J) in my project under project-structure -> libraries. & under modules -> dependencies.
I have defined a new artifact. I set a main Class (sample.Main) & under JavaFX I set the application class to sample.Main (both are given to me by Intellij) I then build the project by going to build -> build artifacts.
I checked the MANI-FEST under /src/META-INF/MANI-FEST and it indeed has
Main-Class: sample.Main
On my pi I went to the direction in which the jar file is. Then I ran: sudo java -jar Keimgeraet.jar which should work just fine. But then I get the error.
I probably have done more but forgot it already...
What might work but I don't know how I would to that:
In other threads people mentioned that my specified paths could be wrong on another computer (in this case the pi)
Here are some pictures [imgur] how everything is set up. Maybe that helps.
I will gladly try any answer! I am not the most experienced with java and the pi but I am not a complete starter.
Sidequestion: How can I include all the libraries in one final jar file?
If you need any more details feel free to ask!
How can I create an executable JAR with dependencies using Maven?
This should solve at once your main class declaration (in archive.manifest.mainclass in the maven plugin) and your inclusion of all libraries in the final jar (this will build two JARs: pay attention to take the one "xxx-jar-with-dependencies.jar").
If it still doesn't work with this Maven plugin, let us know.

Trying to develop mods for minecraft, but can't get the SetupdecompWorkspace.bat to work

First of all, this is my first ever post on stack exchange so let me know if this belongs in a different section.
Anyway, I've been wanting to get into modding minecraft so I downloaded JDK, Forge MDK and eclipse neon. After exacting files to a separate folder, I created a path variable and a Setup Workspace.bat file. In that file I typed
gradlew setupDecompWorkspace && gradlew eclipse
After this, I ran it and it got to 56%, or decompilemc. It stayed there for about 6 minutes before the console crashed. I assumed there was an error so I researched it and tried to fix it by adding more RAM to the gradle.properties. That didn't work either, so I continued researching for about an hour, and finally found something that made it go pass the decompilemc, only to break again at recompilemc
Have any of you ever encountered this problem? If so, how did you fix it? Any answer is greatly appreciated.
If this is your first time starting a Forge Workspace, make sure you have the "gradlew.bat" file, then simply shift-click your home directory and click "Open command window here". Then type:
gradlew.bat setupDecompWorkspace eclipse
Or if you're using Intellij (Which I suggest you use over Eclipse):
gradlew.bat setupDecompWorkspace idea
That should start the process of generating your Workspace.
Surprisingly useful is the fact that it can run multiple commands from one command statement. Meaning that you can type both "setupDecompWorkspace" and "eclipse"/"idea" and it will run both separately one after the other.

Categories

Resources