1

I would like to know if there is a posibility to instance a class of an external file

For example: Oreja oreja_object = getClassFromExternalFile("C:\oreja_file.java");

Thanks, Cristina.

Andrew Spencer
  • 15,164
  • 4
  • 29
  • 48
cricap
  • 11
  • 1
  • I wonder if there is any way this can be useful. You should just add the file as source. – Peter Jaloveczki Jun 07 '13 at 09:22
  • Probably you could use JavaCompiler API – vineet Jun 07 '13 at 09:25
  • @PeterJaloveczki One use-case I could imagine is a plugin system where the end-users can add 3rd party plugins distributed as .java files to the application by putting them in some directory. – Philipp Jun 07 '13 at 09:36
  • My idea is developing an app that accepts an external .java file (or a precompiled .class file) and instantiate that object in my program. My goal is to test a generic web service dynamically. Imagine a web service that expects a json-array of 5 entities, but those entities aren't inside the project. So I take a file, create N objects and send them to the Web Services and retrieve the output. Thats the main idea – cricap Jun 07 '13 at 10:15

1 Answers1

3

Cristina, you can load external .class files dynamically using a custom ClassLoader, such as URLClassLoader. Have a look at this question.

You cannot load a .java source file directly. It must be compiled first, for example using the Java Compiler API.

Community
  • 1
  • 1