kelvindev15/Kotlin2PlantUML

How to use kotlin2plantuml.jar with an Android Studio project?

LX222 opened this issue ยท 10 comments

LX222 commented

Hi there,

I'm trying to get kotlin2plantuml.jar running with this project
https://github.com/android/uamp

What I tried was
java -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService
in the root of the Android Studio project.

But I only get

Exception in thread "main" java.lang.ClassNotFoundException: Unable to find com.example.android.uamp.media.MusicService at io.github.kelvindev15.kotlin2plantuml.utils.ReflectUtils$Companion.loadClassOrThrow(ReflectUtils.kt:34) at io.github.kelvindev15.kotlin2plantuml.utils.ReflectUtils$Companion.loadClassOrThrow$default(ReflectUtils.kt:31) at io.github.kelvindev15.kotlin2plantuml.MainKt.main(Main.kt:92)

From the README I could see , that I maybe have to pass the CLASSPATH over to the command.
So I tried

java -cp . -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService
java -cp "C:/uamp_test_uml" -jar kotlin2plantuml.jar com.example.android.uamp.media.MusicService

but unfortunatly this doesn't work and results in the same error

Hi!

You're right, the main issue here has to do with the classpath. Right now i can think of two "quick" solutions:

  1. Create a gradle task in the build file of the project, in order to launch io.github.kelvindev15.kotlin2plantuml.MainKt with the CLI arguments. This requires that you correctly set the classpath. In your specific case, an example could be:
task diagram(type: JavaExec) {
    var classpathList = new ArrayList<File>()
    classpathList.add("<path-to-build-dir>")
    classpathList.add(new File("kotlin2plantuml.jar"))
    classpath classpathList.toArray();

    mainClass = "io.github.kelvindev15.kotlin2plantuml.MainKt"
    args "com.example.android.uamp.media.MusicService"
    args "<any-cli-option>"
}

You can then launch the task with:

./gradlew diagram
  1. As explained here also add the kotlin2plantuml.jar file to the classpath and execute the io.github.kelvindev15.kotlin2plantuml.MainKt class. Example:
java -cp kotlin2plantuml.jar:<path-to-build-dir> io.github.kelvindev15.kotlin2plantuml.MainKt com.example.android.uamp.media.MusicService

I know, it may not the most beautiful way to deal with it...

ok, it's good answer, but personaly i try to do this, but this gradle file doesn't see File class, please say, what i can do,
image

I'm guessing you're missing an import. Try importing

import java.io.File;

At the top of the build file.

well, i try to do this in build.gradle(Project) , but i cant make imopor , AS saying :
image
I have not changed the paths and names of the packages so far

This issue seems to tackle your problem.

Also this may be useful.

big thank you for answering on my questions)

sorry, i solved my problem(just switched from Windows to Linux XDDD)
and i have last 2 questions :

  1. in which directory should we put(any place on the disk or in the folder with the project)
  2. path-to-build-dir - it's the path where the kotlin2plantuml.jar located, or path where located main class of app?
    thx)
  1. If you mean where to put the kotlin2plantuml.jar, I would suggest on the root folder of your project. The gradle task i previously posted assumes the jar is there.
  2. With build-dir I meant the destination directory of the compiled class. Usually for gradle projects is the /build folder, but it actually depends on the build configuration of your project.

Hi @LX222 & @Oneotrix
With the latest release there's now the possibility to set the classpath directly via CLI with the -cp option.

In Android Kotlin project, when I try to run
java -jar kotlin2plantuml.jar com.milence.MainActivity -cp app/build/intermediates/javac/devDebug/classes:app/build/tmp/kotlin-classes/devDebug:app/build/intermediates/asm_instrumented_project_classes/devDebug: --recurse
in Android Gradle Project, I receive the following error:
Exception in thread "main" java.lang.NoClassDefFoundError: androidx/appcompat/app/AppCompatActivity.

It means that it found my MainActivity class, but it can't find all other dependencies (in this case, AppCompatActivity from android core library). I think that the only solution is to include jar for each dependency manually, which makes it not practical.