Print the sourcecode of a whole java project

40,312

Solution 1

If you don't mind installing Cygwin, or running on Linux, the following command will do what you want:

enscript -r -2 --file-align=2 --highlight --line-numbers -o - `find . -name '*.java'` | ps2pdf - files.pdf

enscript is a program for converting text files to a variety of output formats; PostScript is the default, but you can also produce HTML, RTF, and a few others. The -r option says to print in landscape, -2 is two columns per page (save trees), --file-align=2 says that each new file should start on its own physical page, --highlight turns on language-specific syntax highlighting (it will try to figure out the language, or you can specify "java"), --line-numbers should be obvious, and -o - sends the output to standard-out (where it's piped to ps2pdf).

find generates the list of files; here I'm telling it to find all Java files under in the current directory. The output is passed as arguments to enscript; for "50-100 files" you should be OK, but you might need to read about xargs. You could get rid of the -name argument to generate a list of all files, or add multiple -name arguments to add more file types to the list; I wouldn't go with the "all files" approach, because then you'll get source-control files.

ps2pdf takes the PostScript output from enscript and converts it to PDF, which you can print.

Solution 2

If you can afford spending $50 buy Ultraedit, open all files and print it...

Ultraedit features about printing includes:

  • Print preview
  • Print line numbers
  • Print command doesn't print hidden lines
  • Headers/footers (with alignment commands), margins and page breaks
  • Printing of syntax highlighting in color
  • Print 2 pages on one sheet in landscape or portrait modes
  • Separate font selection for display and printer (supports all fonts installed including True Type fonts)
  • Print all open files

Solution 3

I've used Java2Html from Eclipse in the past. See whether it suits your needs.

Solution 4

I don't think you can do this within Eclipse (of course you could write a plugin which does this).

If you use Ant as your build tool you could use the concat task and then print the resulting file

<concat destfile="${concat.src.dir}/concat.txt" force="no">
    <filelist dir="${src.dir}" includes="**/*.java **/*.xml" />
</concat>

Solution 5

If you want the formatting exactly as it is in Eclipse, then you will probably have to print from Eclipse. You'll spend more time trying to duplicate the print formatting that you have in Eclipse with another method.

Another important point: If you are using folding in the text editors in Eclipse, then the folded lines will not be displayed in the printed version.

If you really really have to furnish the source code as trees, then I would suggest that you try and persuade your clients that colour and syntax highlighting are not important, and then format everything in Eclipse, and print from elsewhere. There are suggestions for the line numbers etc in other answers.

Share:
40,312
oliver31
Author by

oliver31

Updated on May 21, 2020

Comments

  • oliver31
    oliver31 about 4 years

    I have to print the whole sourcecode of a java-project. The final version should look like: Eclipse: File -> Print. But with this function you can only print one file at once.

    Is there a way to print (or create a pdf/rtf of) the whole project (all *.java, *.xml, ... files) with one command?

    Im using eclipse galileo on windows xp sp3


    EDIT: For each class/file the page should (more or less) look like this:

    C:\..\..\..\LibraryExtractor.java

    1 package utils.libraries;
    2
    3 import java.io.File;
    9
    10 /**
    11 * @
    12 * @
    13 * @
    14 */
    15 public class LibraryExtractor {
    16
    17 /**
    18 * 
    19 * 
    20 *
    21 * 
    22 * 
    23 *
    24 *
    25 */
    26 public static void extranctLibrary(String library, File targetFile) throws
    IOException, URISyntaxException {
    27 targetFile.getParentFile().mkdirs();
    28 if (!targetFile.exists())
    29 targetFile.createNewFile();
    30
    31 ClassLoader classLoader = LibraryExtractor.class.getClassLoader();
    32 InputStream in = classLoader.getResourceAsStream(library);
    33 OutputStream out = new FileOutputStream(targetFile);
    34
    35 byte[] buf = new byte[1024];
    36 int len;
    37
    38 while ((len = in.read(buf)) > 0)
    39 out.write(buf, 0, len);
    40
    41 in.close();
    42 out.close();
    43 }
    44 }
    45
    

    SOLUTION:

    1. enscript (with Cygwin)

    2. Java2Html Eclipse-Plugin (only works with Europa)

    • matbrgz
      matbrgz over 14 years
      Number of files? Number of projects?
    • oliver31
      oliver31 over 14 years
      50 - 100 files for one project
    • Gopi
      Gopi over 14 years
      Where was the Technology? Paper was replaced by Softcopies... and lots of version control systems are available. Why do want to print all files?
    • oliver31
      oliver31 over 14 years
      because it is required... it surely wasn't my idea -.-
    • BalusC
      BalusC over 14 years
      I see you added a bounty. But you asked this question 17 december, which means in the meanwhile you've had a plenty of time to print the files one by one. Just open them all and press ctrl+p on each of them. "Only" 50-100 times. Maybe one hour of work, but certainly not 3 weeks of work. Was you really that lazy?
    • oliver31
      oliver31 over 14 years
      @BalusC: I have to do a ten-day project in march. At the end i have to print the whole documentation (projectmanagement, uml-diagramms, other stuff) + the whole source code at least twice. i don't really want/have time to print every file one by one.
    • BalusC
      BalusC over 14 years
      Rather use a batch/shell script or write something in Java yourself with help of javax.print API.
    • Boris Callens
      Boris Callens over 14 years
      Have none of you ever needed that for school? Don't know if this is @r3zn1k's case, but I remember one of my teachers (who was severely skill-challenged) to flip through the entire code print-out and ask me to explain what all the automatically generated SOAP code was about... This was after he inquired one of my co-students about the type of printer it was printed with..
    • Boris Callens
      Boris Callens over 14 years
      Also minor heads-up: do you realize your function is called "extranctLibrary"?
  • cal meacham
    cal meacham over 14 years
    You didn't say anything about that did you in your question
  • delfuego
    delfuego over 14 years
    Yep, r3zn1k, it serves you well to actually spell out your requirements in your question. :)
  • oliver31
    oliver31 over 14 years
    The final version should look like "Eclipse: File -> Print" == linenumbers and syntax-highlighting
  • Newbie
    Newbie over 14 years
    Just checked, it has a free trial version, so this will probably solve your problem for now.
  • Matthew Farwell
    Matthew Farwell over 14 years
    Actually, eclipse only prints line numbers if the line number checkbox is checked in the text editor properties. So if I don't have line numbers switched on, then I don't get line numbers.
  • David Brunelle
    David Brunelle over 14 years
    I remember using that a while ago. It was quite nice since I could connect to a FTP and read/write files from there.
  • JuanZe
    JuanZe over 14 years
    @David I use PSPad and also the feature I enjoy the most is the same: connecting via FTP and edit files directly on the server.
  • Lytol
    Lytol over 14 years
    Didn't read your whole command til after I commented -- note that if you drop the -o -, enscript will spool directly to the printer (or you can specify the printer with -P <printer-name>) and you can skip the pipe to ps2pdf.
  • MaD70
    MaD70 over 14 years
    Side note: instead of Cygwin, one can use relevant commands from GnuWin32 (gnuwin32.sourceforge.net). In particular Enscript for Windows (gnuwin32.sourceforge.net/packages/enscript.htm) and FindUtils for Windows (gnuwin32.sourceforge.net/packages/findutils.htm) - see the execdir option of find to run a command for each file.
  • oliver31
    oliver31 over 14 years
    Thanks for your answer. "Print all open files" is useful for few files. For a whole project it's not the best solution...
  • oliver31
    oliver31 over 14 years
    Thank you for your answer. My "problem" can be solved with the Java2Html Eclipse-Plugin. Okay it doesn't work with Galileo (it does with Europa) and the conversion to RTF with multiple files doesn't work, but you can still convert everything to HTML (edit the HTML) and than copy the HTML-Output to an RTF file. If you don't want to install Cygwin this is the best solution.
  • Piotr Sobczyk
    Piotr Sobczyk almost 12 years
    That seems like a great tip but I can't find ps2pdf in Cygwin packages? How to install this utility for Cygwin?