1

这是触发问题的代码。这里的代码使用 janino 创建一个 Compiler。我假设该错误主要与帖子 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.
4

1 回答 1

0

编辑:我更仔细地阅读了您的堆栈跟踪,最后它指出 dalvik can't find the class 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。祝你好运!

于 2015-01-25T17:22:17.443 回答