2

I am using Maven and Java Spark to build a simple webservice.

I am running Java 8.

My (very simple) service:

package com.mycompany.app;

import static spark.Spark.*;

    public class HelloWorld {
        public static void main(String[] args) {
            get("/hello", (req, res) -> "Hello World");
        }
    }

The pom.xml file:

    <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/maven-v4_0_0.xsd">
      <modelVersion>4.0.0</modelVersion>
      <groupId>com.simplerelevance.app</groupId>
      <artifactId>art2</artifactId>
      <packaging>jar</packaging>
      <version>1.0-SNAPSHOT</version>
      <name>art2</name>
      <url>http://maven.apache.org</url>
      <dependencies>
        <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>3.8.1</version>
          <scope>test</scope>
        </dependency>
        <dependency>
          <groupId>com.sparkjava</groupId>
          <artifactId>spark-core</artifactId>
          <version>2.2</version>
        </dependency>
      </dependencies>
    </project>

When I try to run mvn compile, my error is:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.0.2:compile (default-compile) on project art2: Compilation failure: Compilation failure:
[ERROR] /home/pierceg/SR_Repos/ART2/art2_java/art2/src/main/java/com/simplerelevance/app/HelloWorld.java:[3,7] error: static import declarations are not supported in -source 1.3
[ERROR] 
[ERROR] (use -source 5 or higher to enable static import declarations)
[ERROR] /home/pierceg/SR_Repos/ART2/art2_java/art2/src/main/java/com/simplerelevance/app/HelloWorld.java:[7,37] error: lambda expressions are not supported in -source 1.3
[ERROR] -> [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/MojoFailureException

The not supported in -source 1.3 phrasing makes me think that there is something amiss with what java version I'm using. Previously, I was getting the same error but with -source 1.5, so I changed $JAVA_HOME to /usr/lib/jvm/java-8-oracle/jre/. Is this what it should be? One of my coworkers agrees with me that the -source should be 1.8.

Edit: mvn -v returns:

Apache Maven 3.0.5
Maven home: /usr/share/maven
Java version: 1.8.0_51, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.16.0-30-generic", arch: "amd64", family: "unix"
pyg
  • 377
  • 1
  • 5
  • 17
  • 1
    I thought it defaulted to 1.5, but in any case, you're not setting it--try setting it, e.g., http://stackoverflow.com/q/10586384/438992 – Dave Newton Aug 05 '15 at 18:40
  • What version of Maven are you running? Recent versions come with a more recent version of the compiler plugin, which defaults source compatibility to 1.5 – Tome Aug 05 '15 at 18:41
  • Best practice is always to define all plugins you are using with their appropriate versions. – khmarbaise Aug 05 '15 at 18:47
  • @RoddyoftheFrozenPeas that's what I'm missing, thanks a ton – pyg Aug 05 '15 at 19:11

1 Answers1

2

Based off the comments, I updated my pom.xml to read:

        <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/maven-v4_0_0.xsd">
          <modelVersion>4.0.0</modelVersion>
          <groupId>com.simplerelevance.app</groupId>
          <artifactId>art2</artifactId>
          <packaging>jar</packaging>
          <version>1.0-SNAPSHOT</version>
          <name>art2</name>
          <url>http://maven.apache.org</url>
          <dependencies>
            <dependency>
              <groupId>junit</groupId>
              <artifactId>junit</artifactId>
              <version>3.8.1</version>
              <scope>test</scope>
            </dependency>
            <dependency>
              <groupId>com.sparkjava</groupId>
              <artifactId>spark-core</artifactId>
              <version>2.2</version>
            </dependency>
          </dependencies>
          <build>
            <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <version>3.3</version>
              <configuration>
                <source>1.8</source>
                <target>1.8</target>
              </configuration>
            </plugin>
          </build>
        </project>

The difference being I added everything between the <build> tags at the end of the document.

pyg
  • 377
  • 1
  • 5
  • 17