Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.92 KB | None | 0 0
  1. import info.kgeorgiy.java.advanced.implementor.BaseImplementorTest;
  2.  
  3. private void compileClass(Path fileName, Path root) throws ImplerException {
  4.     JavaCompiler javaCompiler = ToolProvider.getSystemJavaCompiler();
  5.     if (javaCompiler == null) {
  6.         throw new ImplerException("Java compiler not found");
  7.     }
  8.     final List<String> args = new ArrayList<>();
  9.     args.add(fileName.toString());
  10.     args.add("-cp");
  11.     args.add(root + File.pathSeparator + getClassPath());
  12.     int returnCode = javaCompiler.run(null, null, null, args.toArray(String[]::new));
  13.     if (returnCode != 0) {
  14.         throw new ImplerException("Compilation failed");
  15.     }
  16. }  
  17.  
  18. private static String getClassPath() {
  19.     try {
  20.         return Path.of(BaseImplementorTest.class.getProtectionDomain().getCodeSource().getLocation().toURI()).toString();
  21.     } catch (final URISyntaxException e) {
  22.         throw new AssertionError(e);
  23.     }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement