Auto-detect Executable File

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

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

Last updated

Was this helpful?