Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / January 2008

Tip: Looking for answers? Try searching our database.

Self-compiling .java file

Thread view: 
jtienhaara@yahoo.com - 18 Jan 2008 07:55 GMT
Hi folks.

Newbie (to the newsgroup) here -- hopefully not stomping on
any rules...  :)

This is a cutesy little snippet for Java / *nix / *nux
programmers.  It's been done for other languages but I
haven't come across any examples for Java so figured I'd
throw one together.  Tested only in bash.

The idea: a .java file that compiles itself, builds its own
javadoc, and executes itself.

Source code:

http://www.mountaingrouse.com/software/self_compiling_class.java

Javadoc:

http://www.mountaingrouse.com/software/self_compiling_class/

Suggestions and bug reports are welcome.  Thanks and enjoy,

Johann Tienhaara
jtienhaara@yahoo.com
Andreas Leitgeb - 18 Jan 2008 09:38 GMT
> Newbie (to the newsgroup) here -- hopefully not stomping on
> any rules...  :)
Don't think so, seems mostly relevant to java :-)

> This is a cutesy little snippet for Java / *nix / *nux
> programmers. ...
[quoted text clipped - 3 lines]
> Source code:
> http://www.mountaingrouse.com/software/self_compiling_class.java

Amazing :-)

I think it can be further simplified, by using this as first line:

//bin/sh << "EOF2"
By putting the "end word" under double-quotes, you no longer need
to backslash-escape each dollar-sign in the actual script-part of
the file.

You should also use this technique for "cat >/dev/null"-ing the
actual java source-code, because otherwise, eventual occurrences
of e.g. `date` (and just as well also nastier things than that)
in the java-part would still be executed! Not that any backticks
or $(...) were appearing in your sample, but I see this more as
a recipe to wrap simple java-code, and some arbitrary thusly
wrapped code could easily contain such thing inside a string-
constant or comment.

Anyway: using  //bin/... for the switching... cool!  :-)
jtienhaara@yahoo.com - 18 Jan 2008 20:56 GMT
> //bin/sh << "EOF2"
> By putting the "end word" under double-quotes, you no longer need
> to backslash-escape each dollar-sign in the actual script-part of
> the file.

Thanks for the feedback, and especially for the great
suggestion Andreas.  I wish I had known about << "EOF" years
ago!  :)

I updated the source code to incorporate Andreas's
suggestion.  Although this info doesn't pertain to Java,
just a quick note that capturing $0 and $* inside << "EOF"
is a bit of a mind-bender...  :)  Interestingly I had to
start the new version of the .java file off with //bin/cat
(instead of //bin/sh) in order to get at $0 and $* further
down in the file.

Anyway thanks much for the tip!  Cheers and enjoy,

Johann
Martin Gregorie - 18 Jan 2008 15:19 GMT
> Hi folks.
>
[quoted text clipped - 12 lines]
>
> http://www.mountaingrouse.com/software/self_compiling_class.java

Neat stuff. Thanks for posting it.

Signature

martin@   | Martin Gregorie
gregorie. | Essex, UK
org       |



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.