Phiction
Phiction

Reputation: 2487

Java interpreter code at runtime

We have discussed how java is first compiled into Java bytecode and then interpreted by the JVM. Build into the program we are using (Dr Java), there is a panel called Interactions where you can type code in real time and have it be interpreted and ran (I believe that is how it works). I was wondering if it was possible to have a compiled program in java be ran, and then allow a user to input java code to be interpreted to modify the things that happen. I can't really think of any practical uses of this, but here is an example to clarify:

User runs a program and an integer in initialized with the value of 2 and the name of changeNumber. A pop-up comes up allowing the user to input some java code. They can input something like - "changeNumber = changeNumber + 2;" and have the code execute in real time where if you ended up printing out changeNumber, you would get 4.

Upvotes: 4

Views: 620

Answers (1)

Uku Loskit
Uku Loskit

Reputation: 42040

This is possible using the Reflection API.

As a side note, I do not understand the downvotes. This is a good and well-written question for a beginner.

Upvotes: 3

Related Questions