Embed Jetty WebServer Into Bukkit Plugin.

Discussion in 'Plugin Development' started by MCMatters, Aug 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline

    MCMatters

    Im trying to use Jetty Webserver with Maven but i get a class not found error.
    Code:
    Code:java
    1.  
    2. import java.io.File;
    3. import java.net.URL;
    4. import java.net.URLClassLoader;
    5. import java.util.ArrayList;
    6. import java.util.List;
    7.  
    8. import org.apache.jasper.servlet.JspServlet;
    9. import org.apache.tomcat.InstanceManager;
    10. import org.apache.tomcat.SimpleInstanceManager;
    11. import org.bukkit.plugin.java.JavaPlugin;
    12. import org.eclipse.jetty.annotations.ServletContainerInitializersStarter;
    13. import org.eclipse.jetty.apache.jsp.JettyJasperInitializer;
    14. import org.eclipse.jetty.plus.annotation.ContainerInitializer;
    15. import org.eclipse.jetty.server.Server;
    16. import org.eclipse.jetty.servlet.ServletHolder;
    17. import org.eclipse.jetty.webapp.WebAppContext;
    18.  
    19. public class Main extends JavaPlugin{
    20. public Server server = null;
    21. public void onEnable() {
    22. server = new Server(80);
    23.  
    24. System.setProperty("org.apache.jasper.compiler.disablejsr199", "false");
    25.  
    26. WebAppContext context = new WebAppContext();
    27.  
    28. context.setContextPath("/");
    29. context.setResourceBase(new File(getDataFolder(), "WEBROOT").toURI().toASCIIString());
    30. context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
    31.  
    32. server.setHandler(context);
    33.  
    34. JettyJasperInitializer sci = new JettyJasperInitializer();
    35. ServletContainerInitializersStarter sciStarter = new ServletContainerInitializersStarter(context);
    36.  
    37. ContainerInitializer initializer = new ContainerInitializer(sci, null);
    38. List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
    39.  
    40. initializers.add(initializer);
    41.  
    42. context.setAttribute("org.eclipse.jetty.containerInitializers", initializers);
    43. context.addBean(sciStarter, true);
    44.  
    45. ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
    46. context.setClassLoader(jspClassLoader);
    47.  
    48. ServletHolder holderJsp = new ServletHolder("jsp", JspServlet.class);
    49.  
    50. holderJsp.setInitOrder(0);
    51. holderJsp.setInitParameter("fork", "false");
    52. holderJsp.setInitParameter("xpoweredBy", "false");
    53. holderJsp.setInitParameter("keepgenerated", "false");
    54. holderJsp.setInitParameter("compilerTargetVM", "1.7");
    55. holderJsp.setInitParameter("compilerSourceVM", "1.7");
    56. holderJsp.setInitParameter("logVerbosityLevel", "DEBUG");
    57.  
    58. context.addServlet(holderJsp, "/index.jsp");
    59.  
    60. try {
    61. server.start();
    62. } catch (Exception e) {
    63. getLogger().info("Cannnot bind to port 80!");
    64. e.printStackTrace();
    65. }
    66. }
    67. public void onDisable() {
    68. try {
    69. server.stop();
    70. } catch (Exception e) {
    71. // TODO Auto-generated catch block
    72. e.printStackTrace();
    73. }
    74. }
    75. }
    76.  

    Pom.xml:
    Code:
    <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>me.dillan</groupId>
      <artifactId>adminpanel</artifactId>
      <version>0.0.1-SNAPSHOT</version>
      <dependencies>
      <dependency>
                <groupId>javax.servlet</groupId>
                <artifactId>javax.servlet-api</artifactId>
                <version>3.1.0</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-annotations</artifactId>
                <version>9.2.1.v20140609</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>jetty-webapp</artifactId>
                <version>9.2.1.v20140609</version>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>apache-jsp</artifactId>
                <version>9.2.1.v20140609</version>
                <type>jar</type>
            </dependency>
            <dependency>
                <groupId>org.eclipse.jetty</groupId>
                <artifactId>apache-jstl</artifactId>
                <version>9.2.1.v20140609</version>
                <type>pom</type>
            </dependency>
            <dependency>
              <groupId>org.bukkit</groupId>
              <artifactId>bukkit</artifactId>
              <version>1.7.9-R0.2</version>
              <type>jar</type>
              <scope>provided</scope>
          </dependency>
            </dependencies>
                      <build>
          <plugins>
              <plugin>
                  <groupId>org.apache.maven.plugins</groupId>
                  <artifactId>maven-compiler-plugin</artifactId>
                  <configuration>
                      <source>1.7</source>
                      <target>1.7</target>
                  </configuration>
              </plugin>
        <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.7.1</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
                <configuration>
                    <artifactSet>
                        <includes>
                            <include>org.eclipse.jetty:jetty-webapp</include>
                            <include>org.eclipse.jetty:jetty-annotations</include>
                            <include>org.eclipse.jetty:apache-jsp</include>
                            <include>org.eclipse.jetty:apache-jstl</include>
                            <include>javax.servlet:javax.servlet.api</include>
                        </includes>
                    </artifactSet>
                </configuration>
            </execution>
        </executions>
    </plugin>
          </plugins>
      </build>
      <repositories>
          <repository>
              <id>bukkit-repo</id>
              <url>http://repo.bukkit.org/content/groups/public/</url>
          </repository>
      </repositories>
    </project>
     
  2. Offline

    Goblom

    MCMatters What is the class that is not found?
     
  3. Offline

    MCMatters

    Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler

    Goblom

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  4. Offline

    xize

    MCMatters
    have you read what the maven console said?
    it should say what it included into the the shaded jar file and what is excluded.
    if it says excluded for jetty please recheck that path in your pom till it says its included, maven console gives the correct path.

    I geuss you should put this:

    Code:
    <include>*jetty:*</include>
    <include>javax.servlet:servlet-api:*</include>
    
     
  5. Offline

    MCMatters

    xize How do i pull up the maven console in eclipse
     
  6. Offline

    Chiller

    MCMatters It probably means your not shading jetty into your main jar.

    MCMatters
    Code:
    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-shade-plugin</artifactId>
        <version>1.6</version>
        <executions>
            <execution>
                <phase>package</phase>
                <goals>
                    <goal>shade</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <transformers>
                <transformer
                    implementation="org.apache.maven.plugins.shade.resource.ServicesResourceTransformer" />
            </transformers>
     
            <filters>
                <filter>
                    <artifact>*:*</artifact>
                    <excludes>
                        <exclude>META-INF/*.SF</exclude>
                        <exclude>META-INF/*.DSA</exclude>
                    </excludes>
                </filter>
            </filters>
        </configuration>
    </plugin>
    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  7. Offline

    MCMatters

    Error:
    Code:
    Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
     
  8. Offline

    Chiller

    MCMatters To compile your plugin your using "maven package" right? Not just exporting the jar.
     
  9. Offline

    MCMatters

    how do i do that with eclipse?

    i just export the jar currently

    Chiller

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 9, 2016
  10. Offline

    Chiller

    MCMatters Select run as > Maven build..., set the goal to package and apply and run!
     
  11. Offline

    MCMatters

    Chiller and then compile it? Because i exported the jar after that and i got
    Code:
    Caused by: java.lang.ClassNotFoundException: org.eclipse.jetty.server.Handler
     
  12. Offline

    Chiller

    MCMatters Maven package exports the jar, but the jar is in the project folder/target.
     
  13. Offline

    MCMatters

Thread Status:
Not open for further replies.

Share This Page