How to "Export" code the right way? - java

I have a project in Java and I need to make a code listing in part of my LaTeX documentation with all my classes and code. What is the best way to export the code? Is it simply just copy and paste, or is there a way to export the code properly to keep all the formatting?

It's easy enough to do:
for d in `find <projectdir> -name '*.java'; do
echo "$d" >> output.txt
cat "$d" >> output.txt
done
...but what possible purpose could dumping all the code into a document serve?

If LaTeX is the target, I'd search for some formatting templates for code. It's easy enough to get text into LaTeX, but the formatting will be a different matter.

I found a way:
http://texblog.wordpress.com/2008/04/02/include-source-code-in-latex-with-listings/
although, this way the code seems to run off the page..

You can also try the package listings you mention directly within Eclipse with the TEXlipse plugin
(source: sourceforge.net)
You can then see if you reference the right Java files in your references.
Used in this tutorial slide 19.
You can also try :
GNU source-highlight, which can produce Latex output, and may be more appropriate for batch processing a all lot of files.
pygmentize, which needs Python, and should also produce Latex output.

Related

Java: Class files containing source code?

I was inspecting the class file format since I wanted to add source code to the class file (which was possible in early Java versions) but all I found was a SourceFile attribute and the SourceDebug attribute. I was looking for the complete source code of the class to be bundled with the class file to ease the post-processing pipeline.
Does anyone know if my memories are wrong or how I can bundle the complete source code of a class within the class file so that I do not have to look up for the java-file when I want to check the source code?
Is there a compiler switch to do that?
Javac has a -g option adding additional debug information. Can someone tell me whats are the information it adds? Without the -g switch it generates lines of code index and source file information.
The main problem I have is generate a class file but only have a reference to a source file that might change. I want simply to bundle up source and class file.
In maven I can simply copy over all the source files to the target directory but would might be incompatible with Eclipse, IntelliJ and NetBeans IDE (and what not)... .
Using a decompiler will also provide a way to extract a useful representation of the source code since most decompiler will value the lines of code information and position the decompiled structures accordingly within the source code.
Since some scenarios will require access to comments and a correct representation on a char by char level, the decompiler would be a second rate solution.
One possible solution I found is defining a new class-file attribute (which is legal) that contains the source. Since the source is huge when compared to the class file, the content might be best compressed (yielding a 1:5 to 1:10 ratio).
This way the class file and the sources stay bundled.
The JVM specification guarantees that every JVM/Tool has to ignore unknown attributes.
I will invest into a wrapper of javac application, that ensures the source was not modified during compilation (and if yes, redo the compilation process) and after compilation is done adding the source code as a class-file attribute.
Since this will be incompatible to the IDE-build cycle of Eclipse (and most likely IntelliJ and NetBeans) it will also require a special post processor.
So integration will also require alternatives to the JavaBuilder.
Once the source code is attached to the class file in question it is very easy to do a lot of advanced stuff with it that helps with both maintaining and managing code. For me its important that the source code and a class stay together and the source information is a 100% percent equal to the source code it was compiled from.

Write a batch file that starts a java program

So I have a java project with multiple java files.
I know that is almost straight forward to start a java application using batch file. But that is for a pretty simple java program with a single class.
However I am wondering if it is possible to do that with in a scale of a project that you usually create using eclipse. A large project with multiple packages, classes and multiple java files.
My try was to write a script and apply on the main class as following
set path = C:\Program Files\Java\jdk1.7.0_25\bin
javac -classpath twitter/twitter4j-stream-3.0.5.jar;twitter4j-core-3.0.5.jar" sourcepath="lib/twitter4j-core-4.0.1.jar;lib/twitter4j-core-4.0.1.jar;lib/twitter4j-stream-4.0.1.jar;svm_light_lib Program.java
java Program
However when I start the .bat file it automatically closes.
Any Ideas ?
Thanks in advance
First, never overwrite the environment variable path, not even
temporarily. Append your folder instead: set "path=%path%;%mypath%" or set "path=%mypath%;%path%".
(There exists a particular path command but I'm not sure about right syntax: path=%path%;%mypath% with = assignment or path %path%;%mypath% without it).
Use full path to a program if you know it, e.g. "%mypath%\javac".
For better readability, values for -classpath and -sourcepath options are stored to the environment variables mycpth and mysrcp, respectively. Note and use proper " quotation and no spacing around = to avoid any leading and trailing spaces in all set commands.
pause to see all the javac output. Displays the message Press any key to continue . . .
Next code should be (syntax) error-free. However, success depends (among others) on classpath and sourcepath entries visibility as well...
set "mypath=C:\Program Files\Java\jdk1.7.0_25\bin"
set "path=%path%;%mypath%"
set "mycpth=twitter/twitter4j-stream-3.0.5.jar;twitter4j-core-3.0.5.jar"
set "mysrcp=lib/twitter4j-core-4.0.1.jar;lib/twitter4j-core-4.0.1.jar;lib/twitter4j-stream-4.0.1.jar;svm_light_lib"
"%mypath%\javac" -classpath "%mycpth%" -sourcepath "%mysrcp%" Program.java
pause
java Program
However I am wondering if it is possible to do that with in a scale of a project that you usually create using eclipse. A large project with multiple packages, classes and multiple java files.
Of course it is possible!
In this case, I suspect the problem is that you java command doesn't have a "-cp" argument. The java command is probably failing because it can't find twitter classes ... at runtime.
Remember to include "." on the classpath ... or else java won't find the file that you just compiled.
#JB Nizet's suggestion is also very important advice for finding out what is actually happening.

view code that was generated after compilation

How to look into code, that was generated after complilation?
I want to watch it and find how it was changed(because I particularly interested in type erasure).
I mean I can look into assembly code using javap -c SomeClass.class.
But how to look into generated code(with type erasure)?
got to Documents\NetBeansProjects\yourProjectName\build\classes\yourPackageName here you will see all your .class files that was generated after compilation and open them using any text editor.
Note: the .class files might contain some binary data and you will see some strange symbols
Note: an internal class will have the same name as its outer class but it will start with $ sign
After short amount of time I found Java Decompiler JD-GUI. It seems that the thing I was looking for. Sorry if did not clarify the question properly.

Is there a way to replace all system.out/err and e.printstacktraces with logger in netbeans?

Is there a feature in netbeans that'll let me easily config and replace all occurrences of "system.out" and "e.printstacktrace" to "logger.info/error/log" ?
I used find/replace to get rid of all the "system.out"s, and now i need to get rid of all the "printstacktraces", I can probably write a parser and read all my java files. But before I do that I just want to know if something like this is already implemented in netbeans, currently in netbeans 7.1 hints, they only show you where these things are, but I couldn't find an option for code refactoring.
Thanks
The following will open each .java file that contains printStackTrace in vim.
Of course you can substitute vim with your text editor of choice:
alias javafind='find . -name '\''*.java'\'' -print | xargs fgrep -il'
vim `javafind printStackTrace`
The first command creates an alias that returns all java files (starting in the current directory) that contain the first argument.
The second command says: open each file that contains the term printStackTrace with vim.
An even better solution would be to use sed to intelligently search/replace with a regex.

groovy require load source file

I have my java code running.
I added a groovy shell to evaluate the main groovy file.
What it does is simple.
GroovyShell.run (main.groovy)
Now, in my main.groovy, if I have other .groovy files I'd like to "require", how can I do that?
Is there something like "require, source, load, require_one, import" filename?
http://groovy.codehaus.org/Embedding+Groovy
if you scroll down to the section entitled "Dynamically loading and running Groovy code inside Java" you will see a full example with two different approaches to solving your problem
Actually, here is the answer:
I couldn't find a function like that in Groovy.
So, when evaluating your file, you have to parse all groovy files.
If you add new files on the fly and would like to evaluate them, you can't reuse the shell, not even through a singleton via the evaluated script
What you can do is register your parsed files and force the shell to "re-parse" them all again when re-evaluating your file.

Categories

Resources