以下是触发该问题的代码。这里的代码使用janino创建一个编译器。我假设这个错误主要与post Java.lang.ClassnotFound底部的字符串语句有关
//---------------------------------------------------------
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.发布于 2015-01-26 01:22:17
编辑:我仔细阅读了你的堆栈跟踪,最后指出dalvik找不到类String。如果是这样,那么忽略下面的整个部分,简单地将dalvik寻找的类引入到类加载器中。我认为您可以通过使用[SimpleCompiler.setParentClassLoader()][1]引入父类加载器来做到这一点
下面的整体部分
构建APK时,Android SDK工具实际上会将您最喜欢的java编译器输出的字节码转换为dalvik兼容格式(dex)。
因此,为了解决这个问题,您正在寻找一种在运行janino输出之前对其进行后处理的方法。这可能真的很复杂。首先,如何说服janino输出.class文件,而不是直接加载它们?实际上,我是从谷歌来到这里,试图了解更多关于这方面的信息。
其次,当Android SDK转换为dex格式时,如何在实际的Android设备本身上运行相同的转换过程
我将以目前为止最好的两条线索来结束:UnitCompiler和dex-tools。祝好运!
https://stackoverflow.com/questions/24253261
复制相似问题