Using Janino In Android and this error popped up... any idea on how to fix it?

590 Views Asked by At

Here is the code that Triggers the issue. The code here uses janino to create a Compiler. I am assuming that the error is mostly related to the String statement down at the bottom of the 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.
1

There are 1 best solutions below

0
On

EDIT: I read your stacktrace a little bit closer and at the end it states that dalvik can't find the class String. You could try supplying your SimpleCompiler with code that uses no external symbols like String, for example public class Test{ public int doTest(){ return 21+21; }} And see if that compiles and runs. If that is the case then disregard the whole part below, and simply work on introducing the classes that dalvik looks for into the classloader. I think you can do that by introducing a parent classloader using [SimpleCompiler.setParentClassLoader()][1]

Whole part below

Android uses an alternative format bytecode spesifict to it's dalvik JVM. When you build an APK, the Android SDK tools will actually convert the bytecodes output by your favourite java compiler into dalvik compatible format (dex).

So to fix this, you are looking for a way to post-process janino output before running it. This could be really complicated. First, how do you convince janino to output .class files instead of trying to load them directly? I actually arrived here from google trying to find out more about that.

Secondly, how can you run the same conversion process that happens in your Android SDK when it converts to dex format, but on the actual Android device itself?

I will end with my two best clues so far: UnitCompiler and dex-tools. Good luck!