Why can I not convert Java file to Kotlin file in IntelliJ? - kotlin

I want to create a Gradle multi-project which will have no source code. This project will then have one subproject, and it will have a single Java main function that has been generated by the IntelliJ IDE. My intent is to convert a large Java console application to Kotlin, so here, I am creating a simple console application, and attempting (but failing) to convert the simple Java main file to Kotlin.
My computer is running Ubuntu 22.04. I am performing this exercise on IntelliJ IDEA 2022.3.1 (Community Edition). When I type gradle -- version into a CLI window, I get the following response:
------------------------------------------------------------
Gradle 7.2
------------------------------------------------------------
Build time: 2021-08-17 09:59:03 UTC
Revision: a773786b58bb28710e3dc96c4d1a7063628952ad
Kotlin: 1.5.21
Groovy: 3.0.8
Ant: Apache Ant(TM) version 1.10.9 compiled on September 27 2020
JVM: 16.0.1 (Private Build 16.0.1+9-Ubuntu-120.04)
OS: Linux 5.15.0-56-generic amd64
To reduce complexity for those who might want to reproduce what I did, I named the root project "untitled" and it's single subproject "untitled1". These values were the defaults presented to me by IntellJ. I start IntelliJ, and, because I didn't start it from a previous session, IntelliJ displays the "Welcome to IntelliJ IDE" window. From this window, I chose New Project.
A second window labelled "New Project" is opened. I select the "New Project" item from the left hand menu items. So I can keep things simple for those who might want to reproduce this problem, I name the project "untitled". The location of this project can be anywhere the reproducer of this problem might like it to be. The language I chose is Java. The build system is Gradle. The JDK is version "java.version 17.0.5". The Gradle DSL is Kotlin. I chose NOT to add sample code for this project because this project will be the root project to a multi-project build. I left the Group and Artifact ID fields with their default values: "org.example" and "untiled" for my IntelliJ installation respectively. Finally, I ask IntelliJ to create this project.
IntelliJ creates this project, and responds with a display that contains, in a left hand view panel, the project files. To the right of this is displayed the project's build file. This window is named "build.gradle.kts(untitled)", and this file contains the following:
1 plugins {
2 id("java")
3 }
4
5 group = "org.example"
6 version = "1.0-SNAPSHOT"
7
8 repositories {
9 mavenCentral()
10 }
11
12 dependencies {
13 testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
14 testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
15 }
16
17 tasks.getByName<Test>("test") {
18 useJUnitPlatform()
19 }
Because I told IntelliJ NOT to add sample code to this project, I did not observe a directory named "src" in the project view panel under the project's name "untitled" as one should if one wanted to add sample code.
I now create the subproject. I do this by right-clicking the project name "untitled" in the project view panel. I select New > Module... from the series of menu items that appear. A dialogue box named "New Module" opens, and I select the "New Module" item from the left hand menu items. So I can keep things simple for those who might want to reproduce this problem, I name the project "untitled1". The location of this project under the project that was created before - the location chosen by the reproducer of this problem has the same directory name as the previous New Project window but with the directory "untitled" appended. The language I chose is Java. The build system is Gradle. The JDK is the same as the root project's SDK. The Gradle DSL is Kotlin. The parent project is named "untitled". This time, I DO tick the box that asks me if I want to create sample code because this project is the one in which the sample code I want to convert will be located. I left the Group and Artifact ID fields with their default values: "org.example" and "untiled1" for my IntelliJ installation respectively. Finally, I ask IntelliJ to create this subproject.
Along with the earlier created build file for the root project, IntelliJ creates this subproject, and responds by displaying the project's build file to the right of the project's view panel. This window is named "build.gradle.kts(untitled1)".
I compared each line of the subproject's build file with each from the root project and observed that the subproject's build file is identical to the the root project's.
I find it of passing interest to notice in the project's view panel that "src" directories have now been created for both the root project and its subproject. In both projects, this directory has the following subdirectory structure:
src
main
java
resources
test
In the subproject, the java directory also contains the sample source code file named "Main" under the package directory "org.example". I can open this file in the IDE, and when I do a window titled "Main.java" appears to the right of the projects view panel, containing the following code:
1 package org.example;
2
3 public class Main {
4 public static void main(String[] args) {
5 System.out.println("Hello world!");
6 }
7 }
Next, I request that the IDE convert this file into Kotlin. I do this by right-clicking on the tab of the editor's view of the source code. From the drop-down menu that appears, I select the option "Convert Java File to Kotlin File" located at the bottom of this menu. A dialogue box appears which advises me that "Kotlin is not configured in the project, and so therefore I will have to configure Kotlin in the project before performing this conversion. I chose the option labelled "OK, configure Kotlin in the project". Another dialogue box appears. From this one, I accept the the current selection of "All modules" and accept the given value of the Kotlin compiler and runtime version of "1.8.0". Finally, I click the button marked "OK".
I now observed that the build files of both projects have changed by the same amount. They both have the following contents:
1 import org.jetbrains.kotlin.gradle.tasks.KotlinCompile
2
3 plugins {
4 id("java")
5 kotlin("jvm") version "1.8.0"
6 }
7
8 group = "org.example"
9 version = "1.0-SNAPSHOT"
10
11 repositories {
12 mavenCentral()
13 }
14
15 dependencies {
16 testImplementation("org.junit.jupiter:junit-jupiter-api:5.8.1")
17 testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:5.8.1")
18 implementation(kotlin("stdlib-jdk8"))
19 }
20
21 tasks.getByName<Test>("test") {
22 useJUnitPlatform()
23 }
24 val compileKotlin: KotlinCompile by tasks
25 compileKotlin.kotlinOptions {
26 jvmTarget = "1.8"
27 }
28 val compileTestKotlin: KotlinCompile by tasks
29 compileTestKotlin.kotlinOptions {
30 jvmTarget = "1.8"
31 }
I observed that the IDE is complaining about these contents. In addition to the description I provide below, I have also attached a screen shot of the IDE to help assist the reader who may be interested this problem should the screen shot assist them.
At line 1, the package name does not resolve; specifically, the IDE says that it cannot resolve the name "jetbrains". Lines 24 and 28 appear identical: The IDE says that it cannot resolve the name "KotlinCompile" as well as advising that the tasks "Property delegate must have a 'getValue(Build_gradle, KProperty*>)' method."
I switch my editor view to the Java source file. I observed that the file has not changed. My observations about the build files have already led me to conclude that something is wrong, but I forge ahead, and so again, I select the "Convert Java File to Kotlin file" menu option from the drop-down that appears by right-clicking the tab for this editor view. The IDE responds as before; telling me that Kotlin isn't configured in the project, so I click the button that says "OK, configure Kotlin in the project". The IDE responds through a dialogue box that says "Kotlin is not configured in the project" and this is because "There are no configurators available".
So... I don't know what is happening. The only thing I think I can reliably ascertain from this exercise is that IntelliJ's claim that it can easily convert Java to Kotlin is false. How could I have gone wrong in this simple exercise?

Thanks to a comment offered by one Matt Freake, I solved the problem. I now understand the Gradle icon that appears in the top right of the build-file editor window specifies that a library is missing. This icon is actually a manual request to fetch the library from a repository. One has to instruct Gradle to do this explicitly by assenting to the request opened by clicking on the icon.
Although this process might only be a once-off, I would personally like it if the IntelliJ IDE somehow made this more obvious and removed the necessity of having to run the process twice; I would have had a more productive time on my holidays. Well, alright, I know now.

Related

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.

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

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.

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.

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:)

JavaFX source code not showing

I downloaded the JavaFx source code from http://hg.openjdk.java.net/openjfx/2.2/master/rt/summary.
The name of the folder in the zip file is re-e71070b0e0c0.
I unzipped this and added the folder under Project Structure - Global Libraries in Idea, however it doesn't work. When I try to open a class it just shows field names and /* compiled code */. I couldn't find anything in the manual.
Much of the information in this answer is now outdated.
More up-to-date information on using JavaFX with Java 11+ in IntelliJ Idea, is available at:
openjfx.io in the section titled: "JavaFX and IntelliJ".
Though, the above-linked article is more focused on using JavaFX in IntelliJ rather than viewing JavaFX library source code in IntelliJ.
These instructions are only necessary for the JavaFX 2.x branch and will be obsoleted once JDK 8 is released.
After you have downloaded the source zip and extracted it, open Idea and follow these instructions:
Modify the source structure for your 1.7 SDK:
File | Project Structure | SDKs | Sourcepath
For each sub-project in the extracted zip, type Alt+Insert and add the respective source directory. For example rt-e71070b0e0c0\javafx-ui-charts\src, rt-e71070b0e0c0\javafx-ui-common\src, etc. Not all JavaFX 2.x source code is currently open sourced, so this will only add the open sourced portions.
Set the documentation lookup path:
File | Project Structure | SDKs | Documentation Paths
Specify a url Alt+S for the documentation: http://docs.oracle.com/javafx/2/api/
In your JavaFX application source, place your cursor over a JavaFX class usage, for instance LineChart:
Press Ctrl+B to open the source code file LineChart.java from your downloaded zip.
Press Shift+F1 to open documentation on the LineChart in a browser.
Press Ctrl+Q to get quick access to documentation from in the IDE.
Update Oct 16 2013
JDK 8 now includes JavaFX source files.
To tell Idea where JavaFX sources are:
Modify the source structure for your 1.8 SDK:
File | Project Structure | SDKs | Sourcepath
Press Alt+S to add the file javafx-src.zip, which is located in the root directory of the JDK 8 distribution.
Set the documentation lookup path:
File | Project Structure | SDKs | Documentation Paths
Specify a url Alt+S for the documentation: http://download.java.net/jdk8/jfxdocs.
The JDK 8 JavaFX documentation url will likely change when JDK 8 reaches General Availability release stage.
Step 3 above provides keyboard shortcuts to quickly access the JavaFX sources and documentation from your project.
(Since this is one of the Google hits for "javafx source code", I'll dump this anwer here too):
Quoting from this answer: For Java 8, the source of the public parts of JavaFX are actually laying right along in the JDK installation as a sibling of src.zip: javafx-src.zip - on the root of your normal JDK Java 8 installation.
Eclipse doesn't by default link them up when you add the JRE (or it finds it itself) - obviously a bug - so you'll have to do that yourself, by hitting Preferences -> Installed JREs. Find your JDK 8, hit Edit button. On the list JRE system libraries, expand the jfxrt.jar node, and link in the javafx-src.zip file.
I've faced this problem on Ubuntu 16.04 with JavaFX 8 (open-jfx package). The fix was to install the missing source package:
sudo apt install openjfx-source
IntellJ started showing JavaFX source code the second after the command finished executing.