Title: Using listings package with beamer/block environments for displaying source code
Tags: beamer;listings;sourcecode
Question: I use beamer with ```semiverbatim``` environment to show source code. 

```\documentclass{beamer}
\setbeamercolor{background canvas}{bg=yellow}
\useinnertheme[shadow]{rounded}
\setbeamercolor{block title example}{fg=white,bg=red!75!black}%
\setbeamercolor{block body example}{fg=black,bg=white!70!red}%

\begin{document}

\begin{frame}[fragile]{Code}
\begin{example}[Code]
\begin{semiverbatim}
class Hello {
...
}
\end{semiverbatim}
\end{example}
    \end{frame}
\end{document}
```



However, I want listings package which provides features such as highlighting and source code numbering. So, I tried this.

```\documentclass{beamer}
\setbeamercolor{background canvas}{bg=yellow}
\useinnertheme[shadow]{rounded}

\usepackage{listings}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1,firstnumber=1,
    numbersep=5pt,language=Java,
    stringstyle=\ttfamily,
    basicstyle=\footnotesize, 
    showstringspaces=false
}

\begin{document}

\begin{frame}[fragile]{Code}

\begin{block}
\begin{lstlisting}[firstnumber=1, caption=Getting labels, label=glabels] 

class Hello {
...
}

\end{lstlisting}
\end{block}

\end{frame}
\end{document}
```

However, I can't compile it to get an output. 

My questions are:


How can I use listings package with beamer/blocks?
Or how can I show source code with line number listing and keyword highlighting with beamer/blocks?



Here is the accepted answer: Beamer blocks need a title (```\begin{block}{_title_}```), even if it is empty (```\begin{block}{}```).

```\documentclass{beamer}
\setbeamercolor{background canvas}{bg=yellow}
\useinnertheme[shadow]{rounded}

\usepackage{listings}

\lstset{numbers=left, numberstyle=\tiny, stepnumber=1,firstnumber=1,
    numbersep=5pt,language=Java,
    stringstyle=\ttfamily,
    basicstyle=\footnotesize, 
    showstringspaces=false
}

\begin{document}

\begin{frame}[fragile]{Code}

\begin{block}{Getting labels}
\begin{lstlisting}[firstnumber=1, label=glabels, xleftmargin=10pt] 
class Hello {
...
}
\end{lstlisting}
\end{block}

\end{frame}
\end{document}
```





{'source': 'StackExchangeDataset', 'subset': 'tex.stackexchange.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 3.0', 'CreationDate': '2018:00:01/20/18', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<beamer><listings><sourcecode>', 'Title': 'Using listings package with beamer/block environments for displaying source code', 'ViewCount': 3116, 'id': 411313, 'user_AboutMe': '<p>A software engineer/programmer/researcher/professor who loves everything about software building.</p>\n<ul>\n<li>Programming Language: C/C++, D, Java/Groovy/Scala, C#, Objective-C, Python, Ruby, Lisp, Prolog, SQL, Smalltalk, Haskell, F#, OCaml, Erlang/Elixir, Forth, Rebol/Red</li>\n<li>Programming Tools and environments: Emacs, Eclipse, TextMate, JVM, .NET</li>\n<li>Programming Methodology: Refactoring, Design Patterns, Agile, eXtreme</li>\n<li>Computer Science: Algorithm, Compiler, Artificial Intelligence</li>\n</ul>\n', 'user_AccountId': 95531.0, 'user_DisplayName': 'prosseek', 'user_Reputation': 5681.0, 'user_Views': 143.0, 'user_id': 379.0}}