How to run java code using java code?

Basically, I want to do two things:

  • I want to know if there is a way to run Java code using Java code.
  • If possible, how can I show the output on my screen? (whether it is regular output or an error or exception)

I know this is possible because one of my seniors did it, but I don’t know how he did it. Perhaps he used one of the built-in Java classes.

Note: the user will write the code in some kind of text file, and then I will save the contents of this file in some variable, and then maybe run this code.

+2
source share
4 answers

Yes it is possible.

1:
ProcessBuilder Runtime Process, Java . ( , , Java ).

2:
. ProcessBuilder Runtime , Java-. Process getInputStream getOutputStream . , Class API Java.

+5

- (, Suns javac), .

Java API, , Java.

+1

If you want an interpreter (so compilation is not required) for Java, check out BeanShell .
I love it!

0
source

You can use a scripting language running on top of the JVM. Groovy is a very good idea, and it has very similar syntax compared to Java.

0
source

Source: https://habr.com/ru/post/1751174/


All Articles