Reputation: 431
I am using the interface that java provides to invoke the compilers from programs.
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
StandardJavaFileManager fileManager = compiler.getStandardFileManager(null, null, null);
Iterable<? extends JavaFileObject> compilationUnits = fileManager.getJavaFileObjectsFromStrings(Arrays.asList("MyClass.java"));
compiler.getTask(null, fileManager, null, null, null, compilationUnits).call();
fileManager.close();
Everything is working fine but what I want to be able to do is pass in variables to the project that i am compiling and compare the results to see if they are what is expected so i no the program is working properly. Kind of like how JUnit works to test classes. Is there methods in the JavaCompiler interface that allow you to pass in variables and read them then... something like System.in()
and System.out()
?
Upvotes: 0
Views: 118
Reputation: 7402
Running a (Java) program has not much to do with compiling it. Either you dynamically load the compiled classes and run them (e.g. in a new thread), or you use the ProcessBuilder
to start a new VM, basically constructing a command line like "java -cp /class/path my.compiled.MainClass ...
". The latter is probably preferred, since the program under test can't accidentally terminate your program through System.exit()
.
Of course there's an even better solution: why don't you generate some JUnit tests programmatically (and invoke JUnit)?
Upvotes: 0