Error: Could not find or load main class sample.Main when creating JavaFX project on IntelliJ - intellij-idea

I am using IntelliJ and I am trying to learn some JavaFX after covering some parts of Java. I am trying to create a simple HelloWorld application first. When I create a new project, it creates some things automatically, but there are errors and I can't run it:
Error: Could not find or load main class sample.Main
Caused by: java.lang.NoClassDefFoundError: javafx/application/Application
Process finished with exit code 1
What can I do to fix it? I tried to update IntelliJ and to update to JDK 12 and I am still getting this error.

If you're using JavaFx 11 12 or 13 then you have to import the jar files inside the lib folder of the specified javafx folder.Go to configure -> Structure for New Projects-> under the section of Platform settings go to Global Libraries hit the + sign and then browse to the lib folder of your javafx installation.Then you select all the files inside the lib folder and add them except the "src.zip" and name the library something like "Java Fx" or whatever you want.Afterwards you normally create a javafx project and you right click the project folder and go to module settings.Go again to the global libraries right click and add this library to the module.Now you can compile the code and it runs.

Related

HelloWorld in Kotlin gives error "Could not find or load main class"

I spent the last 1,5 hour trying to make this simple tutorial work in IntelliJ IDEA, as you can see in this video.
When trying to run the code, I get the error:
/[...] -Dfile.encoding=UTF-8 src.HelloKt
Error: Could not find or load main class src.HelloKt
Caused by: java.lang.ClassNotFoundException: src.HelloKt
I have tried setting up SDK, invalidating cache, removing .idea and .gradle, rebuilding project, deleting the profile and adding it again. I tried those actions in different orders.
Here's a screenshot of the project:
It also complains Kotlin is not configured, but I have already configured it.
Here's the run configuration:
Here are the project settings:
Your Hello.kt file needs to be somewhere inside the src/main folder, probably in src/main/kotlin. This is different from the tutorial, because your project is using Gradle, and the one in the tutorial isn't. I think this is because newer versions of IntelliJ use Gradle by default for new projects, which wasn't the case when the tutorial was written.
The use of src/main/kotlin and src/test/kotlin as source code directories is a convention in Gradle (and Maven). When importing a Gradle project into IntelliJ, main becomes a module, and kotlin becomes a source folder within that module. The same goes for test. In your screenshots, the bold text and blue icons on main and test confirm that's how your project is set up. Files outside of those folders aren't treated as source files, which explains why your Hello.kt file isn't being compiled or recognised correctly.
It's likely that the default behaviour of IntelliJ when creating a new project has changed since this tutorial was written. In the tutorial, they select "Kotlin" as the project type and this creates a project that doesn't use Gradle. As a result, the project doesn't use the src/main/kotlin directory structure.
I can see from your video that you selected the same option, but on the next screen, IntelliJ still automatically selected Gradle as the build system for the new project. To match the project structure used in the tutorial, I think you would need to select "IntelliJ" as the build system.

How to create an empty JavaFX project in IntelliJ

Every time I create a new JavaFX project in IntelliJ it loads a simple HelloWorld project in it. How can I make it empty instead (without the need to delete those .java files every time)? Also how can I choose to create the project without a building system (as you can see in the picture, I'm always forced to create the project with Maven or Gradle)
When creating the new project, choose "Java" instead of "JavaFX".
A JavaFX application is just a Java application, so if you don't want the additional things which IntelliJ is doing when you choose to create a JavaFX project (e.g. supplying example code and associating with a build system like Maven or Gradle), you can just choose a basic Java application project from the wizard and it won't do those other things.
See the section in openjfx.io documentation titled "JavaFX and IntelliJ IDEA" for other steps you need to take:
Set the project JDK
File -> Project Structure -> Project
Create a JavaFX library in Idea
File -> Project Structure -> Libraries
Point to the lib folder of the JavaFX SDK.
Add VM options for the module path
Run -> Edit Configurations...
--module-path /path/to/javafx-sdk-15.0.1/lib --add-modules javafx.controls,javafx.fxml
For windows use quotes around the path and \ rather than /.
Run the project
Run -> Run...
You might also need to take the actions identified in the accepted answer to:
How to convert a normal java project in intellij into a JavaFx project
But that answer was written a while back and setting the resource copy configuration to include JavaFX fxml and css files might not be needed anymore.
Now, you might think that is annoying amount of things to do, and I might agree with you.

Make the new JDK 11 java.net.http package visible in Netbeans 10

After opening an existing Netbeans 8 project in Apache Netbeans 10, and setting the Java version to the newest JDK 11, Netbeans is still unable to resolve references to the new java.net.http package which includes improved HTTP handling with classes such as HttpClient, HttpRequest, and HttpResponse.
What needs to be done to make the new java.net.http package visible to the existing project in Apache Netbeans 10?
In order to make the new java.net.http package visible to your project, you'll need to configure your project so that it includes the module name "java.net.http" (found at the top of the Javadoc page for the package).
The existing Java project imported from Netbeans 8 will not have any knowledge of the module system introduced in Java 9, so initially you'll have no way to add a module requirement. To fix this, right-click on your Java project in Apache Netbeans 10 and then select "New" and then "Java Module Info...". In the dialog which appears, check the details and click the "Next" button and then confirm that you're happy to move entries out of the classpath and into the modulepath if offered. You'll now find a new file "module-info.java" in the default package of your project (under "Source Packages"/"<default package>").
Open the "module-info.java" file and then check your project for error markers (the angry red circles on the file icon, showing that the file contains a parsing or compilation error). Open the files which report errors and you'll probably find that some of the import statements at the top of your Java files now report an error such as this:
"Package javax.xml.stream is not visible:
(package javax.xml.stream is declared in module java.xml but module MyApplication does not read it)"
This error would mean that you'd need to add the following line to the module MyApplication definition (where "MyApplication" will be a name based on your own project) found within your "module-info.java" file:
requires java.xml;
Save that change and you should now see the specific error about javax.xml.stream disappear. Repeat this process until all of the visibility errors vanish from your project. (If your project doesn't use any non-core modules then you may not see any errors at all.)
Finally, once all other visibility errors are out of the way, add this line to your module MyApplication definition:
requires java.net.http;
Save that change, and now when editing your project code in Apache Netbeans IDE 10 you should be able to see and use the new java.net.http classes such as HttpClient.

IntelliJ Play - object index is not a member of package views.html

I'm trying out IntelliJ ultimate and when I create a new play project it does not build with the following error
object index is not a member of package views.html
There is this answer
But I don't see any of the directories mentioned.
The scala files (views) were not compiled. That's why the directories show up as red.
The views can be compiled manually:
activator compile
make IntelliJ
But of course there's no reason to do this manually.
Just go to
Settings/Language and Frameworks/Play
and check Use Play 2 compiler for this project
And then Make the project

Play Framework 2.4 and IntelliJ Idea

I am trying to open a play 2.4 project in IntelliJ but since things have changed I don't know how to do this.
In previous versions I could just run
activator idea
Or use the activator UI and click on generate intelliJ project, but in 2.4 the idea command doesn't seem to exist
[error] Not a valid command: idea (similar: eval, alias)
[error] Not a valid project ID: idea
[error] Expected ':' (if selecting a configuration)
[error] Not a valid key: idea (similar: clean)
[error] idea
[error] ^
And the UI seems broken, when I click on generate intelliJ project it tries to compile the app and gives this error:
play/Play$
java.lang.NoClassDefFoundError: play/Play$
Use 'last' for the full log.
Failed to load project.
I created the project from scratch using the play java template with:
activator new
I have also tried importing the folder as a project but intelliJ doesn't seem to identify it as a project
I run into the same problem, as I used IDEA to open a project folder, it had an play 2 app in a sub folder, then I import module (play 2 app) to the system.
And it works well.
After that I have changed the module folder name, then when I run the app, it displayed:
Not a valid project ID: "project team"
I re-checked the folder, and found that in the File -> Project Structure option, the name of module is "root", and the "team" is the module for whole project (not the module imported by SBT), so apparently, the module wasn't functional after I changed the module folder name.
So I found and removed all .idea folder, which is IDEA configureation, then re-open/re-import the module, still not work. I thought it's IDEA cache issue, it do have cache for the opened project, so I changed the project folder from team to something else, clean the .idea folders, and re-open/re-import it. It worked.
If the play app is in the project folder as a sub folder, to import the module at File -> Project Structure.
The project name should be "root" when running it in IDEA. So in this case, you should rename "project team" to "root" in the name field.
Solution 1
In my case (IDEA 2018.2), I changed the lazy val variable in build.sbt, it had the name "root" when my project name was "top", changed "root" to "top".
Before:
lazy val root = (project in file(".")).enablePlugins(PlayScala)
After:
lazy val top = (project in file(".")).enablePlugins(PlayScala)
Change project variable
Solution 2
The method proposed by Tom solved my problem partially, because after rebooting the IDEA I returned the project name back, it was necessary to change the variable from "top" to "root" in build.sbt.
Before:
name: = "top"
After:
name := "root"
Intellij IDEA lets you quickly create a Play application without using a command prompt. You don’t need to configure anything outside of the IDE, the SBT build tool takes care of downloading appropriate libraries, resolving dependencies and building the project.
Before you start creating a Play application in IntelliJ IDEA, make sure that the latest Scala Plugin is installed and enabled in IntelliJ IDEA. Even if you don’t develop in Scala, it will help with the template engine and also resolving dependencies.
Basically, install Play Framework, Scala plugins and import project into Intellij as SBT project. Don't use activator to create IDEA project files. More details here.
It seems I had not updated scala/sbt to the latest version in intelliJ
Once I had done this it noticed that it was a valid project, though the docs don't seem to mention you can import it as an SBT project, just how to create it as a new sbt project (which I did not want to do as I wanted to create it via activator)
I also had the project/play-fork-run.sbt file issue
I use Intellij Idea 16.3.3 and this problem appears sometimes. How I fixed it? You just need to open your SBT plugin and under your project get the context menu then you should choose "Ignore SBT project". After a current process is finished you should turn on this option again. This is work for me:)