Implement a Java compiler in android app -
i have university:
how implement java compiler , dex converter android app?
my process is:
i have created new project, main activity. in main activity in method oncreate, add piece of code (take url on top)
file storage = getdir("all41", context.mode_private); system.err.println("copying android.jar asssets internal storage make available compiler"); bufferedinputstream bis = null; outputstream dexwriter = null; int buf_size = 8 * 1024; try { bis = new bufferedinputstream(getassets().open("android.jar")); dexwriter = new bufferedoutputstream( new fileoutputstream(storage.getabsolutepath() + "/android.jar")); byte[] buf = new byte[buf_size]; int len; while((len = bis.read(buf, 0, buf_size)) > 0) { dexwriter.write(buf, 0, len); } dexwriter.close(); bis.close(); } catch (exception e) { system.err.println("error while copying assets: " + e.getmessage()); e.printstacktrace(); } system.err.println("instantiating compiler , compiling java file"); org.eclipse.jdt.internal.compiler.batch.main ecjmain = new org.eclipse.jdt.internal.compiler.batch.main(new printwriter(system.out), new printwriter(system.err), false/*nosystemexit*/, null); ecjmain.compile(new string[] {"-classpath", storage.getabsolutepath()+"/android.jar", environment.getexternalstoragedirectory().getabsolutepath() + "/test.java"}); system.err.println("calling dex , dexifying test class"); com.android.dx.command.main.main(new string[] {"--dex", "--output=" + storage.getabsolutepath() + "/test.zip", environment.getexternalstoragedirectory().getabsolutepath() + "/./test.class"}); system.err.println("instantiating dexclassloader, loading class , invoking tostring()"); dexclassloader cl = new dexclassloader(storage.getabsolutepath() + "/test.zip", storage.getabsolutepath(), null, getclassloader()); try { class libproviderclazz = cl.loadclass("test"); object instance = libproviderclazz.newinstance(); system.err.println(instance.tostring()); } catch (exception e) { system.err.println("error while instanciating object: " + e.getmessage()); e.printstacktrace(); }
and created test.java
public string tostring() { return "hallo welt!"; }
now, add in proprieties of project, jdt-core.jar , dx.jar.
when run application, following error:
04-18 12:46:40.169: w/dalvikvm(1314): vfy: unable resolve static method 3386: lcom/android/dx/command/main;.main ([ljava/lang/string;)v 04-18 12:46:40.179: d/dalvikvm(1314): vfy: replacing opcode 0x71 @ 0x00af 04-18 12:46:40.648: w/system.err(1314): copying android.jar asssets internal storage make available compiler 04-18 12:46:40.648: w/system.err(1314): error while copying assets: android.jar 04-18 12:46:40.648: w/system.err(1314): java.io.filenotfoundexception: android.jar 04-18 12:46:40.668: w/system.err(1314): @ android.content.res.assetmanager.openasset(native method) 04-18 12:46:40.668: w/system.err(1314): @ android.content.res.assetmanager.open(assetmanager.java:315) 04-18 12:46:40.668: w/system.err(1314): @ android.content.res.assetmanager.open(assetmanager.java:289) 04-18 12:46:40.668: w/system.err(1314): @ com.example.compilatore.mainactivity.oncreate(mainactivity.java:33) 04-18 12:46:40.668: w/system.err(1314): @ android.app.activity.performcreate(activity.java:5104) 04-18 12:46:40.668: w/system.err(1314): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 04-18 12:46:40.668: w/system.err(1314): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 04-18 12:46:40.679: w/system.err(1314): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 04-18 12:46:40.679: w/system.err(1314): @ android.app.activitythread.access$600(activitythread.java:141) 04-18 12:46:40.679: w/system.err(1314): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-18 12:46:40.679: w/system.err(1314): @ android.os.handler.dispatchmessage(handler.java:99) 04-18 12:46:40.679: w/system.err(1314): @ android.os.looper.loop(looper.java:137) 04-18 12:46:40.688: w/system.err(1314): @ android.app.activitythread.main(activitythread.java:5041) 04-18 12:46:40.688: w/system.err(1314): @ java.lang.reflect.method.invokenative(native method) 04-18 12:46:40.688: w/system.err(1314): @ java.lang.reflect.method.invoke(method.java:511) 04-18 12:46:40.688: w/system.err(1314): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-18 12:46:40.688: w/system.err(1314): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-18 12:46:40.688: w/system.err(1314): @ dalvik.system.nativestart.main(native method) 04-18 12:46:40.688: w/system.err(1314): instantiating compiler , compiling java file 04-18 12:46:40.698: w/system.err(1314): calling dex , dexifying test class 04-18 12:46:40.698: d/androidruntime(1314): shutting down vm 04-18 12:46:40.698: w/dalvikvm(1314): threadid=1: thread exiting uncaught exception (group=0x40a71930) 04-18 12:46:40.718: e/androidruntime(1314): fatal exception: main 04-18 12:46:40.718: e/androidruntime(1314): java.lang.noclassdeffounderror: com.android.dx.command.main 04-18 12:46:40.718: e/androidruntime(1314): @ com.example.compilatore.mainactivity.oncreate(mainactivity.java:57) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activity.performcreate(activity.java:5104) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activitythread.access$600(activitythread.java:141) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 04-18 12:46:40.718: e/androidruntime(1314): @ android.os.handler.dispatchmessage(handler.java:99) 04-18 12:46:40.718: e/androidruntime(1314): @ android.os.looper.loop(looper.java:137) 04-18 12:46:40.718: e/androidruntime(1314): @ android.app.activitythread.main(activitythread.java:5041) 04-18 12:46:40.718: e/androidruntime(1314): @ java.lang.reflect.method.invokenative(native method) 04-18 12:46:40.718: e/androidruntime(1314): @ java.lang.reflect.method.invoke(method.java:511) 04-18 12:46:40.718: e/androidruntime(1314): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 04-18 12:46:40.718: e/androidruntime(1314): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 04-18 12:46:40.718: e/androidruntime(1314): @ dalvik.system.nativestart.main(native method)
Comments
Post a Comment