10

I want to get a line of Java code from user and execute it in Android. For example:

String strExecutable = " int var; var = 4 + 3"
Object obj = aLibrary.eval(strExecutable);

It is not java script and I want to run a java code.

Is it possible? If yes how?

I have studied links like this. But they are questions about JVM not Android Dalvik.

Community
  • 1
  • 1
Bob
  • 22,810
  • 38
  • 143
  • 225
  • 1
    Android is an OS. You make applications for Android using JAVA – Blackbelt Mar 27 '13 at 10:39
  • @blackbelt When we say Android we usually mean Dalvik and its libraries – Bob Mar 27 '13 at 11:12
  • 1
    I'm not sure about arbitrary Java code, but [this question](http://stackoverflow.com/questions/2226863/whats-a-good-library-for-parsing-mathematical-expressions-in-java) and [this one](http://stackoverflow.com/questions/13747069/is-there-a-mathematical-expression-parsing-library-on-android) seem similar, if the requirements are just simple mathematics. [MVEL](http://mvel.codehaus.org) should handle your example string. – ig0774 Mar 27 '13 at 11:29
  • If you need something bigger, maybe you should try Rhino Javascript Engine. But if you want to really run Java, I guess it's not possible. Maybe HotStop can do it. – Daniel Monteiro Mar 27 '13 at 19:58

2 Answers2

6

You can try BeanShell! It's super easy and works also on android. Just build your app with jar library.

import bsh.Interpreter;
private void runString(String code){    
    Interpreter interpreter = new Interpreter();
    try {
         interpreter.set("context", this);//set any variable, you can refer to it directly from string
         interpreter.eval(code);//execute code
    }
    catch (Exception e){//handle exception
        e.printStackTrace();
    }
}

But be careful! Using this in production app may be security risk, especially if your app interacts with users data / files.

user1557434
  • 501
  • 5
  • 6
2

You can try something like this:

// Code Execute, Khaled A Khunaifer, 27 March 2013
class CodeExcute
{
    public static void execute (String[] commands, String[] headers)
    {
        // build commands into new java file
        try
        {
            FileWriter fstream = new FileWriter("Example.java");
            BufferedWriter out = new BufferedWriter(fstream);

            out.write("");

            for (String header : headers) out.append(header);

            out.append("class Example { public static void main(String args[]) { ");

            for (String cmd : commands) out.append(cmd);

            out.append(" } }");

            out.close();
        }
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }

        // set path, compile, & run
        try
        {
            Process tr = Runtime.getRuntime().exec(
               new String[]{ "java -cp .",
                  "javac Example.java",
                  "java Example" } );
        }
        catch (Exception e)
        {
            System.err.println("Error: " + e.getMessage());
        }

    }
}
Khaled.K
  • 5,828
  • 1
  • 33
  • 51