3

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?

3
  • What do you mean by "dynamic changing code"? i.e. What is the use-case here? Commented Jan 5, 2015 at 23:22
  • 1
    If you mean something like c's include directive, absolutely not. Much of Java's design is built around not having to do stuff like that. Commented Jan 5, 2015 at 23:23
  • Do you mean at compile time or at run time? If you want to include one source file from another at compile time, the answer, as jjm said, is a definite 'no' - the java language does not support this. If you're trying to include code at run time, in theory there are ways to do it, but nothing straight-forward, and this almost certainly is not a good idea. What is it that you are trying to accomplish? Commented Jan 5, 2015 at 23:28

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Just a thought: Maybe some compiling tool has this feature. For example, some time ago someone posted a question about how to remove method declarations conditionally like C does with #ifdef. I knew that with java you can't do it; what I didn't know was that with Ant you can. You can specify some "special" comments that ant has to look for before compiling, and then how it should behave when those comments are found. Maybe there is a way to do includes with Ant?
Sure, if there's some kind of pre-compilation process to generate code, then that would work, but it would then mean that everyone else would have to follow your build process, and your IDE wouldn't work properly with those files, etc. One possibility might be to have an initial preprocess step that generates source from a non-Java format (BNF grammar or what have you) & then use normal Java from then on. But then that's not really an #include like OP wanted.
0

Java has no equivalent of eval, or any other construct that is capable of evaluating code at runtime. It is perfectly capable of parsing external data, such as XML, and taking action on what it finds. It can also start and capture output from external processes, which could very well be dynamically generated code. The details of this process vary depending on your OS.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.