\lstnewenvironment{javacode}[2] { \begin{singlespace} \lstset{language=java, label=#1, caption=#2}} { \end{singlespace} }
But upon compiling, I was getting the following 'error':
Overfull \hbox (6.0pt too wide) in paragraph at lines 6--6 [][][][][][][] [1]) [2] [3]) *
The Fix
Upon some research, I found the following question which provided a solution to my problem.The solution to the singlespace environment problem, as suggested by that answer, is to not use the singlespace environment but instead to use \singlespacing instead.
Therefore, this is my final working code:
\lstnewenvironment{javacode}[2] { \singlespacing \lstset{language=java, label=#1, caption=#2}} {}