# How to import Java source code from multiple folders to LaTeX

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:

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:

\documentclass{article}
\usepackage{pgffor}
\usepackage{listings}
\begin{document}

\foreach \java in  {hello, hello} {
\begin{figure}[htpb]
\lstinputlisting[language=java]{\java.java}
\caption{Source code for \textsf{\java.java}}
\label{fig:\java}
\end{figure}
}

\end{document}

• You should be able to use UNIX style relative pathnames, such as ../folder/file – John Kormylo Jun 20 '18 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 Jun 20 '18 at 17:28
• @Marijn ... thx, I did it :-) ... it works ;-) – Aurel Ernst Jun 20 '18 at 20:08
• @AurelErnst good job! – Marijn Jun 20 '18 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 Jun 20 '18 at 22:48

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(".");
content(currentDir);
}

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";
}
content(file);
} else {
fileType = Files.probeContentType(Paths.get(file.getAbsolutePath()));

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

if(folderName.equals("src")){
folderName = file.getParentFile().getParentFile().getName();
}else{
folderName = file.getParentFile().getParentFile().getParentFile().getName();
}

if(!folderName.equals(oldFolderName)){
System.out.println("\\section{" + folderName + "}");
}

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

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

}