diff --git a/compiler/compile_target_code.m b/compiler/compile_target_code.m
index cd6f956..0722f7f 100644
--- a/compiler/compile_target_code.m
+++ b/compiler/compile_target_code.m
@@ -1125,10 +1125,18 @@ compile_java_files(ErrorStream, JavaFiles, Globals, Succeeded, !IO) :-
     % Also be careful that each option is separated by spaces.
     JoinedJavaFiles = string.join_list(" ", JavaFiles),
     string.append_list([InclOpt, DirOpts,
-        Target_DebugOpt, JAVAFLAGS, " ", JoinedJavaFiles], CommandArgs),
+        Target_DebugOpt, " ", JoinedJavaFiles], CommandArgs),
+    % The -J flags for the Java launcher called by javac must not be put into
+    % an @argument file, so iff they are set, they are added directly to
+    % the executable path.
+    ( JAVAFLAGS = "" ->
+        JavaCompilerWithFlags = JavaCompiler
+    ;
+        JavaCompilerWithFlags = JavaCompiler ++ " " ++ JAVAFLAGS
+    ),
     invoke_long_system_command_maybe_filter_output(Globals, ErrorStream,
-        cmd_verbose_commands, JavaCompiler, CommandArgs, MaybeMFilterJavac,
-        Succeeded, !IO).
+        cmd_verbose_commands, JavaCompilerWithFlags, CommandArgs,
+        MaybeMFilterJavac, Succeeded, !IO).
 
 :- func java_classpath_separator = string.
 
