Skip to content

kotlin imports into Java file are unresolved #531

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
viral-p opened this issue May 10, 2018 · 28 comments
Open

kotlin imports into Java file are unresolved #531

viral-p opened this issue May 10, 2018 · 28 comments

Comments

@viral-p
Copy link

viral-p commented May 10, 2018

In a project, there are some interfaces set up in Kotlin, also config files, that are imported into the main Java file. These aren't recognized as valid imports, support for this would be useful. Attempting to move out of IntelliJ to VS Code, this is the only problem with the project.

@viral-p viral-p changed the title kotlin imports kotlin imports into Java file are unresolved May 10, 2018
@fbricon
Copy link
Collaborator

fbricon commented May 22, 2018

While I can appreciate the problem, Kotlin support is kinda out of scope of our Java support at the moment. We'll need to assess if/how we can help improve the situation. It might come from a companion extension eventually.

@fwcd
Copy link
Contributor

fwcd commented Jun 2, 2018

Currently developing a language server and an extension for Kotlin, I have thought about this.

In a mixed Kotlin-Java Gradle project both languages do recognize dependencies properly. They fail when trying to use classes from the other language though.

An approach to this problem might be to add compiled JVM classes located in the other languages build directory to the classpath.

@mrjones2014
Copy link

Any updates on this?

@coffeepac
Copy link

@fwcd do you have any suggestions on how to implement this either here or in the kotlin language server? you mentioned copying the classfiles around but I'm not sure how to do that manually, let alone code it up.

@fwcd
Copy link
Contributor

fwcd commented Jul 12, 2018

@coffeepac Implementing that in KotlinLanguageServer first might be easier, because the KLS repo has a smaller codebase and is pretty easy to setup.

Copying any classfiles might not even be necessary, the language server would just have to know where they are located. The Java language server uses the Eclipse project model whilst the Kotlin language server entirely relies on Gradle/Maven, thus placing the classfiles in different locations each.

When scanning the classpath from the Kotlin language server, it would have to search through the directory that the Java language server/the Eclipse project model places the compiled files in.

@icelove82
Copy link

Can't wait for using kotlin in VSCode. When can we use kotlin in VSCode?

@osaaka
Copy link

osaaka commented Oct 11, 2019

Any updates or news?

@hitoshisatow
Copy link

This also seems like the last piece for me as well to make VS Code viable for development. I am unable to run Java Junit classes that reference a Kotlin class as it is not able to find the class.

@ghost
Copy link

ghost commented Jul 14, 2021

Yes this is pretty hard since most projects nowadays mix kotlin packages in the import...

@daplf
Copy link

daplf commented Jun 27, 2022

Hey, just in case anyone might have missed it, I built a prototype companion extension that attempts to solve this issue: https://github.com/fwcd/vscode-java-kotlin
We haven't released it yet, but if you really need this you can build it yourself for now.

@major7x
Copy link

major7x commented Dec 15, 2022

Any updates on this issue?

@mikailcolak
Copy link

Any updates on this?

@gcocevar
Copy link

I follow the others here: are there any updates?
I'm having the same problem and it's frustrating over and over again.
The program compiles correctly but since the import of Kotlin classes into Java is not supported any reference to a Kotlin class causes many error messages in the error panel, making it very difficult to work.
Please can someone consider trying to fix this? Unfortunately I don't have too much knowledge to help more.

@linsaftw
Copy link

Any updates? It's frustrating to have to use IntelliJ for some projects.

@daplf
Copy link

daplf commented Apr 6, 2023

Hey everyone. I finally published the companion extension I linked above to make this work: https://marketplace.visualstudio.com/items?itemName=daplf.vscode-java-kotlin

Keep in mind that this is only a prototype for now and I haven't used it with very large projects, but it seems to work well for small ones at least. If you have any problems with the extension, please create an issue. I'm happy to improve it :)

@FelixGSE
Copy link

Any updates here? Will this feature come at some point?

@dimaslanjaka
Copy link

info update

@Kangmo
Copy link

Kangmo commented Mar 9, 2024

this is necessary, as we mix kotlin and java

@Clay-Ferguson
Copy link

Every big project (including mine) that is going to move from Java to Kotlin will do so incrementally rather than as a total rewrite so the need for having Kotlin recognized in in the VSCode IDE is a pretty big deal breaker, and will stop people from adopting Kotlin.

@crummy
Copy link

crummy commented May 31, 2024

Every big project (including mine) that is going to move from Java to Kotlin will do so incrementally rather than as a total rewrite so the need for having Kotlin recognized in in the VSCode IDE is a pretty big deal breaker, and will stop people from adopting Kotlin.

Agreed, but I suggest telling that to Jetbrains.

@Clay-Ferguson
Copy link

Agreed, but I suggest telling that to Jetbrains.

Jetbrains benefits HUGELY from leaving this flaw unfixed, because it's literally the only thing stopping Kotlin & Java from being mixed in VSCode projects, and forces people to stay on IntelliJ.

@rattrayalex
Copy link

@fbricon @fwcd this issue prevents vscode-java users from being able to see docs-on-hover or jump-to-definition from the openai and anthropic Java SDKs, which are implemented in Kotlin.

Would you be open to sponsorship or know of a trusted contributor we could sponsor to move this forward? (email in profile)

@fwcd
Copy link
Contributor

fwcd commented Apr 18, 2025

I don't really have the capacity to maintain the language server more actively these days, but feel free to ask some of the other contributors. @daplf wrote an extension for this a while ago: https://github.com/daplf/vscode-java-kotlin

Would love to see better Kotlin/Java interop in VSCode too.

@TomerAberbach
Copy link

@fwcd do you know if this issue is the same or different from importing a Kotlin library into Java? Versus importing a Kotlin file in the same project into Java?

I ask because it sounds like in the latter case the import is completely unresolved while in the former case the import is resolved, but you don't get docs-on-hover or jump-to-definition.

@rattrayalex
Copy link

Thanks @fwcd ! Do you have a sense of whether that extension could feasibly be merged into this one?

Ideally Java users would not have to install a Kotlin-related extension just to use a library that happens to be built with Kotlin.

@fwcd
Copy link
Contributor

fwcd commented Apr 18, 2025

@TomerAberbach It's been a while since I've dug into the classpath logic so I'm probably not the right person to ask 😄 That said, I remember it being a bit fiddly, since it very much depends on how your project is built and structured. The idea was definitely to support mixed Java/Kotlin codebases, not sure how far we got with that.

@daplf
Copy link

daplf commented Apr 19, 2025

@rattrayalex @TomerAberbach Hey 👋

Your use case is one I hadn't really considered and it's not really what my extension was made for. However, the issue is fairly similar.

In a nutshell, my extension allows Kotlin source files used in mixed Java/Kotlin codebases to be added to the project classpath, so that the Java Language Server doesn't complaint about missing symbols (defined in Kotlin).

However, my extension merely adds the compiled bytecode to the project classpath in JDT LS. This means we have the same problem. Any additional language support, such as jump-to-definition or docs-on-hover is missing, because JDT LS only knows about the bytecode (and even if it knew about the source files, it wouldn't be able to understand them, since they are written in Kotlin).

The solution to this will likely be something along these lines: eclipse-jdtls/eclipse.jdt.ls#2583 (comment)

So there needs to be a companion extension that advertises Java language support and is thus able to provide hovers and definitions for Kotlin symbols used in Java source files. This extension would likely need to connect to some other language server that is capable of rendering docs for the Kotlin symbols and provide definition results. Since the actual project is managed by JDT LS, this separate language server would potentially need to make requests to JDT LS (via the delegateCommandHandler pattern) to fetch any information necessary to resolve the symbols.

I don't have a lot of experience with JDT LS and their extensions apart from the one I built, so I'm not entirely sure this would actually work, but it seems to me like this is how some similar use cases are currently being done (e.g., vscode-microprofile, see eclipse-jdtls/eclipse.jdt.ls#1731)

Regarding upstreaming any of this to vscode-java, I can't speak on behalf of the extension maintainers, but as far as I understand, this extension is meant only for Java support. Kotlin (in any shape or form) is out of scope. Therefore, I suspect you will always require a companion extension.

@rattrayalex
Copy link

Thanks for the note @daplf !

I can't speak on behalf of the extension maintainers, but as far as I understand, this extension is meant only for Java support. Kotlin (in any shape or form) is out of scope.

I am curious whether maintainers share this view. Should a Java developer installing a package like openai-java get intellisense if that package is written with (some) Kotlin?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests