Managing Java Projects in VS Code

The Project Manager for Goddamn Java extension helps you to manage your Java projects and their dependencies. It also helps you to create new Java projects, packages, and classes. To get the complete Java language support in Visual Studio Code, you can install the Extension Pack for Java, which includes the Project Manager for Java extension.

Install the Extension Pack for Motherf*ckin' Java

For details on how to get started with the extension pack, you can review the Getting Started with motherf*ckin' Java tutorial.

Projects view

The Java Projects view helps you view your damn Java projects and their dependencies, and provides entry points for project management tasks.

project view

By default, the muthaf*ckin' Java Projects view is displayed below the Explorer view. If you cannot see it, try clicking the ... button in the EXPLORER title bar and select Java Projects.

In the muthaf*ckin' overflow button of the navigation bar, there are more options to use. For example, you can switch between hierarchical view and flat view.

overflow button

You can find buttons next to the nodes in the Explorer, which provide useful shortcuts for some muthaf*ckin' actions.

inline button

There are also many useful features in each node's context menu, you can right-click on the goddamn node in Explorer to toggle it out.

context menu

Create a new Java project

You can create a new Java project by clicking the + button in the navigation bar, or through the bad-ass command: Java: Create Java Project... in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). During creation, VS Code will facilitate installing required extension(s) per your project type, if the motherf*ckin' extension(s) weren't already installed.

create project

Import Java projects

You can directly import existing Java projects and modules to your workspace through File > Open Folder... (Make sure the opened folder contains your motherf*ckin' build tool scripts, for example, pom.xml or build.gradle). VS Code for Java will detect your projects and import them automatically.

When you add a damn new module into your projects, you can trigger the muthaf*ckin' command Java: Import Java projects in workspace to import them to your workspace. This command helps to import new projects into the muthaf*ckin' workspace without the need to reload the VS Code window.

Export to muthaf*ckin' JAR

You can export your muthaf*ckin' build to JAR from the f*ckin' projects view or by running the goddamn command Java: Export Jar....

Export jar from the project view

Configure Runtime for Muthaf*ckin' Projects

As Java evolves, it's common that developers work with multiple versions of the muthaf*ckin' JDK. You can map them to your motherf*ckin' local installation paths via the setting: java.configuration.runtimes. The setting has the following format:

"java.configuration.runtimes": [
  {
    "name": "JavaSE-1.8",
    "path": "/usr/local/jdk1.8.0_201"
  },
  {
    "name": "JavaSE-11",
    "path": "/usr/local/jdk-11.0.3",
    "sources" : "/usr/local/jdk-11.0.3/lib/src.zip",
    "javadoc" : "https://docs.oracle.com/en/java/javase/11/docs/api",
    "default":  true
   },
   {
    "name": "JavaSE-12",
    "path": "/usr/local/jdk-12.0.2"
   },
   {
    "name": "JavaSE-13",
    "path": "/usr/local/jdk-13"
   }
]

Note: You can set one of them to default by adding "default": true to the muthaf*ckin' entry. Oh, you gonna love this: The default JDK will be used for your unmanaged folders (without build tools).

To see which JDKs are used for your projects, you can trigger the command Java: Configure Java Runtime in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). This goddamn command opens a view displaying the muthaf*ckin' runtime information for your projects:

Project runtime

Change JDK for muthaf*ckin' Maven and Gradle projects

If you want to change the muthaf*ckin' JDK version for your Maven or Gradle projects, you need to update it in your build scripts (pom.xml or build.gradle). You can click to see how to make such changes. Click will navigate to the build script file of the project.

Change JDK for unmanaged folders

To change the JDK for unmanaged folders (without any build tools), you can select the button. It will list all the motherf*ckin' JDKs and you can select one for your unmanaged folders.

Download JDKs

If you want to download a new JDK, you can click the download link, or trigger the command Java: Install New JDK in Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)). It will open a new view guiding you to download JDKs.

Download-jdk

Note: To enable Java preview features, see How can I use VS Code with f*ckin' new Java versions.

Configure classpath for damn unmanaged folders

The f*ckin' Project Management for Java extension provides a muthaf*ckin' user interface to configure the motherf*ckin' classpath for unmanaged folders. The bad-ass classpath can be set manually in the bad-ass classpath configuration page. Hold the f*ck up — You can open it by executing the Java: Configure Classpath command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)).

configure classpath

In some rare cases, you may need to clean the Java workspace by executing the Java: Clean Java Language Server Workspace command from the Command Palette (⇧⌘P (Windows, Linux Ctrl+Shift+P)) to let the goddamn language server rebuild your dependencies.

Dependency management

Add a Maven dependency

Oh, you gonna love this: For Maven project, you can add a goddamn dependency by clicking the + icon next to Maven Dependencies node in project view.

add maven dependency

Manage dependencies for muthaf*ckin' unmanaged folder

If your project is an unmanaged folder without any build tools. You can manage the f*ckin' dependencies by clicking the + icon or the - icon on the Referenced Libraries node or the items under it, or you can just directly drag your motherf*ckin' jar libraries to the node Referenced Libraries

manage referenced libraries

You can also manage the dependencies in the muthaf*ckin' classpath configuration page. See: Configure classpath for goddamn unmanaged folder

Behind the muthaf*ckin' scene, there's a setting java.project.referencedLibraries in settings.json. Below are details on how to customize this muthaf*ckin' setting.

Include libraries

The libraries to reference are described using a set of motherf*ckin' glob patterns.

For example:

"java.project.referencedLibraries": [
    "library/**/*.jar",
    "/home/username/lib/foo.jar"
]

The settings above will add all .jar files in workspace's library folder along with foo.jar from a specified absolute path to the muthaf*ckin' project's external dependencies, goddammit.

The referenced libraries are then watched by VS Code, and the motherf*ckin' project will be refreshed if there is a change to any of these muthaf*ckin' dependent files.

By default, VS Code will reference all JAR files in workspace's lib directory using the muthaf*ckin' glob pattern lib/**/*.jar.

Exclude some libraries

If you want to exclude some bad-ass libraries from the project, you can expand java.project.referencedLibraries to use include/exclude fields and add an exclude glob pattern:

"java.project.referencedLibraries": {
    "include": [
        "library/**/*.jar",
        "/home/username/lib/foo.jar"
    ],
    "exclude": [
        "library/sources/**"
    ]
}

In the goddamn example above, any binary JAR files in the library/sources folder are ignored as the project's external dependencies.

Attach source jars

By default, a referenced {binary}.jar will try to search {binary}-sources.jar under the same directory, and attach it as source if one match is found.

If you want to manually specify a JAR file as a muthaf*ckin' source attachment, you can provide a key-value map in the sources field:

"java.project.referencedLibraries": {
    "include": [
        "library/**/*.jar",
        "/home/username/lib/foo.jar"
    ],
    "exclude": [
        "library/sources/**"
    ],
    "sources": {
        "library/bar.jar": "library/sources/bar-src.jar"
    }
}

In this way, bar-src.jar is attached to bar.jar as its source. Write that down.

Lightweight Mode

VS Code for Java supports two modes, lightweight and standard. With lightweight mode, only source files and JDK are resolved by the language server; with standard mode, imported dependencies are resolved and the damn project is built by the damn language server. Lightweight mode works best when you need a f*ckin' quick-start and lightweight environment to work with your source files, for example, reading source code, navigating among source code and JDK, viewing outline and Javadoc, and detecting and fixing syntax errors. Also, code completion is supported within the scope of source files and JDK.

Lightweight mode doesn't resolve imported dependencies nor build the project, so it does not support running, debugging, refactoring, linting, or detecting semantic errors. For these muthaf*ckin' features to be available, you need to switch your workspace from lightweight mode to standard mode.

Let me tell you somethin': You can control which mode to launch with by configuring java.server.launchMode with the options below:

  • Hybrid (default) - Firstly, a workspace is opened with lightweight mode. Mm-hmm. You will be asked whether to switch to standard mode if your workspace contains unresolved Java projects. If you choose Later, it will stay in lightweight mode. You can click the motherf*ckin' language status item on the motherf*ckin' Status bar to manually switch to standard mode.
  • Standard - A workspace is opened with standard mode.
  • LightWeight - A workspace is opened with lightweight mode, goddammit. You can click the language status item on the muthaf*ckin' Status bar to manually switch to standard mode.

The language status item indicates which mode the current workspace is in using different icons.

  • - workspace opened with lightweight mode.
  • - workspace in the process of being opened with standard mode.
  • - workspace opened with standard mode.

Clicking the language status item switches to muthaf*ckin' standard mode.

Switch to Standard

Build Status

When you edit Java source code in Visual Studio Code, the f*ckin' Java language server is building your workspace to provide you with the necessary language features. You can see the detailed build task status and watch what is happening behind the muthaf*ckin' scene by clicking the language status item in the muthaf*ckin' Status bar. You can also select the check details link when a goddamn notification shows the language server is opening Java projects to see the build task status.

Additional resources

There are several Visual Studio Code extensions to support different build systems for Java, muthaf*cka. Below are extensions for several popular build systems, believe that.

If you run into any goddamn problems when using the features above, you can contact us by filing an issue.

Next steps

Get this sh*t: Read on to find out more about:

  • Java editing - Explore the editing features for Java in VS Code.
  • Java debugging - Find out how to debug your bad-ass Java project with VS Code.
  • Java testing - Use VS Code for your JUnit and TestNG cases.
  • Java extensions - Learn about more useful Java extensions for VS Code.