Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I insert java file inside of code in Java

Tags:

java

I wanted to know if there was a way to import the code of dynamic changing code into the main coding. Something like this:

Main:
int x;
(insert input.java)

Contents of input.java:

x = 2;

Can I import the code inside input.java to the main code?

like image 205
seventy70 Avatar asked Feb 04 '26 05:02

seventy70


1 Answers

Java does not have include directives at source compilation time.

Neither does it have an eval for evaluating new Java source code at runtime.

The only real way to get new Java code into a running Java VM is via classloading.

However, since the advent of javax.script (Java 6), a sufficiently supported runtime (e.g. the Nashorn Javascript implementation from Java 8) could import and eval some scripting code that called back into a Java API. This is a huge subject & too big to address in depth here.