After scanning with custom rules in version 9.9, java.lang.NoClassDefFoundError: com/sun/source/tree

What are you trying to accomplish?
Scan for normal execution

  • Support a new language?
    No
  • Extend an existing one? Which one?
    java
  • Add language-agnostic features?
    No
  • Something else?
    Add formatting code checking

What’s your specific coding challenge in developing your plugin?
I use googlejavaformat to format the code. When I deploy the plugin, I run a scan and an error occurs, prompting java.lang.NoClassDefFoundError: com/sun/source/tree/Tree
I build the jar package in jdk17.
My sonarqube is deployed using docker.

And, if relevant, please share the code that’s giving you problems:

@Rule(key = JavaCodeFormatRule.RULE_KEY, name = "code format check", description = "XXXXXXX")
public class JavaCodeFormatRule implements IRule {

    private static final Logger LOGGER = Loggers.get(JavaCodeFormatRule.class);

    public static final String RULE_KEY = "JavaCodeFormatRule";

    @Override
    public void execute(SensorContext sensorContext, InputFile file, RuleKey ruleKey) {
        try {
            String contents = file.contents();
            Formatter formatter = new Formatter(JavaFormatterOptions.builder()
                    .style(JavaFormatterOptions.Style.AOSP)
                    .formatJavadoc(true)
                    .reorderModifiers(false)
                    .build());
            String formattedCode = formatter.formatSource(contents);

            if (!Objects.equals(contents, formattedCode)) {
                LOGGER.error("代码未格式化!");
                NewIssue newIssue = sensorContext.newIssue();
                newIssue.forRule(ruleKey)
                        .at(newIssue.newLocation().on(file).at(file.selectLine(1)))
                        .save();
            }
        } catch (IOException | FormatterException e) {
            throw new RuntimeException(e);
        }
    }
}

The specific error is

10:31:06.548 INFO  Execute PMD 6.15.0 (done) | time=108493ms
10:31:06.652 INFO  Sensor PmdSensor [pmd] (done) | time=108598ms
10:31:06.652 INFO  Sensor JaCoCo XML Report Importer [jacoco]
10:31:06.654 INFO  'sonar.coverage.jacoco.xmlReportPaths' is not defined. Using default locations: target/site/jacoco/jacoco.xml,target/site/jacoco-it/jacoco.xml,build/reports/jacoco/test/jacocoTestReport.xml
10:31:06.655 INFO  No report imported, no coverage information will be imported by JaCoCo XML Report Importer
10:31:06.655 INFO  Sensor JaCoCo XML Report Importer [jacoco] (done) | time=3ms
10:31:06.655 INFO  Sensor IaC CloudFormation Sensor [iac]
10:31:06.678 INFO  0 source files to be analyzed
10:31:06.690 INFO  0/0 source files have been analyzed
10:31:06.690 INFO  Sensor IaC CloudFormation Sensor [iac] (done) | time=35ms
10:31:06.690 INFO  Sensor IaC Kubernetes Sensor [iac]
10:31:06.703 INFO  0 source files to be analyzed
10:31:06.704 INFO  0/0 source files have been analyzed
10:31:06.705 INFO  Sensor IaC Kubernetes Sensor [iac] (done) | time=15ms
10:31:06.705 INFO  Sensor JavaScript inside YAML analysis [javascript]
10:31:06.710 INFO  No input files found for analysis
10:31:06.711 INFO  Hit the cache for 0 out of 0
10:31:06.713 INFO  Miss the cache for 0 out of 0
10:31:06.713 INFO  Sensor JavaScript inside YAML analysis [javascript] (done) | time=8ms
10:31:06.713 INFO  Sensor CSS Rules [javascript]
10:31:06.719 INFO  No CSS, PHP, HTML or VueJS files are found in the project. CSS analysis is skipped.
10:31:06.719 INFO  Sensor CSS Rules [javascript] (done) | time=6ms
10:31:06.719 INFO  Sensor Analyzer all Java files [example]
10:31:07.029 INFO  EXECUTION FAILURE
10:31:07.033 INFO  Total time: 2:42.421s
10:31:07.034 ERROR Error during SonarScanner CLI execution
java.lang.NoClassDefFoundError: com/sun/source/tree/Tree
	at org.sonarsource.plugins.custom.rules.JavaCodeFormatRule.execute(JavaCodeFormatRule.java:32)
	at org.sonarsource.plugins.custom.rules.JavaFilesSensor.lambda$execute$0(JavaFilesSensor.java:58)
	at java.base/java.util.HashMap$Values.forEach(Unknown Source)
	at org.sonarsource.plugins.custom.rules.JavaFilesSensor.execute(JavaFilesSensor.java:58)
	at org.sonar.scanner.sensor.AbstractSensorWrapper.analyse(AbstractSensorWrapper.java:64)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:88)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.lambda$execute$1(ModuleSensorsExecutor.java:61)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.withModuleStrategy(ModuleSensorsExecutor.java:79)
	at org.sonar.scanner.sensor.ModuleSensorsExecutor.execute(ModuleSensorsExecutor.java:61)
	at org.sonar.scanner.scan.SpringModuleScanContainer.doAfterStart(SpringModuleScanContainer.java:82)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scan(SpringProjectScanContainer.java:403)
	at org.sonar.scanner.scan.SpringProjectScanContainer.scanRecursively(SpringProjectScanContainer.java:399)
	at org.sonar.scanner.scan.SpringProjectScanContainer.doAfterStart(SpringProjectScanContainer.java:368)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.scanner.bootstrap.SpringGlobalContainer.doAfterStart(SpringGlobalContainer.java:137)
	at org.sonar.core.platform.SpringComponentContainer.startComponents(SpringComponentContainer.java:188)
	at org.sonar.core.platform.SpringComponentContainer.execute(SpringComponentContainer.java:167)
	at org.sonar.batch.bootstrapper.Batch.doExecute(Batch.java:72)
	at org.sonar.batch.bootstrapper.Batch.execute(Batch.java:66)
	at org.sonarsource.scanner.lib.internal.batch.BatchIsolatedLauncher.execute(BatchIsolatedLauncher.java:41)
	at java.base/jdk.internal.reflect.NativeMethod

And I also tried to add --add-opens=jdk.compiler/com.sun.source=ALL-UNNAMED to the environment variables SONAR_WEB_JAVAADDITIONALOPTS and SONAR_CE_JAVAADDITIONALOPTS of sonarqube’s docker deployment, but it still doesn’t work

Hey @cglcl,

Welcome to the community! From the snippet you shared, it is hard to tell what may be the source of the problem.

Did you start your custom rule plugin from a specific template? Is your plugin based on the java-custom-rules template?

Cheers,

Dorian