Reputation: 2232
I'm trying to run a single page web application written in angularjs with spark java on a jetty, the project is written in eclipse kepler as a Maven project. the error I'm receiving when I go into the url http://localhost:8085/ is as follows, and it doesnt matter if I try to access any other url's or point directly to the index.html/jsp still gets the same error.
HTTP ERROR 404 Problem accessing /. Reason: Not Found Powered by Jetty://
Eclipse Console is showing that the server is up and running
== Spark has ignited ... Listening on localhost:8085 [Thread-2] INFO org.eclipse.jetty.server.Server - jetty-9.0.2.v20130417 [Thread-2] INFO org.eclipse.jetty.server.ServerConnector - Started ServerConnector@12ee37d9{HTTP/1.1}{localhost:8085}
Added the Package Explorer in case that might help.
I'll add some of the files code and if move is needed just let me know and ill edit to add them.
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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>todoapp</groupId>
<artifactId>todoapp1</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.0.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.5</version>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>2.11.3</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.0.2.v20130417</version>
<configuration>
<webApp>
<contextPath>/public</contextPath>
</webApp>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>true</createDependencyReducedPom>
<filters>
<filter>
<artifact>*:*</artifact>
<excludes>
<exclude>META-INF/*.SF</exclude>
<exclude>META-INF/*.DSA</exclude>
<exclude>META-INF/*.RSA</exclude>
</excludes>
</filter>
</filters>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer
implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>com.todoapp.Bootstrap</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Bootstrap.java
package com.todoapp;
import com.mongodb.*;
import static spark.Spark.setIpAddress;
import static spark.Spark.setPort;
import static spark.SparkBase.staticFileLocation;
public class Bootstrap {
private static final String IP_ADDRESS = System.getenv("OPENSHIFT_DIY_IP") != null ? System.getenv("OPENSHIFT_DIY_IP") : "localhost";
private static final int PORT = System.getenv("OPENSHIFT_DIY_IP") != null ? Integer.parseInt(System.getenv("OPENSHIFT_DIY_IP")) : 8085;
public static void main(String[] args) throws Exception {
setIpAddress(IP_ADDRESS);
setPort(PORT);
staticFileLocation("/public");
new TodoResource(new TodoService(mongo()));
}
private static DB mongo() throws Exception {
String host = System.getenv("OPENSHIFT_MONGODB_DB_HOST");
if (host == null) {
MongoClient mongoClient = new MongoClient("localhost");
return mongoClient.getDB("todoapp");
}
int port = Integer.parseInt(System.getenv("OPENSHIFT_MONGODB_DB_PORT"));
String dbname = System.getenv("OPENSHIFT_APP_NAME");
String username = System.getenv("OPENSHIFT_MONGODB_DB_USERNAME");
String password = System.getenv("OPENSHIFT_MONGODB_DB_PASSWORD");
MongoClientOptions mongoClientOptions = MongoClientOptions.builder().connectionsPerHost(20).build();
MongoClient mongoClient = new MongoClient(new ServerAddress(host, port), mongoClientOptions);
mongoClient.setWriteConcern(WriteConcern.SAFE);
DB db = mongoClient.getDB(dbname);
if (db.authenticate(username, password.toCharArray())) {
return db;
} else {
throw new RuntimeException("Not able to authenticate with MongoDB");
}
}
}
this 2 files are the ones responsible for the jetty server, now I have been researching the internet for over a day now trying to find solution and reasons for this error, but nothing helped me.
Upvotes: 0
Views: 934
Reputation: 1359
Make sure your /public
directory, where your html files land, is accessible through your project's classpath. In order to test this, try using staticFiles.externalLocation (this for version 2.5, externalStaticFileLocation
in your case) with the full path to /public
as the param. If it works, you'd probably move your /public
directory to the same location as your java packages and it will work with staticFileLocation("/public")
as you expect.
Upvotes: 1