11

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.

4
  • 1
    Android is an OS. You make applications for Android using JAVA Commented Mar 27, 2013 at 10:39
  • @blackbelt When we say Android we usually mean Dalvik and its libraries Commented Mar 27, 2013 at 11:12
  • 1
    I'm not sure about arbitrary Java code, but this question and this one seem similar, if the requirements are just simple mathematics. MVEL should handle your example string. Commented Mar 27, 2013 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. Commented Mar 27, 2013 at 19:58

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

What do I need to get access to bsh? It says cannot resolve symbol.bsh
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());
        }

    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.