mojohaus / exec-maven-plugin

Exec Maven Plugin
https://www.mojohaus.org/exec-maven-plugin/
Apache License 2.0
163 stars 96 forks source link

An default visibility class, has main method, occurs error on execution #336

Open yukihane opened 1 year ago

yukihane commented 1 year ago

When executing mvn clean compile exec:java, following error occurs:

[INFO] --- exec-maven-plugin:3.1.0:java (default-cli) @ package-visibility ---
[WARNING] 
java.lang.Exception: The specified mainClass doesn't contain a main method with appropriate signature.
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:283)
    at java.lang.Thread.run (Thread.java:833)
Caused by: java.lang.IllegalAccessException: symbolic reference class is not accessible: class Main, from class org.codehaus.mojo.exec.ExecJavaMojo$1 (unnamed module @4425dfee)
    at java.lang.invoke.MemberName.makeAccessException (MemberName.java:955)
    at java.lang.invoke.MethodHandles$Lookup.checkSymbolicClass (MethodHandles.java:3686)
    at java.lang.invoke.MethodHandles$Lookup.resolveOrFail (MethodHandles.java:3646)
    at java.lang.invoke.MethodHandles$Lookup.findStatic (MethodHandles.java:2588)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:276)
    at java.lang.Thread.run (Thread.java:833)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  0.152 s
[INFO] Finished at: 2022-09-01T23:19:46+09:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal org.codehaus.mojo:exec-maven-plugin:3.1.0:java (default-cli) on project package-visibility: An exception occurred while executing the Java class. The specified mainClass doesn't contain a main method with appropriate signature.: symbolic reference class is not accessible: class Main, from class org.codehaus.mojo.exec.ExecJavaMojo$1 (unnamed module @4425dfee) -> [Help 1]
[ERROR] 
[ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
[ERROR] Re-run Maven using the -X switch to enable full debug logging.
[ERROR] 
[ERROR] For more information about the errors and possible solutions, please read the following articles:
[ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException

On the other hand, java src/main/java/Main.java command does well.

I think a main class doesn't need to be public.

RESOURCES

src/main/java/Main.java:

class Main {
    public static void main(final String[] args) {
        System.out.println("Hello, world!");
    }
}

pom.xml:

<project xmlns="http://maven.apache.org/POM/4.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.github.yukihane.examples</groupId>
  <artifactId>package-visibility</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <exec-maven-plugin.version>3.1.0</exec-maven-plugin.version>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>org.codehaus.mojo</groupId>
        <artifactId>exec-maven-plugin</artifactId>
        <version>${exec-maven-plugin.version}</version>
        <executions>
          <execution>
            <goals>
              <goal>java</goal>
            </goals>
          </execution>
        </executions>
        <configuration>
          <mainClass>Main</mainClass>
        </configuration>
      </plugin>
    </plugins>
  </build>

</project>
pzygielo commented 1 year ago

exec:java executes main method of given class in existing JVM. Unless requested class is in org.codehaus.mojo.exec - it has to be public I belive.

Use exec:exec to execute Main.main in external java process.

bmalinowsky commented 1 year ago

This is likely a regression, it works with v1.5.0. Probably due to using MethodHandles.publicLookup() in #119. Having main in a package-private visible class is valid.