2

I assume that additional dependencies are needed, but I can not understand exactly what. I appreciate everyone's help.

My pom file is:

<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.spark</groupId>
    <artifactId>spark</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <name>M101J</name>
    <url>http://maven.apache.org</url>

    <dependencies>
        <dependency>
            <groupId>com.sparkjava</groupId>
            <artifactId>spark-core</artifactId>
            <version>1.1.1</version>
        </dependency>

        <dependency>
            <groupId>org.freemarker</groupId>
            <artifactId>freemarker</artifactId>
            <version>2.3.19</version>
        </dependency>

        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-simple</artifactId>
            <version>1.6.4</version>
        </dependency>
    </dependencies>
    <repositories>
        <repository>
            <id>Spark repository</id>
            <url>http://sparkjava.com/nexus/content/repositories/spark/</url>
        </repository>
    </repositories>
    <build>
        <plugins>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>com.spark.SparkHomework</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

When I try to run my project with command:

mvn compile exec:java -Dexec.mainClass=com.spark.SparkHomework

I have the issue like this:

Unknown lifecycle phase ".mainClass=com.spark.SparkHomework". You must specify a valid lifecycle phase or a goal in the format : or :[:]:. Available lifecycle phases are: validate, initialize, generate-sources, process-sources, generate-resources, process-resources, compile, process-classes, generate-test-sources, process-test-sources, generate-test-resources, process-test-resources, test-compile, process-test-classes, test, prepare-package, package, pre-integration-test, integration-test, post-integration-test, verify, install, deploy, pre-clean, clean, post-clean, pre-site, site, post-site, site-deploy.

My class named as SparkHomework (package named as com.spark) is :

package com.spark;

import freemarker.template.Configuration;
import freemarker.template.Template;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import spark.Request;
import spark.Response;
import spark.Route;
import spark.Spark;

import java.io.StringWriter;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class SparkHomework {
    private static final Logger logger = LoggerFactory.getLogger("logger");

    public static void main(String[] args) throws UnknownHostException {
        final Configuration configuration = new Configuration();
        configuration.setClassForTemplateLoading(
                SparkHomework.class, "/");

        Spark.get(new Route("/") {
            @Override
            public Object handle(final Request request,
                                 final Response response) {
                StringWriter writer = new StringWriter();
                try {
                    Template helloTemplate = configuration.getTemplate("answer.ftl");

                    Map<String, String> answerMap = new HashMap<String, String>();
                    answerMap.put("answer", createAnswer());

                    helloTemplate.process(answerMap, writer);
                } catch (Exception e) {
                    logger.error("Failed", e);
                    halt(500);
                }
                return writer;
            }
        });
    }


    private static String createAnswer() {
        int i = 0;
        for (int bit = 0; bit < 16; bit++) {
            i |= bit << bit;
        }
        return Integer.toString(i);
    }
}
10
  • What do you want to do? Run your main class? Commented Jun 4, 2018 at 10:36
  • @TiagoMussi, yes. Commented Jun 4, 2018 at 10:37
  • 1
    I don't think maven is going to help you with that. Maven is basically for build and dependencies. To run your main class, try: stackoverflow.com/questions/1279542/… Commented Jun 4, 2018 at 10:39
  • @TiagoMussi thanks, I will try that. But do you know what happend with my project? Command is correct... Commented Jun 4, 2018 at 10:42
  • 1
    Do you wish to run your main class every time you compile? In that case, you should probably configu the exec plugin in your pom. If you wish to run it only that one time, just run mvn package and then java -cp target/spark-0.0.1-SNAPSHOT.jar com.spark.SparkHomework Commented Jun 4, 2018 at 10:43

1 Answer 1

7

SOLVED! In Powershell need to add quotes like -D"exec.mainClass". In command prompt - all is okay without them. Thanks everyone! command prompt and powershell.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.