Java Intro to Java Web Development with Spark Diving into Web Development Sending Responses

I am getting an error when I try to run this code, it says ERROR spark.Spark - ignite failed

When I try to run my code this is what I get this at the end

[Thread-1] ERROR spark.Spark - ignite failed
java.net.BindException: Address already in use: bind
    at sun.nio.ch.Net.bind0(Native Method)
    at sun.nio.ch.Net.bind(Net.java:433)
    at sun.nio.ch.Net.bind(Net.java:425)
    at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223)
    at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
    at org.eclipse.jetty.server.ServerConnector.open(ServerConnector.java:298)
    at org.eclipse.jetty.server.AbstractNetworkConnector.doStart(AbstractNetworkConnector.java:80)
    at org.eclipse.jetty.server.ServerConnector.doStart(ServerConnector.java:236)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at org.eclipse.jetty.server.Server.doStart(Server.java:431)
    at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
    at spark.embeddedserver.jetty.EmbeddedJettyServer.ignite(EmbeddedJettyServer.java:130)
    at spark.Service.lambda$init$2(Service.java:504)
    at java.lang.Thread.run(Thread.java:748)

Process finished with exit code 100

Java Code:

package com.teamtreehouse.courses;

import spark.ModelAndView;
import spark.template.handlebars.HandlebarsTemplateEngine;

import static spark.Spark.get;

public class Main {
    public static void main(String[] args){

        get ("/", (req, res) -> {
              return new ModelAndView(null, "index.hbs");
        }, new HandlebarsTemplateEngine());
    }
}

index.hbs code:

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Hello Students</title>
</head>
<body>
    <h1>Welcome Students!</h1>
</body>
</html>

Fahad Mutair I have updates the question with some code.

2 Answers

Luca Cunico
PRO
Luca Cunico
Pro Student 14,196 Points

Hello, The error you got is because you have another server running. Close all run tasks and try again.

You can see this by reading this line:

java.net.BindException: Address already in use: bind
Naval Kulkarni
Naval Kulkarni
13,782 Points

Follow this stackoverflow thread and your problem will be solved. i was getting the same error. Some java.exe process wass listening on port 4567, once i ended it, the error was gone.

Here is the link:

https://stackoverflow.com/a/12737775