1

How can I parse Java code stored in a String?

Example:

String s = "int i = 2; System.out.println(\"the number is: \" + i);";
2
  • What exactly are you trying to achieve? Commented Oct 13, 2011 at 14:36
  • Do You want to execute the code in the String ? Commented Oct 13, 2011 at 14:36

3 Answers 3

3

You would probably try to use the JavaCompiler interface...

Search for JavaCompiler and ToolProvider (which is what you typically use to get hold of an instance of the interface) for tutorials etc.

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

Comments

2

An alternative could be the Beanshell

Comments

1

Use Groovy!

String s = "int i = 2; System.out.println(\"the number is: \" + i);";
Binding binding = new Binding();
GroovyShell shell = new GroovyShell(binding);
Object value = shell.evaluate(s);

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.