EmbeddedScanner (SonarScaner API) fails second time execute is called

SonarQube version: 9.4.0.54424
Scanner: Sonar Scanner API, version 2.16.2.588

Hello!
I’m using the Sonar Scanner API to build an application that scans single files of code.

I’ve set up EmbeddedScanner like this:

embeddedScanner = EmbeddedScanner.create("example", "1", new StdOutLogOutput())
                .setGlobalProperty("sonar.host.url", "http://localhost:9000")
                .setGlobalProperty("sonar.projectName", "example-project-name")
                .setGlobalProperty("sonar.projectKey", "example-project-key")
                .setGlobalProperty("sonar.sources", ".scanner_code_files")
                .setGlobalProperty("sonar.login", /*some token*/);
        embeddedScanner.start();

…and I’m using this method to execute a scan.

private void execute(String fileName) {
        Map<String, String> localExecutionProperties = new HashMap<>();
        localExecutionProperties.put("sonar.inclusions", "**/*" + fileName);
        this.embeddedScanner.execute(localExecutionProperties);
    }

The first time I call execute it works fine, but the second time I get this stacktrace:

INFO: Default locale: "sv_SE", source code encoding: "UTF-8" (analysis is platform dependent)
DEBUG: Work directory: C:\Users\alexa\IdeaProjects\examensarbete\sonarqubot\.scannerwork
DEBUG: Execution execute
java.lang.IllegalArgumentException: Logback configuration not found in classloader: /org/sonar/batch/bootstrapper/logback.xml
	at org.sonar.core.config.Logback.configure(Logback.java:49)
	at org.sonar.batch.bootstrapper.LoggingConfigurator.apply(LoggingConfigurator.java:50)
	at org.sonar.batch.bootstrapper.LoggingConfigurator.apply(LoggingConfigurator.java:46)
	at org.sonar.batch.bootstrapper.Batch.configureLogging(Batch.java:128)
	at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:70)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
	at org.sonarsource.scanner.api.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:46)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.sonarsource.scanner.api.internal.IsolatedLauncherProxy.invoke(IsolatedLauncherProxy.java:60)
	at jdk.proxy3/jdk.proxy3.$Proxy53.execute(Unknown Source)
	at org.sonarsource.scanner.api.EmbeddedScanner.doExecute(EmbeddedScanner.java:189)
	at org.sonarsource.scanner.api.EmbeddedScanner.execute(EmbeddedScanner.java:138)
	at org.five.sonarqubot.scanner.ScannerServiceImpl.execute(ScannerServiceImpl.java:72)

I tried to look into the source code and this is the code that throws the exception.

public class Logback {
    private Logback() {
    }

    public static void configure(String classloaderPath, Map<String, String> substitutionVariables) {
        InputStream input = Logback.class.getResourceAsStream(classloaderPath);
        if (input == null) {
            throw new IllegalArgumentException("Logback configuration not found in classloader: " + classloaderPath);
        } else {
            configure(input, substitutionVariables);
        }
    }

/*Omitted code*/

    private static void configure(InputStream input, Map<String, String> substitutionVariables) {
        LoggerContext lc = (LoggerContext)LoggerFactory.getILoggerFactory();

        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(configureContext(lc, substitutionVariables));
            configurator.doConfigure(input);
        } catch (JoranException var7) {
        } finally {
            IOUtils.closeQuietly(input);
        }

        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);
    }
/*Omitted code */
}

It seems like InputStream input = Logback.class.getResourceAsStream(classloaderPath); results in a null the second time around.

Any idea on how to solve this?

Best regards,
Alexander

This can be closed.

I looked at how existing scanners used the EmbeddedScanner.
The answer was to create a new EmbeddedScanner for everytime you want to execute.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.