[Tex/LaTex] How to left align lstlisting code with text

listings

I'm using listings package to include sample code to my document and the frame option to surround the code inside a box. The problem is that I got too much white space between the left rule of the frame and the code (see pic below).

The problem seems not to be the frame itself, but the left alignment of the code. I tried to update the xleftmargin option, but nothing happened. The only way I can decrease the white space is to give a negative length to the left rule of the frame with the framexleftmargin option, but I don't like it since I want the frame width to be of the same width of the text. I just want to decrease the left white space to take advantage of the full line and get less broken lines within the code.

This is my current configuration.

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black}
} 

Please help me to solve this problem. I'm stuck on it for hours.

Thank you.

EDIT: Provided a minimal working example, as required.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black}
}

\begin{document}
    \begin{lstlisting}
        public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
            LongWritable mkey = new LongWritable();
            LongWritable mval = new LongWritable();

            public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String line = value.toString();
                StringTokenizer tokenizer = new StringTokenizer(line);
                long src, dst;

                if (tokenizer.hasMoreTokens()) {
                    src = Long.parseLong(tokenizer.nextToken());
                    if (!tokenizer.hasMoreTokens())
                        throw new RuntimeException("Invalid edge line: " + line);
                    dst = Long.parseLong(tokenizer.nextToken());

                    mkey.set(src);
                    mval.set(dst);
                    context.write(mkey, mval);
                    context.write(mval, mkey);
                }
            }
        }
    \end{lstlisting}
\end{document}

Trouble with listing and frame

Best Answer

Just load the lstautogobble package and insert autogobble=true in your \lstset, so you don't have to bother about code indentation.

MWE

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{listings,lstautogobble}
\usepackage{xcolor}

\definecolor{RoyalBlue}{cmyk}{1, 0.50, 0, 0}

\lstset{language=Java,
    keywordstyle=\color{RoyalBlue},
    basicstyle=\scriptsize\ttfamily,
    commentstyle=\ttfamily\itshape\color{gray},
    stringstyle=\ttfamily,
    showstringspaces=false,
    breaklines=true,
    frameround=ffff,
    frame=single,
    rulecolor=\color{black},
    autogobble=true
}

\begin{document}
    \begin{lstlisting}
        public static class FirstMapper extends Mapper<LongWritable, Text, LongWritable, LongWritable> {
            LongWritable mkey = new LongWritable();
            LongWritable mval = new LongWritable();

            public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
                String line = value.toString();
                StringTokenizer tokenizer = new StringTokenizer(line);
                long src, dst;

                if (tokenizer.hasMoreTokens()) {
                    src = Long.parseLong(tokenizer.nextToken());
                    if (!tokenizer.hasMoreTokens())
                        throw new RuntimeException("Invalid edge line: " + line);
                    dst = Long.parseLong(tokenizer.nextToken());

                    mkey.set(src);
                    mval.set(dst);
                    context.write(mkey, mval);
                    context.write(mval, mkey);
                }
            }
        }
    \end{lstlisting}
\end{document} 

Output:

enter image description here

The autogobble option gobbles the spaces in the whole listing according to the number of spaces in the first line. If such a package has been implemented, this means that your question is not silly at all!