Supply temporary directory to genrule() invoked java_binary()

JVMs on UNIX systems typically hardcode to use /tmp as the default
temporary area, unless system property java.io.tmpdir is set on
the command line. Pass the genrule's TMP location to the binary
as a system property so it can be used automatically.
diff --git a/src/com/facebook/buck/java/JavaBinaryRule.java b/src/com/facebook/buck/java/JavaBinaryRule.java
index 9c488ab..4085396 100644
--- a/src/com/facebook/buck/java/JavaBinaryRule.java
+++ b/src/com/facebook/buck/java/JavaBinaryRule.java
@@ -43,6 +43,7 @@
 
 import java.io.File;
 import java.io.IOException;
+import java.util.Collections;
 import java.util.List;
 import java.util.Map;
 
@@ -174,15 +175,23 @@
 
   @Override
   public String getExecutableCommand() {
+    return getExecutableCommand(Collections.<String>emptyList());
+  }
+
+  public String getExecutableCommand(List<String> jvmArgs) {
     Preconditions.checkState(mainClass != null,
         "Must specify a main class for %s in order to to run it.",
         getBuildTarget().getFullyQualifiedName());
-
-    return String.format("java -classpath %s %s",
+    StringBuilder cmd = new StringBuilder();
+    cmd.append("java");
+    if (!jvmArgs.isEmpty()) {
+      cmd.append(' ').append(Joiner.on(' ').join(jvmArgs));
+    }
+    return cmd.append(String.format(" -classpath %s %s",
         Joiner.on(':').join(Iterables.transform(
             getTransitiveClasspathEntries().values(),
             Functions.RELATIVE_TO_ABSOLUTE_PATH)),
-        mainClass);
+        mainClass)).toString();
   }
 
   public static class Builder extends AbstractBuildRuleBuilder {
diff --git a/src/com/facebook/buck/shell/Genrule.java b/src/com/facebook/buck/shell/Genrule.java
index f3bb075..553574e 100644
--- a/src/com/facebook/buck/shell/Genrule.java
+++ b/src/com/facebook/buck/shell/Genrule.java
@@ -16,6 +16,7 @@
 
 package com.facebook.buck.shell;
 
+import com.facebook.buck.java.JavaBinaryRule;
 import com.facebook.buck.model.BuildTarget;
 import com.facebook.buck.rules.AbstractBuildRuleBuilder;
 import com.facebook.buck.rules.AbstractCachingBuildRule;
@@ -340,10 +341,18 @@
             buildTarget, cmd, getType().getName(), getFullyQualifiedName());
       }
       BinaryBuildRule binaryBuildRule = (BinaryBuildRule)matchingRule;
+      String bincmd;
+      if (binaryBuildRule instanceof JavaBinaryRule) {
+        List<String> jvmArgs = Lists.newArrayListWithCapacity(4);
+        jvmArgs.add(String.format("-Djava.io.tmpdir=%s", tmpDirectory));
+        bincmd = ((JavaBinaryRule)binaryBuildRule).getExecutableCommand(jvmArgs);
+      } else {
+        bincmd = binaryBuildRule.getExecutableCommand();
+      }
 
       // Note that matcher.group(1) is the non-backslash character that did not escape the dollar
       // sign, so we make sure that it does not get lost during the regex replacement.
-      String replacement = matcher.group(1) + binaryBuildRule.getExecutableCommand();
+      String replacement = matcher.group(1) + bincmd;
       matcher.appendReplacement(buffer, replacement);
     }
     matcher.appendTail(buffer);