0

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?

2
  • 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. Commented Mar 22, 2020 at 23:07
  • Do you mind explaining how I would go about doing that? Commented Mar 23, 2020 at 0:26

2 Answers 2

1

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
Sign up to request clarification or add additional context in comments.

Comments

0

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

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.