I want to add all my Java source code to an appendix. But I have 20 projects. Is there a possibility to just tell LaTeX where the folder with the different projects are, and LaTeX collects all the source code from there? And gives each section and subsection the name of the file?

Wotcha. I've programmed a lot in Java lately and I want to include the different projects into my appendix.

I did write my java projects in Netbeans. The projects are structured as follows:

folders and files

The purple folders (project XYZ) shall be the name of the section. The name of the subsection should be the name of the package (or the file, if there is no subsection) and the subsubsection shall be the name of the file.

I found an example on how to do it for the individual files. But I don't want to add all files individually for each project. I have like 20 projects with many packages and many files in them. It would take years.

So I wanted to ask: is there a way to just give the path to the folder with all projects in or to each individual projects and LaTeX collects all the files from there?

Here the example I talked about:


\foreach \java in  {hello, hello} {
        \caption{Source code for \textsf{\java.java}}

  • You should be able to use UNIX style relative pathnames, such as ../folder/file Commented Jun 20, 2018 at 14:40
  • Or you can write a small Java program to generate the relevant LaTeX code, with all of that Java experience I assume that will be easy for you :)
    – Marijn
    Commented Jun 20, 2018 at 17:28
  • @Marijn ... thx, I did it :-) ... it works ;-) Commented Jun 20, 2018 at 20:08
  • @AurelErnst good job!
    – Marijn
    Commented Jun 20, 2018 at 20:17
  • This post might be helpful: tex.stackexchange.com/questions/245540/…. If you prefer a TeX solution to your javascript one and can't get this to work let me know.
    – user30471
    Commented Jun 20, 2018 at 22:48

1 Answer 1


I did write a little Java program. If you have any better solution, please let me know! Either if it's about optimizing my Java code, having an alternative solution or anything else: let me know. Thanks.

This is the code:

package dir;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Dir {

    private static String fileType;
    private static String fileName;
    private static String pathName;
    private static String folderName;
    private static String oldFolderName = "";

    public static void main(String[] args) {
        File currentDir = new File(".");

    public static void content(File dir) {
        try {
            File[] files = dir.listFiles();
            for (File file : files) {
                if (file.isDirectory()) {
                    pathName = file.getName();
                    if (pathName.equals("src")) {
                        pathName = "main";
                } else {
                    fileType = Files.probeContentType(Paths.get(file.getAbsolutePath()));

                    if (fileType.equals("text/x-java")) {
                        fileName = file.getName();
                        folderName = file.getParentFile().getName();

                            folderName = file.getParentFile().getParentFile().getName();
                            folderName = file.getParentFile().getParentFile().getParentFile().getName();

                            System.out.println("\\section{" + folderName + "}");

                        System.out.println("\\subsection{" + pathName + "}");

                        System.out.println("\\subsubsection{" + fileName + "}");
                        System.out.println("\\lstinputlisting[language=java]{" + file.getCanonicalPath() + "}");
                        pathName = "";
                        oldFolderName = folderName;
        } catch (IOException e) {


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .