How to execute Java string as Java code?[SOLVED] | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to execute Java string as Java code?[SOLVED]

For example: ----String code = "System.out.println('hello');"; ----//Some function and for example let's set the function as exec() and now : ----exec(code); //OUTPUT: hello

21st Apr 2020, 6:56 AM
Learn Big - LB
Learn Big - LB - avatar
24 Answers
+ 1
I have done some experiments with jshell in the meantime. It is a command line tool that comes with jdk 9+ and it is a Java REPL interpreter, but it can also run scripts written in Java. I wrote a sample that saves some code in a .jsh file, then invokes jshell on this script via Runtime.exec. It works fine on my windows pc, sadly SL gives an error I can't solve. But anyway this looks like a cool hack to use Java as scripting language. https://code.sololearn.com/cP15lPiSJOpz/?ref=app
25th Apr 2020, 8:19 PM
Tibor Santa
Tibor Santa - avatar
+ 3
Learn Big - LB Ok but you can write in one reply also. Don't Spam. Btw the below code will give error. String code = System.out.println("hello");
21st Apr 2020, 7:10 AM
A͢J
A͢J - avatar
+ 3
You can't do that in Java. It is only possible in interpreter languages like Python, JavaScript... Java is not interpreter language. If you want, you can write source code to file from your program, run compiler to translate it to bytecode and then execute it. But it will be the different program.
21st Apr 2020, 7:57 AM
andriy kan
andriy kan - avatar
+ 2
Are you asking for solution or giving any task. If you are giving any task here then this is not right place. Post in your Personal Feed Posts if it is task. https://www.sololearn.com/discuss/1316935/?ref=app
21st Apr 2020, 7:06 AM
A͢J
A͢J - avatar
+ 2
No, C# is not interpreter. In your example the whole class(!) is compiled but not a statement. It's like your compile another standalone module. CompileAssemblyFromSource() runs a compiler to compile a passed source code. It uses similar steps to compile the given code as I wrote! You asked to exec a statement: exec("System.out.println('hello');"); You can't do it. As I wrote, it is only possible in interpreter language. Or you can write your own class with CompileAssemblyFromSource method to compile source code at runtime which writes given string to a temp file and runs a compiler to compile it.
21st Apr 2020, 8:36 AM
andriy kan
andriy kan - avatar
+ 1
Learn Big - LB I understood right but you mentioned Java as a tag then how I could understand that in which Language you are talking.
21st Apr 2020, 8:23 AM
A͢J
A͢J - avatar
+ 1
Learn Big - LB Not possible in Java as you want. Can you show what you had tried?
21st Apr 2020, 8:26 AM
A͢J
A͢J - avatar
+ 1
a.) You can use Runtime.exec() to compile and then run a Java program. If you have the source code in a String, you would have to write it into a file first. https://www.journaldev.com/937/compile-run-java-program-another-java-program b.) javax.tools.JavaCompiler interface is a little better. https://www.rgagnon.com/javadetails/java-0039.html This one is closest to your C# example. c.) third party library org.joor.Reflect https://www.javadoc.io/doc/org.jooq/joor/0.9.9/org/joor/Reflect.html https://github.com/jOOQ/jOOR
23rd Apr 2020, 3:12 AM
Tibor Santa
Tibor Santa - avatar
0
I M asking for question
21st Apr 2020, 7:08 AM
Learn Big - LB
Learn Big - LB - avatar
0
And this is not a task
21st Apr 2020, 7:08 AM
Learn Big - LB
Learn Big - LB - avatar
0
I need help about this topic and also there is written that only code related stuff and this is code related stuff
21st Apr 2020, 7:09 AM
Learn Big - LB
Learn Big - LB - avatar
0
If you know anything about this then plzz help me I will appreciate it
21st Apr 2020, 7:09 AM
Learn Big - LB
Learn Big - LB - avatar
0
Thanks 😊
21st Apr 2020, 7:11 AM
Learn Big - LB
Learn Big - LB - avatar
0
Ok let me try
21st Apr 2020, 7:53 AM
Learn Big - LB
Learn Big - LB - avatar
0
It doesn't work
21st Apr 2020, 7:56 AM
Learn Big - LB
Learn Big - LB - avatar
0
I guess you understood it wrong.
21st Apr 2020, 7:56 AM
Learn Big - LB
Learn Big - LB - avatar
21st Apr 2020, 8:03 AM
Learn Big - LB
Learn Big - LB - avatar
0
Is C# interpreted? Then.
21st Apr 2020, 8:04 AM
Learn Big - LB
Learn Big - LB - avatar
0
In java
21st Apr 2020, 8:25 AM
Learn Big - LB
Learn Big - LB - avatar