kscripting / kscript

Scripting enhancements for Kotlin
MIT License
2.09k stars 126 forks source link

java.lang.NoClassDefFoundError: org/jetbrains/kotlin/mainKts/MainKtsScript #290

Open johnsonlee opened 4 years ago

johnsonlee commented 4 years ago
[kscript] Resolving dependencies...
[kscript]     Resolving com.android.tools.ddms:ddmlib:27.1.0...Done
[kscript]     Resolving com.github.holgerbrandl:kscript-annotations:1.4...Done
[kscript] Dependencies resolved
Exception in thread "main" java.lang.NoClassDefFoundError: org/jetbrains/kotlin/mainKts/MainKtsScript
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:756)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:468)
    at java.net.URLClassLoader.access$100(URLClassLoader.java:74)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:369)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:363)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:362)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    at Main_Ddms_main$Companion.main(Main_Ddms_main.kt:5)
    at Main_Ddms_main.main(Main_Ddms_main.kt)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:64)
    at org.jetbrains.kotlin.runner.Main.run(Main.kt:149)
    at org.jetbrains.kotlin.runner.Main.main(Main.kt:159)
Caused by: java.lang.ClassNotFoundException: org.jetbrains.kotlin.mainKts.MainKtsScript
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
    ... 20 more

ddms.main.kts

#!/usr/bin/env kscript

@file:MavenRepository("google", "https://dl.google.com/dl/android/maven2/")
@file:DependsOnMaven("com.android.tools.ddms:ddmlib:27.1.0")

import com.android.ddmlib.*
import com.android.ddmlib.internal.*
import java.io.*
import java.util.concurrent.*
import kotlin.system.*

fun waitForDevice(timeout: Long = Long.MAX_VALUE, unit: TimeUnit = TimeUnit.MILLISECONDS): IDevice {
    val signal = CountDownLatch(1)
    var remote: IDevice? = null

    AndroidDebugBridge.addDeviceChangeListener(object : AndroidDebugBridge.IDeviceChangeListener {
        override fun deviceChanged(device: IDevice, mask: Int) {
            if (0 != (mask and IDevice.CHANGE_CLIENT_LIST)) {
                remote = device
                AndroidDebugBridge.removeDeviceChangeListener(this)
                signal.countDown()
            }
        }
        override fun deviceDisconnected(device: IDevice) = Unit
        override fun deviceConnected(device: IDevice) {
            println("${device.serialNumber}: ${device.state}")
        }
    })

    val exe = System.getenv("PATH").split(':').map {
        File(it, "adb")
    }.firstOrNull(File::exists)?.canonicalPath ?: error("ADB not found")
    AndroidDebugBridge.createBridge(exe, true)
    signal.await(timeout, unit)
    return remote!!
}

fun IDevice.watchThread(app: String) {
    (getClient(app) as? ClientImpl)?.let { client ->
        val signal = CountDownLatch(1)

        AndroidDebugBridge.addClientChangeListener(object : AndroidDebugBridge.IClientChangeListener {
            var lastSize = 0
            override fun clientChanged(client: Client, mask: Int) {
                if (0 == (mask and Client.CHANGE_THREAD_DATA)) {
                    return
                }
                val threads = client.clientData.threads
                if (threads.size == lastSize) {
                    AndroidDebugBridge.removeClientChangeListener(this)
                    println("""
                        |----------------------------------------------------------------------------------------
                        |  id  |   tid    |  stime   |  utime   | name
                        |------+----------+----------+----------+------------------------------------------------
                    """.trimMargin())
                    threads.forEach { thread ->
                        println("${if (thread.isDaemon) "*" else " "}${thread.threadId.toString().padStart(4)} | ${thread.tid.toString().padStart(8)} | ${thread.stime.toString().padStart(8)} | ${thread.utime.toString().padStart(8)} | ${thread.threadName}")
                    }
                    signal.countDown()
                }
                lastSize = threads.size
            }
        })
        client.isThreadUpdateEnabled = true
        client.requestThreadUpdate()
        signal.await()
    } ?: error("Application ${app} not found")
}

if (args.size <= 0) {
    error("application name is required")
}

AndroidDebugBridge.init(true)
DdmPreferences.setLogLevel(Log.LogLevel.ERROR.stringValue)
waitForDevice().watchThread(args[0])
exitProcess(0)
johnsonlee commented 4 years ago
Copyright : 2020 Holger Brandl
License   : MIT
Version   : v2.9.3
Website   : https://github.com/holgerbrandl/kscript
holgerbrandl commented 4 years ago

I've tried and it seems to work as intended

brandl@ubuntu2:~$ kscript kscript300.kts 
[kscript] Resolving dependencies...
[kscript]     Resolving com.android.tools.ddms:ddmlib:27.1.0...Done
[kscript]     Resolving com.github.holgerbrandl:kscript-annotations:1.4...Done
[kscript] Dependencies resolved
Exception in thread "main" java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at Main_Kscript300$Companion.main(Main_Kscript300.kt:6)
    at Main_Kscript300.main(Main_Kscript300.kt)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.jetbrains.kotlin.runner.AbstractRunner.run(runners.kt:64)
    at org.jetbrains.kotlin.runner.Main.run(Main.kt:149)
    at org.jetbrains.kotlin.runner.Main.main(Main.kt:159)
Caused by: java.lang.IllegalStateException: application name is required
    at Kscript300.<init>(kscript300.kts:71)
    ... 13 more
brandl@ubuntu2:~$ java -version
openjdk version "1.8.0_272"
OpenJDK Runtime Environment (build 1.8.0_272-8u272-b10-0ubuntu1~20.04-b10)
OpenJDK 64-Bit Server VM (build 25.272-b10, mixed mode)
brandl@ubuntu2:~$ kotlin -version
Kotlin version 1.4.10-release-411 (JRE 1.8.0_272-8u272-b10-0ubuntu1~20.04-b10)
brandl@ubuntu2:~$ 

Could you check/provide the version of java and kotlin?

johnsonlee commented 4 years ago

Java version:

openjdk version "1.8.0_232"
OpenJDK Runtime Environment (AdoptOpenJDK)(build 1.8.0_232-b09)
OpenJDK 64-Bit Server VM (AdoptOpenJDK)(build 25.232-b09, mixed mode)

Kotlin versoin:

Kotlin version 1.4.10-release-411 (JRE 1.8.0_232-b09)
holgerbrandl commented 4 years ago

That's odd. Your settings seem to be identical to my setup and it works for me (see above).

To trace down the root cause of the problem, could you 1) clear the cache with kscript --clear-cache. Does this fix script execution? 2) If not, could you temporarily move your maven cache to make sure that dependencies are correctly resolved:

mv ~/.m2 m2_tmp
kscript kscript300.kts 
#restore m2
rm -rf ~/.m2
mv m2_temp ~/.m2
johnsonlee commented 4 years ago

Nothing has changed

image

johnsonlee commented 4 years ago

I've moved to kotlin script, it works well.

https://gist.github.com/johnsonlee/154671b11f7e8c068aa6966ac72aeb7f

holgerbrandl commented 4 years ago

Hmm, it works for me, and you have found another solution. So I think can solve the ticket then. Also, the new version v3 of kscript may solve the problem on your end as well.

Feel welcome to reopen the ticket if needed.

martinbonnin commented 3 years ago

I've bumped into the same issue. This looks related to the file extension. I'm guessing kotlin/kotlinc do some specific handling for files ending with *.main.kts and that doesn't play nice with kscript.

There is now a -howtorun option that might or might not help...

holgerbrandl commented 3 years ago

Without howtorun it is indeed hard to handle.

chardskarth commented 1 year ago

I'm currently encountering this same issue when I do "kscript --idea myscript.main.kts".

Intellij shows this error. Anyone knows how to fix this error in the IDE?