Skip to content
Advertisement

mvn compile OK, but mvn compile exec:java -D exec.mainClass=”mygroup.TestExpression” fails

I’ve refered to this post: Resource specification not allowed here for source level below 1.7

I used the solution, but didn’t solve problem.

Well I’m not using eclipse but jdk 1.8 + vscode on win10, using maven to generate a new project:

mvn archetype:generate

add these lines in my pom.xml inside the top level <project> tag:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

Then I have this code:

public static void main(String[] args) {
        try(Scanner sc = new Scanner(System.in)) { // Error!
            String infix;
            while(! (infix = sc.nextLine()).isEmpty()) {
                
            }
        }
    }

Using mvn compile ok, but using mvn compile exec:java -D exec.mainClass="mygroup.TestExpression" will fail:

It reports error:

mvn compile exec:java -D exec.mainClass="mygroup.TestExpression"

[INFO] --- exec-maven-plugin:3.0.0:java (default-cli) @ myalgos ---
[WARNING] 
java.lang.Error: Unresolved compilation problem: 
        Resource specification not allowed here for source level below 1.7

    at mygroup.TestExpression.main (TestExpression.java:8)
    at org.codehaus.mojo.exec.ExecJavaMojo$1.run (ExecJavaMojo.java:254)
    at java.lang.Thread.run (Thread.java:748)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

You could see I’ve specified maven-compiler-plugin version 3.8.0, while command line output showed exec-maven-plugin:3.0.0:.

I’m not sure if this was the reason, what’s the difference between mvn compile and mvn compile exec:java -D exec.mainClass="mygroup.TestExpression"?

How to solve this problem(compile+run in one step)?

Thanks!

Advertisement

Answer

Try specifying java version in properties tag inside pom

<properties>
    <maven.compiler.target>1.8</maven.compiler.target>
    <maven.compiler.source>1.8</maven.compiler.source>
</properties>

Also check out this link, maybe you’ll find something new there

2 People found this is helpful
Advertisement