home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jetty example source code file (RunEmbedded.java)

This example Jetty source code file (RunEmbedded.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jetty tags/keywords

bufferedreader, class, enter, exception, exception, file, fileserver, hashmap, io, object, object, reflection, runembedded, runembedded, string, string, util

The Jetty RunEmbedded.java source code

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;
import java.lang.reflect.Method;
import java.util.HashMap;

import junit.framework.TestCase;

public class RunEmbedded
    private static HashMap argMap;
    private static String[] stringArrayType = new String[0];
    private BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        argMap = new HashMap();
        argMap.put ("FileServer", new String[]{"port", "resourceBase"});
    private String askWhichOneToRun()
    throws Exception
        System.err.println("Embedded examples to choose from: ");
        File classesDir = new File ("./target/classes/org/mortbay/jetty/example");
        String[] classNames = classesDir.list();
        for (int i=0; i<classNames.length; i++)
            String className = classNames[i].substring(0, classNames[i].length()-6);
            if (className.indexOf('$') < 0)
                if (!className.equals("LikeJettyXml"))
        System.err.println("The example LikeJettyXml can only be run from $(jetty.home), so try it using your IDE instead.\n\n");
        System.err.print("Enter the name (case sensitive) > ");
        return reader.readLine();
    public void testEmbedded ()
    throws Exception
        String whichOneToRun = askWhichOneToRun();
        String className = "org.mortbay.jetty.example."+ whichOneToRun;
        Class clazz = Thread.currentThread().getContextClassLoader().loadClass(className);
        Object o = clazz.newInstance();
        Method main = clazz.getMethod("main", new Class[]{stringArrayType.getClass()});
        String[] prompts = (String[])argMap.get(whichOneToRun);
        String[] args = new String[0];
        if (prompts != null)
            args = new String[prompts.length];
            for (int i=0; i<prompts.length; i++)
                System.err.print("Enter arg "+i+": "+prompts[i]+" > ");
        Object[] methodArgs = new Object[1];
        main.invoke(null, methodArgs);
    public static final void main (String[] args)
        RunEmbedded runner = new RunEmbedded();
        catch (Exception e)

Other Jetty examples (source code examples)

Here is a short list of links related to this Jetty RunEmbedded.java source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.