IntelliJ Plugin Development Notes
  • Plugin Development Notes for JetBrains IDEs
  • Project Setup
    • Target a Specific IDE
  • OpenAPI
    • Actions
      • DumbAwareAction
    • Options
      • SettingsEditor
  • UI
    • Navigation
    • Tool Window
    • Icons
    • Project Wizard
    • Internationalization
    • Status Bar
    • Layout Managers
    • Text
    • Dialogs
    • Services Tool Window
  • Common Patterns
    • PSI Facade
    • Smart Mode and Dumb Mode
    • Lazy
  • Official Documentation
    • IntelliJ Platform SDK DevGuide
    • IntelliJ SDK Platform Documentation (GitHub)
    • Design Guidelines
    • Getting Started With IntelliJ
    • Creating IntelliJ Platform SDK Code Samples
  • Example Code
    • IntelliJ IDEA - Community Edition (Upsource)
    • IntelliJ IDEA Community Edition (GitHub)
    • Code Samples from SDK Docs
    • Open Source IntelliJ Plugins
    • Gradle Plugin Examples
    • IntelliJ Platform Plugin Template
  • Community
    • JetBrains Platform Slack
    • JetBrains Community Discord
    • Open API Community Support
  • Misc Links
    • FileDocumentManager Examples
  • Tools
    • Indices viewer
    • PsiViewer
    • UI Inspector
    • IntelliJ Platform Explorer
    • Plugin Validator
  • Tutorials
    • Publishing and Distributing a development build
    • Add New Module Type to the New Project Wizard
    • Auto-detect Executable File
  • Questions
    • Where are persistant state and settings files
Powered by GitBook
On this page
  • Introduction
  • Prerequisites
  • Local Executable
  • Create a service
  • Create a Type for your Executable
  • Searching the PATH
  • HomeBrew
  • Manually Entered Files
  • TODO

Was this helpful?

  1. Tutorials

Auto-detect Executable File

Learn to automatically detect installed commands and lay the groundwork for filling in configuration dialogs and building custom drop downs.

PreviousAdd New Module Type to the New Project WizardNextWhere are persistant state and settings files

Last updated 1 year ago

Was this helpful?

Introduction

Many plugins, like the Node.js or PHP plugins, allow you to select from a list of installed interpreters. Other plugins will automatically fill out the path of an executable that is needed in their settings dialogs. In this tutorial you will learn how to auto detect the path of an executable file that can be later used for selecting an active interpreter or deciding if your plugin can run.

Prerequisites

This tutorial assumes that you already know the basics of how to create a new IntelliJ Plugin and already have a plugin project set up to modify.

Local Executable

The most common use case that you will probably come across is looking for a file that is installed on your system. This can be something in your PATH, project directory, or in a custom set of locations that is specific to the program you are trying to find.

The most common places to check are

  • PATH

  • HomeBrew

  • app specific version manager paths

  • environment variables (ex. NODIST_PREFIX)

  • snap packages

  • WSL (windows subsystem for linux)

  • previously saved paths (persistent state component configs)

Create a service

The first step is to create a new service manager interface that will be used to find the executable.

<!-- META-INF/plugin.xml -->
<applicationService serviceImplementation="com.example.autodetect_execuatable.manager.ExampleExecutableManager"/>
package com.example.autodetect_execuatable.manager;

public class ExampleExecutableManager {
    // Optional make a persistant state component
    static final String EXAMPLE_EXECUTABLE_BASE_NAME = SystemInfo.isWindows ? "example.exe" : "example";

    
    @NotNull
    public static ExampleExecutableManager getInstance() {
        return ServiceManager.getService(ExampleExecutableManager.class);
    }
    
    @NotNull
    public List getExecutables() {
    }

}

Create a Type for your Executable

You will want a class to wrap your executable and store metadata about it.

package com.example.autodetect_execuatable.executable;

public class ExampleExecutable {
    private final File myExecutable;
    private final String myExecutableSystemIndependentPath;
    private final String myExecutableSystemDependentPath;
    
    @NotNull
    public String getExecutableSystemDependentPath() {
        return this.myExecutableSystemDependentPath;
    }

    @NotNull
    public String getExecutableSystemIndependentPath() {
        return this.myExecutableSystemIndependentPath;
    }
    
}

Searching the PATH

The PATH environment variable provides a list of directories that are automatically searched to find commands. At a minimum your plugin should support checking this variable to find your command. IntelliJ provides some useful functions to ease working with a system's PATH.

package com.example.autodetect_execuatable.manager;

public class ExampleExecutableManager {
    // Optional make a persistant state component
    static final String EXAMPLE_EXECUTABLE_BASE_NAME = SystemInfo.isWindows ? "example.exe" : "example";
    private static final FileFilter EXAMPLE_FILTER = (File pathname) -> {
        return true; // don't filter out any found files
    };
    
    private volatile List<LocalExecutable> myExecutables;

    
    @NotNull
    public static ExampleExecutableManager getInstance() {
        return ServiceManager.getService(ExampleExecutableManager.class);
    }
    
    @NotNull
    public List getExecutables() {
    }
    
    @NotNull
    public static List<File> detectAllLocalExecutables() {
        List<File> fromPath = PathEnvironmentVariableUtil.findAllExeFilesInPath(EXAMPLE_EXECUTABLE_BASE_NAME, EXAMPLE_FILTER);
        List<File> executables = new ArrayList<>(fromPath);

        return executables;
    }

}

HomeBrew

Manually Entered Files

TODO

PathEnvironmentVariableUtil.java (upsource)