\documentclass[12pt]{article}
\usepackage{fixltx2e}%% the official fixes for LaTeX2e
\usepackage[T1]{fontenc}
\usepackage[latin1]{inputenc}
\usepackage[scaled=0.92]{helvet}
\usepackage{mathptmx}
%\usepackage{courier}
\usepackage{color}
\usepackage{listings}
\newcommand{\lst}[2]{%
    \noindent\rule[-1ex]{\textwidth}{0.3mm}\vspace{-1ex}
    \lstinputlisting[caption={#2},%
    label={#1},%
    showstringspaces=false,%
    frame=tb,%
    extendedchars=true,%
    basicstyle=\footnotesize\tt,%
    numbers=left,%
    stepnumber=1,%
    numberstyle=\tiny,%
    keywordstyle=\color{red},%
    language=Java,%
    breaklines=true]{#1}%
    \vspace{1ex}%
}
\lstdefinelanguage{bash}{% 
     morekeywords={% 
                for, if, else, then, fi, echo, case, in,% 
              elif, esac, for, do, done, tar, while, until, shift,% 
          mv, mkdir ;;, \$\#, \$1, \$2},% 
        sensitive=t, % 
  morecomment=[l]{\#} ,% 
  morestring=[d]{"}% 
}   
\usepackage{ifpdf}
\ifpdf
    \usepackage[colorlinks,linktocpage]{hyperref}
\fi
\begin{document}
\title{Demo for Package \texttt{listings}}
\author{Herbert Voß}
\maketitle
At first we want to see the first Java code for a little program,
called Welcome (see Listing \ref{prog1}).

\lstinputlisting[caption={My first Java Program},%
    label={prog1},%
    showstringspaces=false,%
    frame=tb,%
    extendedchars=true,%
    basicstyle=\normalfont\footnotesize\tt,%
    numbers=left,%
    stepnumber=1,%
    numberstyle=\tiny,%
    linewidth=0.9\linewidth,%
    language=Java,%
    keywordstyle=\color{red},%
    breaklines=true]{Welcome1.java}

Another way is to write an own command for the inputlisting, which
has a better layout. This one is called \texttt{lst} with 2 parameters:

\begin{enumerate}
\item The filename of the Sourcefile
\item The caption
\end{enumerate}
\begin{lstlisting}[language={[LaTeX]TeX}]
\newcommand{\lst}[2]{%
    \noindent\rule[-1ex]{\textwidth}{0.3mm}\vspace{-1ex}
    \lstinputlisting[caption={#2},%
    label={#1},%
    showstringspaces=false,%
    frame=tb,%
    extendedchars=true,%
    basicstyle=\footnotesize\tt,%
    numbers=left,%
    stepnumber=1,%
    numberstyle=\tiny,%
    keywordstyle=\color{red},%
    language=Java,
    breaklines=true]{#1}%
    \vspace{1ex}%
}
\end{lstlisting}

\medskip{}
Now the listing is called by 

\verb|\lst{Welcome1.java}{My first Java Program}|

The lst-command creates by default a label which is the filename,
here \verb|{Welcome1.java}|. 

\lst{Welcome1.java}{My first Java Program}

A reference to this listings is therefore no problem, look at listing
\ref{Welcome1.java}. This reference is set by \verb|\ref{Welcome1.java}|. 

Changing the fontstyle to courier is possible with:

\verb|basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize|

or with loading the package \verb|courier.sty|.

\lstset{basicstyle=\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize,%
keywordstyle=\bfseries, language=Java, breaklines}

\lstinputlisting[caption={My first Java Program}, label={prog2}]{Welcome1.java}

The baselineskip (vertical space) can also be changed. As an additional
command to the options of \verb|basicstyle| write:

\verb|\renewcommand\baselinestretch{0.8}|

and the listings becomes:

\lstinputlisting[caption={My first Java Program},%
    label={prog3},%
    basicstyle=\renewcommand\baselinestretch{0.8}\fontfamily{pcr}\fontseries{m}\selectfont\footnotesize,%
    keywordstyle=\bfseries, language=Java,%
    breaklines=true]{Welcome1.java}

A new language which is not part of the current list, can easily be
defined with the \verb|\lstdefinelanguage|:

\begin{lstlisting}[language=bash]
\lstdefinelanguage{bash}{% 
     morekeywords={% 
                for, if, else, then, fi, echo, case, in,% 
              elif, esac, for, do, done, tar, while, until, shift,% 
          mv, mkdir ;;, \$\#, \$1, \$2},% 
        sensitive=t, % 
  morecomment=[l]{\#} ,% 
  morestring=[d]{"}% 
}   
\end{lstlisting}

With this definition an output of a bashfile is shown with listings
\ref{bash}.

\lstset{language=bash,%
     basicstyle=\footnotesize,% 
     keywordstyle=\bfseries, frame=tb} 
\begin{lstlisting}[label=bash, 
    caption={A listing for a new defined Language},%
    xleftmargin=-0.25\marginparwidth,%
    breaklines=true] 
#!/bin/bash 
# voss/010622 
# 
NOMENCL_STYLE=~voss/styles/nomencl.ist # or path to nomencl-package 
INDEX_STYLE=~voss/styles/Letter.ist        # my default 
#TEXPATH=/usr/share/texmf/teTeX/bin/i386-linux-glibc 
TEXPATH=/usr/share/texmf/teTeX/bin/i386-linux-libc6 
FILENAME=$3              # lyx calls it with makeindex -p -s filename 
NAME=${FILENAME%%.*}      # without suffix 
echo "This is myMakeIndex ..." 
echo "File: $NAME" 
# 
$TEXPATH/makeindex -s $INDEX_STYLE -c -q $3 
# if there is no glossary it doesn't matter 
if [ -f $name.gls ] ; then 
  $TEXPATH/makeindex $NAME.glo -s $NOMENCL_STYLE -o $NAME.gls 
fi

\end{lstlisting}

A list of listings is possible with \verb|\lstlistoflistings?|:

\lstlistoflistings
\end{document}
