+ 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
24 Réponses
+ 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
+ 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");
+ 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.
+ 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
+ 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.
+ 2
You may find answer in https://www.google.com/search?q=how+to+execute+java+string+as+java+code&rlz=1C9BKJA_enUS705US705&oq=how+to+execute+java+string+as+java+code&aqs=chrome..69i57j0.37722j0j4&hl=en-US&sourceid=chrome-mobile&ie=UTF-8.
You may go throuth https://stackoverflow.com/questions/45820995/execute-code-from-a-string-in-java, (wherein same query was hitherto answered in Quora), to find an answer for your query.
+ 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.
+ 1
Learn Big - LB Not possible in Java as you want. Can you show what you had tried?
+ 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
0
I M asking for question
0
And this is not a task
0
I need help about this topic and also there is written that only code related stuff and this is code related stuff
0
If you know anything about this then plzz help me I will appreciate it
0
Thanks 😊
0
Ok let me try
0
It doesn't work
0
I guess you understood it wrong.
0
Is C# interpreted? Then.
0
In java