pepe1
pepe1

Reputation: 235

Compile external Java file during runtime using Eclipse

Is it possible to dynamically compile an external .java file using the Java compiler that eclipse uses rather than JDK?

What I want my code to do is:

  1. Dynamically create a .java file
  2. Compile that .java file
  3. Use reflection to get info about this file

I know how to do steps 1 and 3 but is step 2 possible just using Eclipse or are there better ways to accomplish what I want?

Upvotes: 0

Views: 115

Answers (2)

pepe1
pepe1

Reputation: 235

I ended up using Eclipse's batchCompiler found in org.eclipse.jdt.core.compiler.batch.BatchCompiler: https://help.eclipse.org/2019-03/index.jsp?topic=%2Forg.eclipse.jdt.doc.user%2Ftasks%2Ftask-using_batch_compiler.htm

Upvotes: 0

howlger
howlger

Reputation: 34137

Yes, you can use the Java compiler for that. You do not need to compile the .java file, just parse it to get an abstract syntax tree (AST) containing the information:

  1. Add the required Eclipse compiler JARs as dependencies to your Java application
  2. Create an AST and get the information you want, e.g. like in this example

Upvotes: 1

Related Questions