Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Java Auto-Compile

by JediMasterT (Sexton)
on Nov 29, 2011 at 22:46 UTC ( #940717=CUFP: print w/replies, xml ) Need Help??

I've been using a lot of Java for school. Because I run Linux, this means going into the command line and running javac, sometimes up to 5 different files for one program. And if I have one thing wrong, I have to do it all over again. So finally, I got tired of it and wrote this:

#!/usr/bin/perl my $file; my $last = 1; while (@ARGV and $last) { last if ($ARGV[0] =~ /^\-a$/); $file = shift; die "$file\.java does not exist\!\n" unless (-e $file."\.java"); system ("javac", $file.".java"); } shift if !@ARGV; exec ("java", $file, @ARGV);

For every argument, provided that the file exists, it compiles the .java, the runs the last file it compiles. If you have "-a", it passes the arguments after that to the program! I know it's not much, but it's my first legitimate, made for use Perl script, so I thought I'd post it.

Now, if only I could tell it to not run if I get a compile error...

UPDATE: I got the code to die if there was a compile error, but still print out the error messages. Now it looks like this:

#!/usr/bin/perl my $file; while (@ARGV and $last) { if ($ARGV[0] =~ /^\-a$/) { shift; last; } $file = shift; die "$file\.java does not exist\!\n" unless (-e $file."\.java"); $error= system("javac", $file."\.java"); } die "Compiling error, could not run!\n" if ($error); shift if !@ARGV; exec ("java", $file, @ARGV);

Replies are listed 'Best First'.
Re: Java Auto-Compile
by roboticus (Chancellor) on Nov 29, 2011 at 23:08 UTC


    You'll find the 'make' command on most versions of *nix, and it gives you a nice way to recompile anything that's out of date. If you're with the "in crowd", you might consider learning 'Ant' which is similar but different. (If I understand it correctly, rather than using a "horrible text file", it uses a horrible XML file to describe how to keep the project up-to-date.)


    XML: a horribly-overengineered solution to a simple problem.

    Update: 'nant' to Ant (NAnt is for .NET), thanks for the catch, Perlbotics.

Re: Java Auto-Compile
by ikegami (Pope) on Nov 29, 2011 at 23:00 UTC

    Good, but it could be improved some.

    • $last is never really used.
    • You might want to continue compiling files (but not run the program) if one of the files is missing or doesn't compile.
    • You shouldn't try to execute the program if there's been a compilation error.
    • You could avoid compiling files that haven't changed since the last time you've compiled them.
      • I forgot that was there. It was part an earlier version
      • The way that java works, if you change one thing, you have to recompile it all
      • that's what I asked

        The way that java works, if you change one thing, you have to recompile it all

        That's not true. Like other languages, you just have to recompile what depends on the class you changed.

        That said, that requires a list of dependencies, and finding that list is beyond what this tool should do.

        that's what I asked

        I didn't notice there was a question, since I didn't look for one, since this isn't SoPW.

        javac surely returns a non-zero exit code on error, and zero on success.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://940717]
Approved by muba
and the monastery is silent...

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (11)
As of 2018-06-25 13:52 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.