2

I'm trying to create a Java learning Android application. It should have a code playground for users; which can run simple Java code.

I save users code in a java code and I'm trying to run it. I'm using JANINO, but the problem is that I can not load external class including users code with it.

Here is my code:

String UsersCodeInput;
public void Run() throws FileNotFoundException, IOException
{
    
    File sourcepath = new File(Environment.getExternalStorageDirectory()+"/MainFolder");
    File JavaFile = new File(sourcepath.getPath()+"A.java");
    FileOutputStream fos = new FileOutputStream(JavaFile);
    fos.write(UsersCodeInput.getBytes());
    fos.close();
    // now Users Code is Saved.
    //trying to run it by means of janino :
    
    ClassLoader cl = new JavaSourceClassLoader(
        this.getClass().getClassLoader(),  // parentClassLoader
        new File[] {sourcepath}, //our sourceFolder
        "UTF-8" //Encodeing                    
    );

    try
    {
        // problem is here. next line is not working well; throws classNotFound.
        Object o = cl.loadClass(".A").newInstance();
        //the class implements runnable.
        ((Runnable) o).run();
    }
    catch (ClassNotFoundException e)
    {Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
    catch (InstantiationException e)
    {Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
    catch (IllegalAccessException e)
    {Toast.makeText(this,e.getCause().toString(),Toast.LENGTH_SHORT).show();}
    
}

And here is JANINOs tutorial ; Read "source code compiler" part http://janino-compiler.github.io/janino/

Thanks.

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

0 Answers0