gpt4 book ai didi

java - 在 Android 中使用 Janino 并弹出此错误...知道如何修复它吗?

转载 作者:太空宇宙 更新时间:2023-11-04 14:47:21 25 4
gpt4 key购买 nike

这是触发问题的代码。这里的代码使用janino创建一个编译器。我假设该错误主要与帖子 Java.lang.ClassnotFound 底部的 String 语句有关

  //---------------------------------------------------------
CompilerFactory compilerFactory = new CompilerFactory();
ISimpleCompiler SimpI = compilerFactory.newSimpleCompiler();
SimpI.cook("public class test{"
+ "public static int main(String[] args){"
+ "System.out.println(\"test\");"
+ "return 2 + 2;"
+ "}"
+ "}");
06-16 17:31:55.308    5243-5243/mandj.appbuildin.codingcoach.innovo D/AndroidRuntime﹕ Shutting down VM
06-16 17:31:55.308 5243-5243/mandj.appbuildin.codingcoach.innovo W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x418afc08)
06-16 17:31:55.323 5243-5243/mandj.appbuildin.codingcoach.innovo E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: mandj.appbuildin.codingcoach.innovo, PID: 5243
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3969)
at android.view.View.performClick(View.java:4640)
at android.view.View$PerformClick.run(View.java:19421)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5476)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: org.codehaus.commons.compiler.CompileException: Line 1, Column 48: String
at org.codehaus.janino.UnitCompiler.findTypeByName(UnitCompiler.java:6809)
at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5255)
at org.codehaus.janino.UnitCompiler.access$12600(UnitCompiler.java:182)
at org.codehaus.janino.UnitCompiler$16.visitReferenceType(UnitCompiler.java:5101)
at org.codehaus.janino.Java$ReferenceType.accept(Java.java:2884)
at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5367)
at org.codehaus.janino.UnitCompiler.access$12400(UnitCompiler.java:182)
at org.codehaus.janino.UnitCompiler$16.visitArrayType(UnitCompiler.java:5099)
at org.codehaus.janino.Java$ArrayType.accept(Java.java:2958)
at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
at org.codehaus.janino.UnitCompiler.access$16700(UnitCompiler.java:182)
at org.codehaus.janino.UnitCompiler$31.getParameterTypes(UnitCompiler.java:8449)
at org.codehaus.janino.IClass$IMethod.getDescriptor(IClass.java:1025)
at org.codehaus.janino.IClass.getIMethods(IClass.java:211)
at org.codehaus.janino.IClass.getIMethods(IClass.java:199)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:405)
at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:389)
at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:182)
at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:343)
at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1136)
at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:350)
at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:318)
at org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:346)
at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:189)
at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:180)
at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:78)
at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:71)
at mandj.appbuildin.codingcoach.innovo.BooleanLogicOne.onClick(BooleanLogicOne.java:64)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: String
at java.lang.Class.classForName(Native Method)
at java.lang.Class.forName(Class.java:251)
at org.codehaus.janino.ClassLoaderIClassLoader.findIClass(ClassLoaderIClassLoader.java:78)
at org.codehaus.janino.IClassLoader.loadIClass(IClassLoader.java:254)
at org.codehaus.janino.UnitCompiler.findTypeByName(UnitCompiler.java:6805)
            at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5255)
            at org.codehaus.janino.UnitCompiler.access$12600(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$16.visitReferenceType(UnitCompiler.java:5101)
            at org.codehaus.janino.Java$ReferenceType.accept(Java.java:2884)
            at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
            at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5367)
            at org.codehaus.janino.UnitCompiler.access$12400(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$16.visitArrayType(UnitCompiler.java:5099)
            at org.codehaus.janino.Java$ArrayType.accept(Java.java:2958)
            at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
            at org.codehaus.janino.UnitCompiler.access$16700(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$31.getParameterTypes(UnitCompiler.java:8449)
            at org.codehaus.janino.IClass$IMethod.getDescriptor(IClass.java:1025)
            at org.codehaus.janino.IClass.getIMethods(IClass.java:211)
            at org.codehaus.janino.IClass.getIMethods(IClass.java:199)
            at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:405)
            at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:389)
            at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:343)
            at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1136)
            at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:350)
            at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:318)
            at org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:346)
            at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:189)
            at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:180)
            at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:78)
            at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:71)
            at mandj.appbuildin.codingcoach.innovo.BooleanLogicOne.onClick(BooleanLogicOne.java:64)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NoClassDefFoundError: String
            at java.lang.Class.classForName(Native Method)
            at java.lang.Class.forName(Class.java:251)
            at org.codehaus.janino.ClassLoaderIClassLoader.findIClass(ClassLoaderIClassLoader.java:78)
            at org.codehaus.janino.IClassLoader.loadIClass(IClassLoader.java:254)
            at org.codehaus.janino.UnitCompiler.findTypeByName(UnitCompiler.java:6805)
            at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5255)
            at org.codehaus.janino.UnitCompiler.access$12600(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$16.visitReferenceType(UnitCompiler.java:5101)
            at org.codehaus.janino.Java$ReferenceType.accept(Java.java:2884)
            at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
            at org.codehaus.janino.UnitCompiler.getType2(UnitCompiler.java:5367)
            at org.codehaus.janino.UnitCompiler.access$12400(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$16.visitArrayType(UnitCompiler.java:5099)
            at org.codehaus.janino.Java$ArrayType.accept(Java.java:2958)
            at org.codehaus.janino.UnitCompiler.getType(UnitCompiler.java:5141)
            at org.codehaus.janino.UnitCompiler.access$16700(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$31.getParameterTypes(UnitCompiler.java:8449)
            at org.codehaus.janino.IClass$IMethod.getDescriptor(IClass.java:1025)
            at org.codehaus.janino.IClass.getIMethods(IClass.java:211)
            at org.codehaus.janino.IClass.getIMethods(IClass.java:199)
            at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:405)
            at org.codehaus.janino.UnitCompiler.compile2(UnitCompiler.java:389)
            at org.codehaus.janino.UnitCompiler.access$400(UnitCompiler.java:182)
            at org.codehaus.janino.UnitCompiler$2.visitPackageMemberClassDeclaration(UnitCompiler.java:343)
            at org.codehaus.janino.Java$PackageMemberClassDeclaration.accept(Java.java:1136)
            at org.codehaus.janino.UnitCompiler.compile(UnitCompiler.java:350)
            at org.codehaus.janino.UnitCompiler.compileUnit(UnitCompiler.java:318)
            at org.codehaus.janino.SimpleCompiler.compileToClassLoader(SimpleCompiler.java:346)
            at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:189)
            at org.codehaus.janino.SimpleCompiler.cook(SimpleCompiler.java:180)
            at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:78)
            at org.codehaus.commons.compiler.Cookable.cook(Cookable.java:71)
            at mandj.appbuildin.codingcoach.innovo.BooleanLogicOne.onClick(BooleanLogicOne.java:64)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at android.view.View$1.onClick(View.java:3964)
            at android.view.View.performClick(View.java:4640)
            at android.view.View$PerformClick.run(View.java:19421)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.`enter code here`system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: Didn't find class "String" on path: DexPathList[[zip file "/data/app/mandj.appbuildin.codingcoach.innovo-79.apk"],nativeLibraryDirectories=[/data/app-lib/mandj.appbuildin.codingcoach.innovo-79, /vendor/lib, /system/lib]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.

最佳答案

编辑:我仔细阅读了你的堆栈跟踪,最后它指出dalvik找不到类String。您可以尝试为 SimpleCompiler 提供不使用 String 等外部符号的代码,例如 public class Test{ public int doTest(){ return 21+21; } }} 看看是否可以编译并运行。如果是这种情况,请忽略下面的整个部分,只需将 dalvik 查找的类引入到类加载器中即可。我认为您可以通过使用 [SimpleCompiler.setParentClassLoader()][1]

引入父类加载器来做到这一点

下面的整个部分

Android 使用特定于其 dalvik JVM 的替代格式字节码。当您构建 APK 时,Android SDK 工具实际上会将您最喜欢的 java 编译器输出的字节码转换为 dalvik 兼容格式 ( dex )。

因此,要解决此问题,您需要寻找一种在运行 janino 输出之前对其进行后处理的方法。这可能真的很复杂。首先,如何说服 janino 输出 .class 文件而不是尝试直接加载它们?我实际上是从谷歌来到这里的,试图了解更多相关信息。

其次,当 Android SDK 转换为 dex 格式时,如何在实际 Android 设备本身上运行相同的转换过程?

我将以迄今为止最好的两条线索结束:UnitCompilerdex-tools 。祝你好运!

关于java - 在 Android 中使用 Janino 并弹出此错误...知道如何修复它吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24253261/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com