# Java Code in a paper

I am writing a paper and would like to include some of my "real-world" Java code. What is the best way to accomplish this? I would like to see two templates:

1. My explanation to the right of my Java Code in a column fashion
2. All of my Java code pasted in one block and then a description afterwards

I think 2 is probably the best way to go for simplicity.

-
Have a look at the listings package. Also useful to search here for [listings] (could possibly include the word java in your query, but answers for other languages will probably also be useful). –  Vivi Dec 14 '12 at 5:26
another alternative might be minted,you can search here by the tags listings and minted to narrow down search results –  texenthusiast Dec 14 '12 at 5:51

You can use the listings package like this sample:

\documentclass{article}

\usepackage{filecontents}
\begin{filecontents*}{_myperl.pl}
#!/usr/bin/perl
while (<@ARGV>)
{ $sum += 1/($_*$_); } print sqrt(1/$sum), "\n";
\end{filecontents*}

\usepackage{listings}
\begin{document}
\section{listings}
\lstset{language=Perl}
\lstinputlisting{_myperl.pl}
\end{document}


Here I have included the sample java code in the document, but you will probably want to keep it in a separate file.

-
Um, that's Perl :-) (Same idea applies of course, but might slightly confuse people.) –  Joseph Wright Dec 14 '12 at 7:03
I'm trying this and my code is spilling over the page? –  Magpie Dec 19 '12 at 8:07