JavaCompileEngine icon indicating copy to clipboard operation
JavaCompileEngine copied to clipboard

Can you convert CompileActivity.kt into CompileActivity.java . From Java class class kotlin code

Open MuhammadImran2 opened this issue 1 year ago • 9 comments

Can you convert CompileActivity.kt into CompileActivity.java . From Java to kotlin code

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

i need an urgent this

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

I am waiting help me

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

i want only convert below method into java code private fun runProgram() = launch(JavaEngine.CompileExceptionHandler) { binding.printView.text = null

    // build 文件夹
    val buildDir = PathUtils.getExternalAppFilesPath() + "/SingleExample/build"

    // java 文件夹
    val javaDir = PathUtils.getExternalAppFilesPath() + "/SingleExample/java"

    // 待编译的 Main.java
    val javaFilePath = withContext(Dispatchers.IO) {
        // java 文件夹内 Main.java 文件,写入代码内容
        val javaFilePath = "$javaDir/Main.java"

        FileIOUtils.writeFileFromString(javaFilePath, binding.codeText.text.toString())

        // 返回源文件路径
        javaFilePath
    }

    // 编译 class,libFolder 为第三方 jar 存放目录,没有传空即可
    // 编译完成返回目标 classes.jar,内部通过协程在 IO 线程处理的
    val compileJar: File = JavaEngine.classCompiler.compile(
        sourceFileOrDir = javaFilePath,
        buildDir = buildDir,
        libFolder = null
    ) { taskName, progress ->

        // 这里是进度,回调在主线程...
        binding.printView.append(String.format("%3d%% Compiling: %s\n", progress, taskName))
    }
    binding.printView.append("Compiling class success!\n\n")

    binding.printView.append("Compiling dex start...\n")

    // 编译 classes.dex,这一步相关的信息通过 System.xxx.print 输出
    val dexFile = JavaEngine.dexCompiler.compile(compileJar.absolutePath, buildDir)

    binding.printView.append("Compiling dex success!\n\n")

    binding.printView.append("Run dex start...\n\n")

    // JavaEngine.
    val programConsole = JavaEngine.javaProgram.run(dexFile, arrayOf("args"),
        chooseMainClassToRun = { classes, continuation ->
            val dialog = AlertDialog.Builder(this@CompileActivity)
                .setTitle("请选择一个主函数运行")
                .setItems(classes.toTypedArray()) { p0, p1 ->
                    p0.dismiss()
                    continuation.resume(classes[p1])
                }
                .setCancelable(false)
                .setNegativeButton("取消") { d, v ->
                    d.dismiss()
                    continuation.resumeWithException(Exception("取消操作"))
                }.create()

            dialog.show()
            dialog.setCanceledOnTouchOutside(false)
        },
        printOut = {
            binding.printView.append(it)
        },
        printErr = {
            binding.printView.append(Html.fromHtml("<font color=\"#FF0000\">$it</font>"))
        })

    binding.btSend.setOnClickListener {
        val input = binding.inputEdit.text.toString()
        programConsole.inputStdin(input)
        binding.inputEdit.text = null
    }
}

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

i am facing issue // 待编译的 Main.java val javaFilePath = withContext(Dispatchers.IO) { // java 文件夹内 Main.java 文件,写入代码内容 val javaFilePath = "$javaDir/Main.java"

    FileIOUtils.writeFileFromString(javaFilePath, binding.codeText.text.toString())

    // 返回源文件路径
    javaFilePath
}

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

also

// 编译 classes.dex,这一步相关的信息通过 System.xxx.print 输出 val dexFile = JavaEngine.dexCompiler.compile(compileJar.absolutePath, buildDir)

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

and this

val programConsole = JavaEngine.javaProgram.run(dexFile, arrayOf("args"), chooseMainClassToRun = { classes, continuation -> val dialog = AlertDialog.Builder(this@CompileActivity) .setTitle("请选择一个主函数运行") .setItems(classes.toTypedArray()) { p0, p1 -> p0.dismiss() continuation.resume(classes[p1]) } .setCancelable(false) .setNegativeButton("取消") { d, v -> d.dismiss() continuation.resumeWithException(Exception("取消操作")) }.create()

        dialog.show()
        dialog.setCanceledOnTouchOutside(false)
    },
    printOut = {
        binding.printView.append(it)
    },
    printErr = {
        binding.printView.append(Html.fromHtml("<font color=\"#FF0000\">$it</font>"))
    })

MuhammadImran2 avatar Mar 03 '24 11:03 MuhammadImran2

You can copy this java code into chatGpt, it can help you to convert it

xiaoyvyv avatar Mar 04 '24 07:03 xiaoyvyv

Yes, I do, but ChatGPT is not sufficient for coding. ChatGPT showed me the wrong code.

MuhammadImran2 avatar Mar 04 '24 07:03 MuhammadImran2

Cannot resolve method 'compile' in 'JavaClassCompiler'

File compileJar = JavaEngine.getClassCompiler() .compile( javaFilePath, buildDir, null, (taskName, progress) -> { // printView.append(String.format("%3d%% Compiling: %s\n", progress, taskName)); });

Screenshot_20240304_130608

MuhammadImran2 avatar Mar 04 '24 08:03 MuhammadImran2