Error: Could not find or load main class MainKt in IntelliJ - intellij-idea

I'm using IntelliJ.
I can run Java or Kotlin project without any issue but
I can't build Kotlin-JS project
I have the error Error: Could not find or load main class MainKt
My IntelliJ information
IntelliJ IDEA 2018.2.6 (Community Edition)
Build #IC-182.5107.16, built on November 6, 2018
JRE: 1.8.0_152-release-1248-b22 amd64
JVM: OpenJDK 64-Bit Server VM by JetBrains s.r.o
Linux 4.19.0-041900-generic
File tree
File tree
My main file contain just :
fun main(args: Array<String>) {
val message = "Hello JavaScript!"
println(message)
}
Run Menu
Run menu
I tried the option $KOTLIN_BUILDED$, $MODULE_WORKIN_DIR$, and to put the path manually but it didn't change anything.
When I'm hovering MainKt with my mouse, a tooltip appear "Class 'MainKt' is in the default package" so IntelliJ see the class.
My project settings seem to be ok
Project Structure part 1
Project Structure part 2
Project Structure part 3
I also tried to
Delete the *.iml file
Reinstalle IntelliJ
Re add the main
Invalidate cache and restarted
Thanks you in advance

The "Kotlin" run configuration runs Kotlin/JVM. To run a Kotlin/JS program as a command-line program, you need to make sure you have node.js installed. Then you can either use IntelliJ IDEA Ultimate with the node.js plugin installed (in which case you will see a "run" icon in the gutter that will produce the correct run configuration automatically), or use Gradle to build and run your program from the command line.

Your main file need to be .kt
You need to create it like this: src ->New ->Cotlin File/Class->File.
NOT like this: src ->New ->File.

Related

How build EXE from TornadoFX JAR?

I have a simple JavaFX/TornadoFX project on Windows that builds in a JAR and runs correctly from under IntelliJ IDEA and in the JRE installed on my computer. I want to make it so that this project can run on any Windows machine without JRE installed. To do this, in IntelliJ, I created an artifact "JavaFx application" and configured it as it is written in the instructions for TornadoFX.
project settings
project settings
The executable file builds successfully, but when I try to run it I get the error "No method main in class com/example/demo/app/MyApp."
I tried to add main to the project code, as it is written in the instructions for TornadoFX, but I cannot specify this method in the project settings.
project settings
Only the MyApp class can be added automatically, but of course it does not contain the main method.
project settings
What am I doing wrong and how can I make an executable file under Windows?
My code in MyApp.kt:
package com.example.demo.app
import com.example.demo.view.MainView
import tornadofx.App
import tornadofx.launch
class MyApp: App(MainView::class, Styles::class)
fun main(args: Array<String>) {
launch<MyApp>(args)
}
Once again I carefully re-read the manual for TornadoFx and saw the important phrase: "Notice the Kt at the end.". Thus, in the project settings in the Application class field, you need to add "Kt" by hand. I did it and it worked out as I wish.

JavaFX can't build artifact - fx:deploy is not available in this JDK

I have a JavaFX project that I would like to build as a Jar-file. However, when I attempt to do so, I get an error.
Error:Java FX Packager: Can't build artifact - fx:deploy is not available in this JDK
I found a similar problem on here from last year, but it seemed like they concluded nothing.
This happens because either you have many JDKs installed and compiling by another and running by another or you are using the Javafx Application jar feature when creating artifacts in Intellij which is unfortunately broken. Before proceeding with the below steps make sure that you are compiling with and running with the same JDK version.
Here is you fix it:
1 - Create a Launcher class:
The Launcher class is going to call the main JavaFx class from which your appliaction runs. Choosing to make the Jar directly through the Main class is going to error out giving the following error:
Error: Could not find or load main class Main
Caused by: java.lang.ClassNotFoundException: Main
Your Launcher class should look something like this:
public class Launcher {
public static void main(String[] args) {
MainGUI.main(args);
}
}
2 - On to building the Jar
You probably still have a META-INF folder from the previous build so delete it.
Build the project as a JAR:
File->Project Structure -> Artifacts -> "+" -> JAR-> from modules with dependancies..
Choose the Launcher class for you main and check "copy to the output directory and link via Manifest" and Press Ok
Press Apply then OK.
go to Build -> Build artifacts-> Rebuild
In the JetBrains website I found a good article about, Package JavaFX applications which was really helpful. In the #troubleshoot section it says that,
Error:Java FX Packager: Can't build artifact – fx:deploy is not available in this JDK
The fx:deploy task was a part of the Ant plugin that was formerly
distributed in ant-javafx.jar as a part of Java Packager. The Ant
plugin is not included in jpackage in the current JDK versions.
If you're using a JDK build of version 9 and later, use third-party
solutions for packaging. For example, refer to section Runtime images
in the JavaFX official documentation.

How to get IntelliJ to associate Gradle sources with build.gradle?

When writing Gradle scripts for my Java project, specifically, when writing build.gradle files, IntelliJ does not recognize the Gradle API.
For instance, Gradle methods calls like apply, dependencies configure appear with a black line under them and it is not possible to navigate to method declarations, there is no auto-completion etc.
I managed to work around this by adding compile gradleApi() to the build's dependencies block. However, I don't want to have this explicit dependency in my code.
I tried editing IntelliJ's project structure and add a dependency on a Gradle library (tried gradle-core and gradle-all) to my modules, but that seems to have no effect.
Is there a way to make IntelliJ associate all build.gradle files with the Gadle sources?
I solved this problem as follows:
As mention in already posted answers, configure gradle
update gradle/wrapper/gradle-wrapper.properties file
change bin to all in distributionUrl i.e.
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-bin.zip
to
distributionUrl=https\://services.gradle.org/distributions/gradle-6.8.3-all.zip
OR
[optional] If you are using old version of gradle wrapper and wanted to upgrade, then execute
./gradlew wrapper --gradle-version 6.8.3 --distribution-type all
Update gradle task (if present in build file)
wrapper {
gradleVersion = '6.8.3'
distributionType = Wrapper.DistributionType.ALL
}
Before importing the project to IntelliJ-Idea IDE, update build.gradle and add java and idea plugin to the plugins list
plugins {
id "java-library"
id "idea"
}
From a terminal, execute ./gradlew clean build idea or simply ./gradlew idea
Import project to IntelliJ idea.
Go to Preferences --> build,Execution,Deployment --> BuildTools --> Gradle
You can see
Restart IntelliJ idea IDE.
So above we have configured both of the options so choose either of them, except the specified location option. That's it.
Before
After
Autocomplete functionality as mentioned in this answer.
I had similar frustrations with Grails 3, which defines and runs a wrapper task when an app is created. Changing to the "all" zip in the wrapper properties file did not work because this kept getting changed back to the "bin" zip.
This was solved when it was understood that the "gradle-wrapper.properties" file simply stores the values from the "wrapper" task, and if this task is run after the properties are changed, they get changed right back.
This is easily fixed by setting some properties on the wrapper task:
wrapper.gradleVersion='3.2.1'
wrapper.distributionType=Wrapper.DistributionType.ALL
Now importing the project into IDEA gives you smart editing of your build.gradle.
when I choose build.gradle in IDEA and open it, IDE prompts
You can configure Gradle wrapper to use distribution with sources. It will provide IDE with Gradle API/DSL documentation.
I choose Ok, apply suggestion!
after project refreshing I am able to use code completion
before you import your project, configure it to use the customizable gradle wrapper as per the instructions here :-
https://docs.gradle.org/current/userguide/gradle_wrapper.html
add a task to your top level project like this:-
task wrapper(type: Wrapper) {
println "Wrapper gradleVersion = '2.12'"
gradleVersion = '2.12'
}
or whatever the latest version is.
make sure you can build the project from the gradle command line before you try importing into intelliJ, using the ./gradlew command, which will download and install a gradle distribution for you the first time you build.
set your java home, intelliJ home and gradle home variables in your machine and in intelliJ (mine look like this, yours may be different depending on your setup and your history of hacking around your machine...:-
(from .bashrc
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.8.0_45.jdk/Contents/Home
)
When you do import, choose the customisable gradle wrapper. if all is well, when you open the top level build.gradle for your project, you will be asked to configure sources for the gradle dsl, which will also update your gradle wrapper properties file to this:-
#Thu Mar 31 14:04:00 BST 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.12-all.zip
.. the change being from ... bin.zip to all.zip. and that's it. This had been giving me lots of grief for a long time, but that's the way to do it. (on IntelliJ IDEA 2016.1 CE at least...)
most of this was in
Dimitry's answer too, but I couldn't get it to work using the default wrapper , it had to be the customisable wrapper.

IntelliJ 14 + gradle issue with system properties in test

This is snippet from my build.gradle file:
test{
systemProperty "test", "test"
}
Then I have my test code which looks like this:
#org.junit.Test
public void test(){
Assert.assertEquals("test", System.getProperty("test"));
}
And when I run this test from command line it is passing. If I choose in gradle window task test and click right mouse button and then select run this test also pass. However when I go to test itself and select run on method name it does not pass. It looks like in the third case IntelliJ completely ignored gradle context. Is there something that can be done to make this test work when running directly from IDE? Thanks in advance for reply. Gradle version 2.3.3 and IntelliJ 14.0.2
IntelliJ doesn't currently understand such configuration. To fully solve this category of problems, IntelliJ will need to use Gradle as its underlying build/execution engine, like Android Studio already does today. Until this happens (or support for this specific use case is added to IntelliJ), you'll have to define the same system property in the IntelliJ run configuration (template).
If you use the IntelliJ project generation approach (gradle idea), you can (with some effort) make Gradle generate such a run configuration (template). This is something we do for Gradle's own build.
For IntelliJ you need to add your System Property -Dtest=test to Gradle VM options within Settings > Build, Execution, Deployment > Build Tools > Gradle

How to define Gradle's home in IDEA?

I am trying to import a Gradle project into IntelliJ, and when I get to the Gradle Home textbox, it is not automatically populated, nor will typing in the path of Gradle Home result in a valid location - I have the GRADLE_USER_HOME environment variable set (to what I think is!) the correct path, and I have been able to successfully import this same project into Eclipse. Any suggestions?
You can write a simple gradle script to print your GRADLE_HOME directory.
task getHomeDir {
doLast {
println gradle.gradleHomeDir
}
}
and name it build.gradle.
Then run it with:
gradle getHomeDir
If you installed with homebrew, use brew info gradle to find the base path (i.e. /usr/local/Cellar/gradle/1.10/), and just append libexec.
The same task in Kotlin in case you use build.gradle.kts:
tasks.register("getHomeDir") {
println("Gradle home dir: ${gradle.gradleHomeDir}")
}
Installed on a Mac via Homebrew, the path
/usr/local/opt/gradle/libexec
is preferable to
/usr/local/Cellar/gradle/X.X/libexec
since the former will survive version upgrades.
If you installed gradle with homebrew, then the path is:
/usr/local/Cellar/gradle/X.X/libexec
Where X.X is the version of gradle (currently 2.1)
If you are using IntelliJ, just do the following.
Close the project
(re)Open the project
you will see "Import gradle project" message on the right bottom. click.
select "Use default gradle wrapper". not "Use local gradle distribution"
That's all.
This is what helped me solve the problem of not having Gradle home set for the IDEA when importing a Gradle project.
THREE OPTIONS -- (A) Default Wrapper (B) "gradle 'wrapper' task configuration" OR (C) "local gradle distribution" defined by jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
A. Default Wrapper (recommended)
If you are able, select this recommended option. If it is grayed out, see option C, which should then set your default for all subsequent projects.
B. Gradle 'Wrapper' Task Configuration
If you want IDEA to define your gradle version for you from your build script
Set this option if you define your gradle build versions as a task within your actual gradle build.
Example below from jetbrains: https://www.jetbrains.com/help/idea/gradle-settings.html
(useful if you do not want to share gradle builds between projects)
C. Local Gradle Distribution
1. Run the following command to get gradle location:
brew info gradle (if gradle was installed with homebrew)
2. You are looking for something like this:
/usr/local/Cellar/gradle/4.8.1
3. Next, append 'libexec' to the gradle location you just found:
/usr/local/Cellar/gradle/4.8.1/libexec
This is because "libexec is to be used by other daemons and system utilities executed by other programs" (i.e. IDEA). Please see https://unix.stackexchange.com/questions/312146/what-is-the-purpose-of-usr-libexec
4. Finally, put that new path in the Gradle home input box if IDEA prompts you.
IDEA should now have allowed you to hit OK
C:\Users\<_username>\.gradle\wrapper\dists\gradle-<_version>-all\<_number_random_maybe>\gradle-<_version>
\Android studio\gradle didn't worked for me.
And "Default gradle wrapper" wasn't configured while importing (cloning) the project from bitbucket
If it causes problem to figure out the path, here is my path :
C:\Users\prabs\.gradle\wrapper\dists\gradle-5.4.1-all\3221gyojl5jsh0helicew7rwx\gradle-5.4.1
This is instruction for MAC only.
I had the same problem. I solved it by configuring $GRADLE_HOME in .bash_profile. Here's how you do it:
Open .bash_profile (usually it's located in the user’s home directory).
Add the following lines to update $PATH variable:
export GRADLE_HOME=/usr/local/opt/gradle/libexec
export PATH=$GRADLE_HOME/bin:$PATH
Save it.
Apply your changes by running
source .bash_profile
I wrote my own article with instruction in a case if somebody will encounter the same problem.
On a mac it should ideally be at : /Applications/Android Studio.app/Contents/gradle/gradle-2.14.1
(Replace the version string with the latest)
AFAIK it is GRADLE_HOME not GRADLE_USER_HOME (see gradle installation http://www.gradle.org/installation).
On the other hand I played a bit with Gradle support in Idea 13 Cardea and I think the gradle home is not automatically discover by Idea. If so you can file a issue in youtrack.
Also, if you use gradle 1.6+ you can use the Graldle support for setting the build and wrapper. I think idea automatically discover the wrapper based gradle project.
$ gradle setupBuild --type java-library
$ gradle wrapper
Note: Supported library types: basic, maven, java
Regards
If you're using MacPorts, the path is
/opt/local/share/java/gradle
I had to setup the Project SDK before selecting gradle path. Once that was set correctly, I had to choose "Use default gradle wrapper (recommended) in "Import Project from Gradle" dialog.
Still works if I remove gradle using brew:
$ brew remove gradle
This is where my gradle home is (Arch Linux):
/usr/share/java/gradle/
I had some weird errors where it could not find my class, I had to right click on my src folder (was red) to "Make Directory as" -> Source Folder Root
Click New -> Project from existing sources -> Import gradle project...
Then Idea recognized gradle automatically.
I couldn't get it to accept my Gradle JVM selection until I deleted a broken JDK
Th window below is from File -> Other Settings -> Structure For New Projects...
I had a red 1.8 JDK SDK entry here, once I deleted that Gradle JVM error below disappeared and I could move on to the next step
In case you are using Mac, most probably your gradle home should be /usr/local/gradle-2.0 for example.
In preference of IDEA search for gradle and set gradle home as given above.
It should work