Java Code Examples for javax.tools.StandardJavaFileManager#setLocationFromPaths()

The following examples show how to use javax.tools.StandardJavaFileManager#setLocationFromPaths() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException if there is an I/O error scanning the source tree or
 *                     creating the destination directory
 */
public static boolean compile(Path source, Path destination, String ... options)
    throws IOException
{
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
            (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
                             Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
        = compiler.getTask(null, jfm, null, opts, null,
            jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 2
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException if there is an I/O error scanning the source tree or
 *                     creating the destination directory
 */
public static boolean compile(Path source, Path destination, String... options)
    throws IOException
{
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
                     (file, attrs) -> (file.toString().endsWith(".java")))
               .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
                             Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
        = compiler.getTask(null, jfm, null, opts, null,
                           jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 3
/**
 * Compile the specified module from the given module sourcepath
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException if there is an I/O error scanning the source tree or
 *                     creating the destination directory
 */
public static boolean compileModule(Path source, Path destination,
                                    String moduleName, String... options) {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    try {
        Files.createDirectories(destination);
        jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
                                 Arrays.asList(destination));
    } catch (IOException e) {
        throw new UncheckedIOException(e);
    }

    Stream<String> opts = Arrays.stream(new String[] {
        "--module-source-path", source.toString(), "-m", moduleName
    });
    List<String> javacOpts = Stream.concat(opts, Arrays.stream(options))
                                    .collect(Collectors.toList());
    JavaCompiler.CompilationTask task
        = compiler.getTask(null, jfm, null, javacOpts, null, null);
    return task.call();
}
 
Example 4
private static boolean compile(Path source, Path destination, String... options)
        throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        // no compiler available
        throw new UnsupportedOperationException("Unable to get system java compiler. "
                + "Perhaps, jdk.compiler module is not available.");
    }
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
            = Files.find(source, Integer.MAX_VALUE,
                    (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
            Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
            = compiler.getTask(null, jfm, null, opts, null,
                    jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 5
private static boolean compile(Path source, Path destination, String... options)
        throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        // no compiler available
        throw new UnsupportedOperationException("Unable to get system java compiler. "
                + "Perhaps, jdk.compiler module is not available.");
    }
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
            = Files.find(source, Integer.MAX_VALUE,
                    (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
            Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
            = compiler.getTask(null, jfm, null, opts, null,
                    jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 6
/**
  * Compile all the java sources in {@code <source>} and optionally its
  * subdirectories, to
  * {@code <destination>}. The destination directory will be created if
  * it doesn't exist.
  *
  * All warnings/errors emitted by the compiler are output to System.out/err.
  *
  * @param source Path to the source directory
  * @param destination Path to the destination directory
  * @param recurse If {@code true} recurse into any {@code source} subdirectories
  *        to compile all java source files; else only compile those directly in
  *        {@code source}.
  * @param options Any options to pass to the compiler
  *
  * @return true if the compilation is successful
  *
  * @throws IOException
  *         if there is an I/O error scanning the source tree or
  *         creating the destination directory
  * @throws UnsupportedOperationException
  *         if there is no system java compiler
  */

public static boolean compile(Path source, Path destination, boolean recurse, String... options)
     throws IOException
 {
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
     if (compiler == null) {
         // no compiler available
         throw new UnsupportedOperationException("Unable to get system java compiler. "
                 + "Perhaps, jdk.compiler module is not available.");
     }
     StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

     List<Path> sources
         = Files.find(source, (recurse ? Integer.MAX_VALUE : 1),
             (file, attrs) -> (file.toString().endsWith(".java")))
             .collect(Collectors.toList());

     Files.createDirectories(destination);
     jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
     jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
             Collections.singletonList(destination));

     List<String> opts = Arrays.asList(options);
     JavaCompiler.CompilationTask task
         = compiler.getTask(null, jfm, null, opts, null,
             jfm.getJavaFileObjectsFromPaths(sources));

     return task.call();
 }
 
Example 7
private static boolean compile(Path source, Path destination, String... options)
        throws IOException {
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        // no compiler available
        throw new UnsupportedOperationException("Unable to get system java compiler. "
                + "Perhaps, jdk.compiler module is not available.");
    }
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
            = Files.find(source, Integer.MAX_VALUE,
                    (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
            Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
            = compiler.getTask(null, jfm, null, opts, null,
                    jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 8
/**
  * Compile all the java sources in {@code <source>} and optionally its
  * subdirectories, to
  * {@code <destination>}. The destination directory will be created if
  * it doesn't exist.
  *
  * All warnings/errors emitted by the compiler are output to System.out/err.
  *
  * @param source Path to the source directory
  * @param destination Path to the destination directory
  * @param recurse If {@code true} recurse into any {@code source} subdirectories
  *        to compile all java source files; else only compile those directly in
  *        {@code source}.
  * @param options Any options to pass to the compiler
  *
  * @return true if the compilation is successful
  *
  * @throws IOException
  *         if there is an I/O error scanning the source tree or
  *         creating the destination directory
  * @throws UnsupportedOperationException
  *         if there is no system java compiler
  */

public static boolean compile(Path source, Path destination, boolean recurse, String... options)
     throws IOException
 {
     JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
     if (compiler == null) {
         // no compiler available
         throw new UnsupportedOperationException("Unable to get system java compiler. "
                 + "Perhaps, jdk.compiler module is not available.");
     }
     StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

     List<Path> sources
         = Files.find(source, (recurse ? Integer.MAX_VALUE : 1),
             (file, attrs) -> (file.toString().endsWith(".java")))
             .collect(Collectors.toList());

     Files.createDirectories(destination);
     jfm.setLocation(StandardLocation.CLASS_PATH, Collections.emptyList());
     jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
             Collections.singletonList(destination));

     List<String> opts = Arrays.asList(options);
     JavaCompiler.CompilationTask task
         = compiler.getTask(null, jfm, null, opts, null,
             jfm.getJavaFileObjectsFromPaths(sources));

     return task.call();
 }
 
Example 9
void test_setPaths_getFiles(StandardJavaFileManager fm, List<Path> inPaths) throws IOException {
    System.err.println("test_setPaths_getFiles");
    JavaFileManager.Location l = newLocation();
    fm.setLocationFromPaths(l, inPaths);
    Iterable<? extends File> outFiles = fm.getLocation(l);
    compare(inPaths, outFiles);
}
 
Example 10
void test_setPaths_getPaths(StandardJavaFileManager fm, List<Path> inPaths) throws IOException {
    System.err.println("test_setPaths_getPaths");
    JavaFileManager.Location l = newLocation();
    fm.setLocationFromPaths(l, inPaths);
    Iterable<? extends Path> outPaths = fm.getLocationAsPaths(l);
    compare(inPaths, outPaths);
}
 
Example 11
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException
 *         if there is an I/O error scanning the source tree or
 *         creating the destination directory
 * @throws UnsupportedOperationException
 *         if there is no system java compiler
 */
public static boolean compile(Path source, Path destination, String ... options)
    throws IOException
{
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    if (compiler == null) {
        // no compiler available
        throw new UnsupportedOperationException("Unable to get system java compiler. " +
            "Perhaps, jdk.compiler module is not available.");
    }
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
            (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocation(StandardLocation.CLASS_PATH, Collections.EMPTY_LIST);
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
                             Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
        = compiler.getTask(null, jfm, null, opts, null,
            jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}
 
Example 12
/**
 * Handles the {@code --release} option.
 *
 * @param additionalOptions a predicate to handle additional options implied by the
 * {@code --release} option. The predicate should return true if all the additional
 * options were processed successfully.
 * @return true if successful, false otherwise
 */
public boolean handleReleaseOptions(Predicate<Iterable<String>> additionalOptions) {
    String platformString = options.get(Option.RELEASE);

    checkOptionAllowed(platformString == null,
            option -> error("err.release.bootclasspath.conflict", option.getPrimaryName()),
            Option.BOOT_CLASS_PATH, Option.XBOOTCLASSPATH, Option.XBOOTCLASSPATH_APPEND,
            Option.XBOOTCLASSPATH_PREPEND,
            Option.ENDORSEDDIRS, Option.DJAVA_ENDORSED_DIRS,
            Option.EXTDIRS, Option.DJAVA_EXT_DIRS,
            Option.SOURCE, Option.TARGET,
            Option.SYSTEM, Option.UPGRADE_MODULE_PATH);

    if (platformString != null) {
        PlatformDescription platformDescription = PlatformUtils.lookupPlatformDescription(platformString);

        if (platformDescription == null) {
            error("err.unsupported.release.version", platformString);
            return false;
        }

        options.put(Option.SOURCE, platformDescription.getSourceVersion());
        options.put(Option.TARGET, platformDescription.getTargetVersion());

        context.put(PlatformDescription.class, platformDescription);

        if (!additionalOptions.test(platformDescription.getAdditionalOptions()))
            return false;

        Collection<Path> platformCP = platformDescription.getPlatformPath();

        if (platformCP != null) {
            JavaFileManager fm = getFileManager();

            if (!(fm instanceof StandardJavaFileManager)) {
                error("err.release.not.standard.file.manager");
                return false;
            }

            try {
                StandardJavaFileManager sfm = (StandardJavaFileManager) fm;

                if (Source.instance(context).allowModules()) {
                    sfm.handleOption("--system", Arrays.asList("none").iterator());
                    sfm.setLocationFromPaths(StandardLocation.UPGRADE_MODULE_PATH, platformCP);
                } else {
                    sfm.setLocationFromPaths(StandardLocation.PLATFORM_CLASS_PATH, platformCP);
                }
            } catch (IOException ex) {
                log.printLines(PrefixKind.JAVAC, "msg.io");
                ex.printStackTrace(log.getWriter(WriterKind.NOTICE));
                return false;
            }
        }
    }

    return true;
}
 
Example 13
/**
 * Compile all the java sources in {@code <source>/**} to
 * {@code <destination>/**}. The destination directory will be created if
 * it doesn't exist.
 *
 * All warnings/errors emitted by the compiler are output to System.out/err.
 *
 * @return true if the compilation is successful
 *
 * @throws IOException if there is an I/O error scanning the source tree or
 *                     creating the destination directory
 */
public static boolean compile(Path source, Path destination, String ... options)
    throws IOException
{
    JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
    StandardJavaFileManager jfm = compiler.getStandardFileManager(null, null, null);

    List<Path> sources
        = Files.find(source, Integer.MAX_VALUE,
            (file, attrs) -> (file.toString().endsWith(".java")))
            .collect(Collectors.toList());

    Files.createDirectories(destination);
    jfm.setLocation(StandardLocation.CLASS_PATH, Collections.EMPTY_LIST);
    jfm.setLocationFromPaths(StandardLocation.CLASS_OUTPUT,
                             Arrays.asList(destination));

    List<String> opts = Arrays.asList(options);
    JavaCompiler.CompilationTask task
        = compiler.getTask(null, jfm, null, opts, null,
            jfm.getJavaFileObjectsFromPaths(sources));

    return task.call();
}