10

I created a Kotlin project and Java class in it:

package text;

public class StringUtils {

    public static void filter(boolean flag) {
        System.out.println("Flag is " + flag);
    }
}

Then I tried to call filter method from the Kotlin code:

val exists = true;
StringUtils.filter(exists)

I didn't see any errors in my Intellij IDE but in run-time I got an exception:

Exception in thread "main" java.lang.NoClassDefFoundError: text/StringUtils
    at Person.printName(Person.kt:66)
    at PersonKt.main(Person.kt:103)
    at PersonKt.main(Person.kt)
Caused by: java.lang.ClassNotFoundException: text.StringUtils
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:606)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:168)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:522)
    ... 3 more
  • Kotlin compiler: 1.4.30
  • IDE: Intellij 2020.3.2
  • JDK: 15.0.1
4
  • 7
    I tried your code and it works fine. Make sure you put the java class inside the Java folder and not Kotlin folder. Commented Feb 7, 2021 at 9:52
  • 2
    Thank you. After I moved TextUtils into src/main/java folder the code was run properly. But I guess Intellij should display errors/warnings if I put Java class into src/main/kotlin folder and try to use it in the Kotlin code Commented Feb 7, 2021 at 13:45
  • You can configure the building system to look for both java and Kolton files in the same directories. I guess that's why the IDE doesn't show it as error. Commented Feb 7, 2021 at 13:50
  • Yes, IDEA should warn, please watch youtrack.jetbrains.com/issue/KTIJ-1233 for updates on this. Commented Feb 11, 2021 at 12:30

1 Answer 1

32

You have to put all Java classes under src/main/java, instead of src/main/kotlin.

Just right click on main -> new -> directory -> select java (or type it).

Sign up to request clarification or add additional context in comments.

1 Comment

You are a lifesaver.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.