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

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

Open
MuhammadImran2 opened this issue Mar 3, 2024 · 9 comments

Comments

@MuhammadImran2
Copy link

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

@MuhammadImran2
Copy link
Author

i need an urgent this

@MuhammadImran2
Copy link
Author

I am waiting help me

@MuhammadImran2
Copy link
Author

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

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

also

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

@MuhammadImran2
Copy link
Author

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>"))
    })

@xiaoyvyv
Copy link
Owner

xiaoyvyv commented Mar 4, 2024

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

@MuhammadImran2
Copy link
Author

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

@MuhammadImran2
Copy link
Author

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

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

2 participants