http://www.perlmonks.org?node_id=940717

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

    JediMasterT:

    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.)

    ...roboticus

    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 (Patriarch) 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.