views:

997

answers:

9

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)

+3  A: 

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>
jitter
good idea, but not the perfect solution -> linenumbers, ...
r3zn1k
You didn't say anything about that did you in your question
jitter
Yep, r3zn1k, it serves you well to actually spell out your requirements in your question. :)
delfuego
The final version should look like "Eclipse: File -> Print" == linenumbers and syntax-highlighting
r3zn1k
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.
MatthieuF
+12  A: 

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.

kdgregory
awesome. enscript sounds like it could come in handy.
Mike Nelson
+1 I use `enscript` all the time, especially for code-review preparation!
notnoop
+1 `enscript` FTW... but I guess being on Win requires a few extra steps for the OP
Lytol
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`.
Lytol
Side note: instead of Cygwin, one can use relevant commands from GnuWin32 (http://gnuwin32.sourceforge.net/). In particular Enscript for Windows (http://gnuwin32.sourceforge.net/packages/enscript.htm) and FindUtils for Windows (http://gnuwin32.sourceforge.net/packages/findutils.htm) - see the execdir option of find to run a command for each file.
MaD70
+1, very powerful. Thanks a lot!
r3zn1k
+5  A: 

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
JuanZe
Just checked, it has a free trial version, so this will probably solve your problem for now.
Edan Maor
I remember using that a while ago. It was quite nice since I could connect to a FTP and read/write files from there.
David Brunelle
@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.
JuanZe
Use Notepad++. It has everything ultraedit has and is FOSS
borisCallens
Thanks for your answer. "Print all open files" is useful for few files. For a whole project it's not the best solution...
r3zn1k
A: 

I would use pygments with linenos enabled as explained in http://pygments.org/docs/formatters/

Xavier Combelle
+1  A: 

An option that looks fancy is to use vim in batch mode to generate a bunch of colorized HTML files and then print by dragging all of them to the printer (I know that can be done, some time ago a colleague printed the whole J2SE API, and I hope she didn't it page by page xD).

find -name "*.java" -exec vim '+set nu' +TOhtml +wq +q '{}' \;
fortran
+1  A: 

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.

MatthieuF
A: 

how about saving some paper and saving some environment. I have used similar ant method mentioned above which has worked for me in the past.

+3  A: 

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

Gregory Pakosz
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.
r3zn1k
+3  A: 

I'm sorry, but this is just offensive. There is no reason to be wasting all that paper for printing out source code, for god's sake!

If you're doing this to back up your (or your company's) code, you need to look into some sort of versioning system and create a daily backup routine, things you should already be doing anyways. There are plenty of good, FREE, easy-to-setup solutions for both of these tasks, and I can't imagine it's that hard to find someplace offsite to store your backups.
Using versioning would also have the advantage of being able to look at the code from any point in time, not just the last day you printed it off.

If you're doing this to share the code with someone, just give them read-only access to all (or part!) of the source-code repository.

If this is for a class, talk to your professor and insist on burning the files to a CD. It is less wasteful, and much easier for both him and you.

BlueRaja - Danny Pflughoeft
I'm guessing r3zn1k finds what he is asking for just as offensive as you do: What would _you_ do if your customer absolutely insisted on a printed copy?
Yang
The customer does not get the right to the source on closed-source projects, and on open-source projects, they are paying for support, not the program itself (besides, they already have the source).
BlueRaja - Danny Pflughoeft
Offensive? seriously? Sometimes the question is wrong, but please, printing source code is not a crime. Don't blow things out of proportion. Some people want to see actual code on paper for some reason. And if a professor has decided that it's easier to use printed source code, so be it. As for your comment about customer access to the source, the customer often gets access to the source even for proprietary projects. And if the guy paying me insists on a paper printout, guess what? he'll get it. How do I know why he needs it? Maybe he wants to decorate his house. It's his money, his decision.
Mr. Shiny and New
You are very bold. I know of very few companies that would give out their closed-source source-code for any reason, other than "We'll pay you an insane amount of (more) money."
BlueRaja - Danny Pflughoeft
MaD70
plenty of companies give out their closed source code in various forms. all the big purchases we (big gov't agency) make require the vendor to put the source in escrow in case they go out of business. not to mention plenty of contractors write custom code for a specific client, who of course will get to see the code they paid for. Also - students rarely get to dictate terms to their professors.
Peter Recore