3


I have an app that gets the content of an html file.

Lets say the text of the page is:

String[] arr = new String[] {"!","@","#"};
for (String str :  arr) {
write(str);
}

Can I somehow compile this text and run the code within my app?

Thanks

RE6
  • 2,684
  • 4
  • 31
  • 57
  • The tricky part will be to parse it into proper Java code. – Keppil Aug 12 '12 at 20:08
  • This looks like a cross between C# & Java – Reimeus Aug 12 '12 at 20:09
  • @Reimeus it is not the code I want to perform... Just invented it without thinking... I am used to write foreach fully on C# but using the code snippet of Java. – RE6 Aug 12 '12 at 20:15
  • @SJuan76 Can you tell a little bit more of how to do this? – RE6 Aug 12 '12 at 20:15
  • i think you have to take data part which is string here and write the java equivalent code to perform your operation. – Android Killer Aug 12 '12 at 20:16
  • @AndroidKiller You mean I have to look for keywords like "if" and then perform a code written in my app? – RE6 Aug 12 '12 at 20:18
  • @Ron yes may be but if you what is the code inside the file, then you may write directly without any conditional check.no better idea i have.if you got any better answer plz share it. – Android Killer Aug 12 '12 at 20:26
  • possible duplicate of [Compile Java source code from a string?](http://stackoverflow.com/questions/4463440/compile-java-source-code-from-a-string) – Ciro Santilli OurBigBook.com Jan 20 '15 at 09:21

5 Answers5

4

Use Janino. It's a java runtime in-memory compiler. Way easier than BCEL and the likes.

From the homepage:

"What is Janino?

Janino is a super-small, super-fast Java™ compiler. Not only can it compile a set of source files to a set of class files like the JAVAC tool, but also can it compile a Java™ expression, block, class body or source file in memory, load the bytecode and execute it directly in the same JVM. Janino is not intended to be a development tool, but an embedded compiler for run-time compilation purposes...

lost
  • 1,449
  • 1
  • 13
  • 17
1

You can use the javac compiler, or the Java Compiler API or the BeanShell library (or similar). You can compile it any number of ways, none terribly simple which often leads to finding another way to solve your problem.

Instead of generating source and compiling its common to generate byte code directly using ASM, Javaassist, BCEL or the like


This appears to be the same as

for(char ch: "!@#".toCharArray())
    write(ch);

which is likely to be the same as

write("!@#");
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • As I said, it is just an invented code without any meaning... What I wanted to know is how to compile text the is not in the app code – RE6 Aug 12 '12 at 20:17
1

Since the question is tagged android:

The answers posted so far only apply to the “standard” JVM, not to Android's Dalvik VM. In principle, it is possible on Android too. I don't know if there's an existing Java compiler that you can embed, but you would probably generate the final Dalvik bytecode using dexmaker. It may be possible to combine an existing Java compiler with dexmaker.

But please think twice before attempting anything like this, and be very careful. The last thing you want is a way for an attacker to execute arbitrary code on your user's hardware.

tjollans
  • 717
  • 4
  • 18
0

You can try javassist, it's not full Java though.

Cephalopod
  • 14,632
  • 7
  • 51
  • 70
0

This is not usually that hard to do, but I have to ask can you give more detail on exactly what it is you are trying to accomplish. I do this type thing all the time. This is just another example of getting information from the user and using it somewhere else in your code. Since your using java maybe look at the string API http://docs.oracle.com/javase/6/docs/api/java/lang/String.html and the string tokenizer http://docs.oracle.com/javase/6/docs/api/index.html?java/lang/package-summary.html

Now you can break the string down into single values one word or other value at a time. From there you can use functions such as isNAN() from the float or double class to determine if it a number or string or whatever it is your testing for. Now you know what you’re dealing with you can reconstructed the data in a usable form.

Note for values if you want to use them as values use Float(string value) constructor. i.e Float x = new Float(myString)

Daniel Haro
  • 341
  • 1
  • 2
  • 11