https://github.com/kohlschutter/jdk.compiler.standalone Skip to content Toggle navigation Sign up * Product + Actions Automate any workflow + Packages Host and manage packages + Security Find and fix vulnerabilities + Codespaces Instant dev environments + Copilot Write better code with AI + Code review Manage code changes + Issues Plan and track work + Discussions Collaborate outside of code Explore + All features + Documentation + GitHub Skills + Blog * Solutions For + Enterprise + Teams + Startups + Education By Solution + CI/CD & Automation + DevOps + DevSecOps Resources + Learning Pathways + White papers, Ebooks, Webinars + Customer Stories + Partners * Open Source + GitHub Sponsors Fund open source developers + The ReadME Project GitHub community articles Repositories + Topics + Trending + Collections * Pricing Search or jump to... Search code, repositories, users, issues, pull requests... Search [ ] Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. [ ] [ ] Include my email address so I can be contacted Cancel Submit feedback Saved searches Use saved searches to filter your results more quickly Name [ ] Query [ ] To see all available qualifiers, see our documentation. Cancel Create saved search Sign in Sign up You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} kohlschutter / jdk.compiler.standalone Public * Notifications * Fork 0 * Star 15 Standalone jdk.compiler / JDK javac Compiler Framework + Compiler Tree API License GPL-2.0 license 15 stars 0 forks Activity Star Notifications * Code * Issues 0 * Pull requests 0 * Actions * Projects 0 * Security * Insights More * Code * Issues * Pull requests * Actions * Projects * Security * Insights kohlschutter/jdk.compiler.standalone This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository. main Switch branches/tags [ ] Branches Tags Could not load branches Nothing to show {{ refName }} default View all branches Could not load tags Nothing to show {{ refName }} default View all tags Name already in use A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch? Cancel Create 1 branch 1 tag Code * Local * Codespaces * Clone HTTPS GitHub CLI [https://github.com/k] Use Git or checkout with SVN using the web URL. [gh repo clone kohlsc] Work fast with our official CLI. Learn more about the CLI. * Open with GitHub Desktop * Download ZIP Sign In Required Please sign in to use Codespaces. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching GitHub Desktop If nothing happens, download GitHub Desktop and try again. Launching Xcode If nothing happens, download Xcode and try again. Launching Visual Studio Code Your codespace will open once ready. There was a problem preparing your codespace, please try again. Latest commit @kohlschuetter kohlschuetter Update README ... fe5e53c Oct 10, 2023 Update README fe5e53c Git stats * 25 commits Files Permalink Failed to load latest commit information. Type Name Latest commit message Commit time jdk.compiler jdk11: Add standalone-home dependency October 9, 2023 00:10 standalone-jdk11 Fix pom dependency October 10, 2023 14:46 standalone-test version 1.0.0 October 10, 2023 14:42 standalone-util pom: Add license and scm info October 10, 2023 14:44 .gitignore Add README and .gitignore October 7, 2023 13:58 .gitmodules Add jdk.compiler submodule October 7, 2023 14:00 ADDITIONAL_LICENSE_INFO Add LICENSE etc. (GPLv2+Classpath exception) October 7, 2023 14:00 ASSEMBLY_EXCEPTION Add LICENSE etc. (GPLv2+Classpath exception) October 7, 2023 14:00 LICENSE Add LICENSE etc. (GPLv2+Classpath exception) October 7, 2023 14:00 NOTICE Add NOTICE file October 7, 2023 16:47 README.md Update README October 10, 2023 19:21 pom.xml pom: Add license and scm info October 10, 2023 14:44 View code [ ] Standalone jdk.compiler / Java Compiler Framework + Tree API What Why Motivation Benefits Examples How Usage Project setup and structure Limitations Building from source When Future enhancements Who License README.md Standalone jdk.compiler / Java Compiler Framework + Tree API What This repository builds standalone jdk.compiler artifacts (the Java code behind javac, etc.) that can be used just like other regular Maven dependencies. Why Motivation A typical way of using the Java Compiler API is to rely on the presence of such API in the Java VM that runs the code requiring it. Starting with Java 16, using the internals of the Java Compiler API requires a series of --add-opens incantations to the running VM, such as: --add-opens=jdk.compiler/com.sun.tools.javac.tree=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.code=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.model=ALL-UNNAMED --add-opens=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED etc. The reason is clear: Eventually, we cannot rely on the presence of said API in the JDK, since jdk.compiler is an internal module that we really shouldn't touch. Since the Compiler Framework code is licensed as GPLv2+Classpath-Exception, let's make it a separate component that we can use regardless of what's available in the current VM! Benefits With this standalone compiler, you can rely on all Java 11 javac features to be available, even when using newer Java versions. Specifically, this is allows you to: * compile code even if your Java environment isn't a full JDK (Java JRE, for example!) * target Java 1.7 for compilation without any warnings or restrictions. * use the Compiler Tree API without resorting to --add-opens trickery that may eventually fail in newer Java releases * build a modified compiler with additional features or custom tweaks Examples See this jsweet fork. jsweet makes exhaustive use of the Compiler Tree API, and now we can run it in Java 17 without prying open some JDK internals. How Usage First, add the following Maven dependency to your project: com.kohlschutter.jdk.compiler standalone-jdk11 1.0.0 If your project is modularized, also add the following statements to your module-info.java: requires standalone.jdk.compiler; requires com.kohlschutter.jdk.standaloneutil; This gives you access to all com.sun.tools.* and com.sun.source.* packages, however they are actually prefixed by standalone., i.e., standalone.com.sun.tools.*, etc. So you need to change your code to use standalone.com.sun.... instead of com.sun.... For example use standalone.com.sun.tools.javac.api.JavacTool instead of com.sun.tools.javac.api.JavacTool. If you use javax.tools.ToolProvider.getSystemJavaCompiler(), change this to our own version: com.kohlschutter.jdk.standaloneutil.ToolProvider.getSystemJavaCompiler () (or just modify the import statement). The original Compiler framework refers to certain files from the JDK's home directory, specifically lib/modules (which contains all default modules), as well as lib/ct.sym, which contains the API fingerprints to support -release compatibility checks. The standalone compiler uses its own copies for both lib/modules contents as well as lib/ct.sym from a recent JDK 11 java home directory, which is automatically included via the com.kohlschutter.jdk:standalone-home dependency. Project setup and structure The code in this repository relies on copies of the "jdk.compiler" code obtained from Open Source Java JDKs (for example, Eclipse Temurin). These copies reside in a separate repository and are added as submodules to this project. Each submodule refers to a different JDK version, which allows the creation of multiple Maven artifacts, one for each major JDK version. Limitations To use this artifact, you currently need Java 11 or better. Building from source Clone this repository, initialize submodules and build: git clone https://github.com/kohlschutter/jdk.compiler.standalone.git cd jdk.compiler.standalone.git git submodule update --init mvn clean install Also see jdk.compiler.home for the corresponding JDK home artifact. When Future enhancements * Next up is adding support for the compiler in Java 21. * With a little bit of luck, we may be able to modify the compiler code enough so we can actually run it from Java 11. * We could even support multiple different compiler versions to run side-by-side in the same VM. * By adding support for GraalVM native image, we could build javac binaries with custom configurations * This approach may be used for other jdk-internal components as well. If you have an idea, please reach out! Who This repository has been put together by Christian Kohlschutter. License The code itself carries the original license, GNU General Public License version 2 only, subject to the "Classpath" exception as provided in the LICENSE file that accompanies this code. About Standalone jdk.compiler / JDK javac Compiler Framework + Compiler Tree API Topics java compiler jdk javac Resources Readme License GPL-2.0 license Activity Stars 15 stars Watchers 1 watching Forks 0 forks Report repository Releases 1 tags Packages 0 No packages published Languages * Java 100.0% Footer (c) 2023 GitHub, Inc. Footer navigation * Terms * Privacy * Security * Status * Docs * Contact GitHub * Pricing * API * Training * Blog * About You can't perform that action at this time.