Skip to content
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

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

Open
johnsonlee opened this issue Nov 9, 2020 · 10 comments

Comments

@johnsonlee
Copy link

[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
Copy link
Author

Copyright : 2020 Holger Brandl
License   : MIT
Version   : v2.9.3
Website   : https://github.com/holgerbrandl/kscript

@holgerbrandl
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Collaborator

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
Copy link
Author

Nothing has changed

image

@johnsonlee
Copy link
Author

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

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

@holgerbrandl
Copy link
Collaborator

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
Copy link
Contributor

martinbonnin commented Feb 9, 2021

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
Copy link
Collaborator

Without howtorun it is indeed hard to handle.

@holgerbrandl holgerbrandl reopened this Feb 11, 2021
@chardskarth
Copy link

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?

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

No branches or pull requests

4 participants