Can NetBeans run Kotlin? - kotlin

This is more to see if Kotlin can really work in NetBeans, even if it requires jumping through too many hoops to be practical. If I really do need to use Kotlin, I have IntelliJ.
Supposedly Kotlin can be used in Eclipse and NetBeans. It looks like you can write Kotlin in NetBeans all you want, but it's not gonna run at all.
I installed the Kotlin for NetBeans plugin, and it seems to work. It has all the hints and syntax highlighting that one would expect. But when it comes to actually running something written in Kotlin, NetBeans seems to be completely incapable. I also had trouble importing JUnit into a Kotlin test class.
But the first thing to accomplish before anything else is Hello World. I tried a couple of simple Hello Worlds. The Java Hello World runs. The Kotlin Hello World doesn't. I even tried to run it through the Scala REPL with much the same results.
From HelloWorld.kt (this is only missing the license header reminder and the generated Javadoc):
package basicexerciseskotlin
fun main(args: Array<String>) = println("Hello, world, from Kotlin!")
My first draft of this had a bunch of errors and warnings, and the hints helped me get it to this form, so now I have a green indicator. But Run > Run File is grayed out, and the keyboard shortcut does nothing.
Then I thought maybe I could call HelloWorld.main() from a Java class. To figure out how to do that, I made a similar project in IntelliJ. I copied what I came up with in IntelliJ to NetBeans, but I had to comment out the line that calls on HelloWorldKt.
From BasicExercisesKotlin.java (the main class in the project, as far as NetBeans is concerned):
package basicexerciseskotlin;
public class BasicExercisesKotlin {
public static void main(String[] args) {
System.out.println("Hello world, from Java!");
String[] kArgs = {""};
// HelloWorldKt.main(kArgs);
}
}
In IntelliJ:
/Library/Java/JavaVirtualMachines/jdk1.8.0_211.jdk/Contents/Home/bin/java ...
Hello, world, from Java!
Hello, world, from Kotlin!
Process finished with exit code 0
But NetBeans can't find the symbol HelloWorld nor HelloWorldKt.
In the Scala REPL:
scala> basicexerciseskotlin.BasicExercisesKotlin.main(null)
Hello world, from Java!
scala> basicexerciseskotlin.HelloWorldKt.main(null)
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at basicexerciseskotlin.HelloWorldKt.main(HelloWorld.kt)
... 28 elided
Caused by: java.lang.ClassNotFoundException: kotlin.jvm.internal.Intrinsics
at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 29 more
Just in case the null was a problem:
scala> val kArgs = Array("")
kArgs: Array[String] = Array("")
scala> basicexerciseskotlin.HelloWorldKt.main(kArgs)
java.lang.NoClassDefFoundError: kotlin/jvm/internal/Intrinsics
at basicexerciseskotlin.HelloWorldKt.main(HelloWorld.kt)
... 28 elided
Maybe the plugin neglected to resolve some necessary paths?
I did think about scrutinizing the bytecode generated by IntelliJ, but I don't know enough about that to make sense of it.

There are two issues about that topic which show that there is promosing progress:
initiation: https://github.com/JetBrains/kotlin-netbeans/issues/137
PR into official NetBeasn repo: https://github.com/apache/netbeans/pull/1398
The accepted PR is a good sign that it could soon land in the official list of plug ins.

Related

Geb DSL Modules unrecognized within Intellij

Going off the documentation here are Modules and other Geb DSL not expected to be recognized within a Spock Spec with IntelliJ? This makes using Geb with IntelliJ a bit cumbersome with no DSL reference. I noticed from this question that the asker did some custom work to get IntelliJ to wrap and notice the Geb DSL.
My questions is am I doing something wrong or is this expected and has there been any progress on getting the Geb DSL recognized by IntelliJ?
If not does anyone have a workaround for this issue/limitation and or another IDE that does recognized the Geb DSL?
IntelliJ has support for Geb DSL, I've been using it for years and there are even tests in IntelliJ's codebase that confirm it's there.
If you could share how your code looks like and what exactly does not work for you then maybe we can find a problem with your setup or expectations. It would also be good to know whether you are using community edition or professional edition.
Please note that the issue discussed in the question you linked to has been fixed by me in this PR and is no longer present since IntelliJ 2018.2.
EDIT:
After reading your comments under my response I now understand what the problem is. Your expectation is that IntelliJ will be able to figure out what the page type at runtime is and provide you with autocompletion. I'm afraid that is simply not possible - you need to track current page instance in your code for IntelliJ to be able to infer types.
Note that tests in geb-example-gradle are written in the very concise, yet dynamic style which means that IntelliJ is not able to infer types. What you need to do is to use the strongly typed style as described in the documentation. That section of the docs could do with a bit of improvement because it's not precise - I've created a Github issue to track that.
Essentially, if you tweak GebishOrgSpec from geb-example-gradle to:
class GebishOrgSpec extends GebSpec {
def "can get to the current Book of Geb"() {
when:
def homePage = to GebishOrgHomePage
and:
homePage.manualsMenu.open()
then:
homePage.manualsMenu.links[0].text().startsWith("current")
when:
homePage.manualsMenu.links[0].click()
then:
at TheBookOfGebPage
}
}
then IntelliJ will provide you with autocompletion.

Intellij Rust Plugin Incorrect 'Unresolved Reference' Errors

I'm trying out building desktop apps in rust, with the OrbTK framework. However, my IntelliJ can't find certain declarations in the orbtk crate. I'm just running the basic example on OrbTK's github page:
extern crate orbtk;
use orbtk::prelude::*;
fn main() {
Application::new()
.window(|ctx| {
Window::new()
.title("Hello, world!")
.position((100.0, 100.0))
.size(420.0, 730.0)
.child(TextBlock::new().text("Hello, world!").build(ctx))
.build(ctx)
})
.run();
}
The code runs fine, but IntelliJ is giving me an error, saying that Window and TextBlock does not exist. This is very hard to work with, since I can't tell when there is actually an error, and I can't autocomplete and check what methods are available on these structs.
So far, I have tried restarting my IDE several times, reinstalling the IntelliJ rust plugin, trying different versions of OrbTK, but none of these seemed to work. Also, this has never happened to me with any crate other than orbtk, so I have no idea if this problem is coming from IntelliJ, cargo, or otherwise.
Has anyone experienced a similar issue, or have any ideas for a solution? I would be grateful for any input. Thanks :)

How can i solve the Array problem in Kotlin?

When I try to compile some basic array code I get errors. I tried to re-install IntelliJ and I made all updates.
fun main(args: Array<String>) {
val numbers = intArrayOf(1, 4, 42, -3)
if (4 in numbers) {
println("numbers array contains 4.")
}
}
Here are the errors.
Exception in thread "main" java.lang.NoClassDefFoundError: kotlin/KotlinPackage
at KtlneKt.main(ktlne.kt:2)
at KtlneKt.main(ktlne.kt)
Caused by: java.lang.ClassNotFoundException: kotlin.KotlinPackage
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 2 more
Process finished with exit code 1
Same question. So same answer: I had the same problem and I discovered that this is a bug of Kotlin library. You cannot call intArrayOf() method.
I am sure that you are using Windows 10 Single Language Turkish. (I was using the same operating system.) Kotlin's some libraries cannot work on Turkish operating system. You can check this issue on YouTrack.
You can solve your problem with installing Windows 10 Pro English or Windows 10 Single Language English. I do not know any other approach right now. (You can open new issue on Kotlin website.)
This bug took from me 30 days. You can check my related question about this Kotlin bug.

Big compilation time for Kotlin code in IntelliJ

I have used Kotlin with latest version of Eclipse for 2 months without any performance problem on my Windows 10 computer.
Now I would like to do a live coding session about Kotlin with intelliJ (since it's the JetBrains language...) ultimate edition that I just installed and never used before, on a recent OSX computer. The two computers have good hardware and are no limiting my tests.
My problem is that each time there is a modification in my Kotlin code, the compilation time is between 8 seconds and 35 seconds. I did my tests on minimalist code:
class TestKotlin {
var a = 1
}
If I change the variable "a" and so need to build again, it always need 8 seconds in the best cases to complete the compilation.
Since I want to do a live coding session with a lot of small functions and compilations, this kind of delay is way too much significative.
The viewers will need to wait a lot before to see the results at each compilation, they are logically expecting good performance from IntelliJ tool.
In the same project, I tried to do the same kind of Java class (with a single attribute) and modify its attribute in order to trigger the compilation, and it takes less than 1 second to compile.
I tried to manually compile the code in command line with that:
kotlinc hello.kt -include-runtime -d hello.jar
java -jar hello.jar
I had some decent compilation times, even if it was near to 3 seconds.
When I look at the "Messages" screen in IntelliJ while it is compiling Kotlin code, I can see this:
Information:Kotlin: Kotlin JPS plugin version 1.0.6-release-127
Information:Kotlin: Using kotlin-home = /Users/myUsername/Library/Application Support/IntelliJIdea2016.3/Kotlin/kotlinc
It stops here for all the compilation time, and then do almost instantaneously the next steps:
Information:Kotlin: Kotlin Compiler version 1.0.6-release-127
Information:17/01/17 11:38 - Compilation completed successfully in 11s 639ms
Maybe there is a problem in the configuration of IntelliJ or something like this. I had a hard time at searching for something that could improve the performances but nothing helped me...
I would be very grateful if someone can help me to have some realistic compilation time with Kotlin in Intellij like in Eclipse!
This seems similar to the problem KT-15491.
To ensure that it's your case too, try to execute the following simple Kotlin program:
import java.io.File
import kotlin.system.measureNanoTime
fun main(args: Array<String>) {
val elapsedNs = measureNanoTime { File.createTempFile("tmp", "", null).deleteOnExit() }
println(elapsedNs.toDouble() / 1000000000)
}
If the printed elapsed time is significantly greater than a fraction of a second, than that is the reason.
This issue affects not only Kotlin compiler but every JVM program that tries to create a temporary file, or to do any other action involving SecureRandom class.
I've experienced the same slowdown on each JPS build on my Windows 7 notebook. I've tried the workaround with security providers order described in this question and it helped.
Make sure you have checked those boxes in settings:
Incremental Kotlin compilation
Kotlin compiler daemon (keeps the kotlinc process alive)

Can I run JUnit 4 to test Scala code from the command line?

If so, how? I haven't come across the proper incantation yet.
If not, what's the best approach to unit-testing Scala code from the command line? (I'm a troglodyte; I use IDEs when I have to, but I prefer to play around using Emacs and command-line tools.)
Since compiled Scala is just Java bytecode (OK, with a lot more $ characters in class names), it would be exactly as for running JUnit 4 tests against Java code, i.e. from the command line by passing the test classes as arguments to org.junit.runner.JUnitCore. As JUnit 4 out of the box only has command line support, you don't even have to worry about suppressing GUI based test runners.
That said, the specific Scala test frameworks (ScalaTest, ScalaCheck) do provide a more idiomatic set approach to testing code written in this more functional language.
You may be interested in ScalaTest.
ScalaTest is a free, open-source
testing tool for Scala and Java
programmers. It is written in Scala,
and enables you to write tests in
Scala to test either Scala or Java
code. It is released under the Apache
2.0 open source license.
Because different developers take
different approaches to creating
software, no single approach to
testing is a good fit for everyone. In
light of this reality, ScalaTest is
designed to facilitate different
styles of testing.
See the Runner documentation for how to run tests from the command line.
The suggestion of ScalaTest -- or any of the other Scala-specific frameworks, for that matter, is very good. I'd like to point to something else, though.
SBT.
SBT is a build tool, like Ant, Maven or Make. One interesting aspect of it, which will matter to us, is that it is Scala-based. I don't mean it has special capabilities to handle Scala code or that it is written in Scala, though both these things are true. I mean it uses Scala code, instead of XML like Maven and Ant, as the configuration source.
That in itself is interesting. Just today I saw a wonderful example of separating test sources from program sources, which I post here just because its so cool.
// on this project we keep all sources, whether they be Scala or Java, and whether they be
// regular classes or test classes, in a single src tree.
override def mainScalaSourcePath = "src"
override def mainJavaSourcePath = "src"
override def testScalaSourcePath = "src"
override def testJavaSourcePath = "src"
override def mainResourcesPath = "resources"
// distinguish main sources from test sources
def testSourceFilter =
"Test*.scala" | "Test*.java" |
"AbstractTest*.scala" | "AbstractTest*.java" |
"ScalaTestRunner.scala"
def mainSourceFilter = ("*.scala" | "*.java") - testSourceFilter
override def mainSources = descendents(mainSourceRoots, mainSourceFilter)
override def testSources = descendents(testSourceRoots, testSourceFilter)
But what makes it even more interesting is that SBT works like a console. You run "sbt", and you get dropped into a console-like interface, from which you can type commands like, for instance, "test", and have your tests run.