Skip to content

Instantly share code, notes, and snippets.

@tayl0r
Created December 31, 2014 14:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tayl0r/bd943708358abf64732f to your computer and use it in GitHub Desktop.
Save tayl0r/bd943708358abf64732f to your computer and use it in GitHub Desktop.
Java 2048m Command Line Interceptor
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.IO;
namespace CommandlineInterceptor {
class Program {
static void Main(string[] args) {
//let's get the exe from our assembly location
string exeLocation = typeof(Program).Assembly.Location;
//now to get the real location, right before the .exe, add -default
exeLocation = exeLocation.Replace(".exe", "-default.exe");
//string javaExe = @"C:\Program Files\Java\jdk1.8.0_20\bin\java-default.exe";
string javaExe = exeLocation;
//go through the arguments and replace anything that starts with -Xmx
string arguments = GetFilteredArgString(args);
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = javaExe;
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.RedirectStandardError = true;
startInfo.RedirectStandardInput = true;
startInfo.Arguments = arguments;
//startInfo.RedirectStandardInput = true;
using (Process process = new Process()) {
//Console.Out.WriteLine("1: init");
process.StartInfo = startInfo;
process.OutputDataReceived += new DataReceivedEventHandler(HandleOutputData);
process.ErrorDataReceived += new DataReceivedEventHandler(HandleErrorData);
//Console.Out.WriteLine("2: start");
process.Start();
//Console.Out.WriteLine("3: output read line");
process.BeginOutputReadLine();
//Console.Out.WriteLine("4: error read line");
process.BeginErrorReadLine();
//Console.Out.WriteLine("5: is piped input?");
if (IsPipedInput()) {
//Console.Out.WriteLine("6: input peek");
while (Console.In.Peek() != -1) {
//Console.Out.WriteLine("7: input read write");
string input = Console.In.ReadLine();
process.StandardInput.WriteLine(input);
}
}
//Console.Out.WriteLine("8: wait for exit");
process.WaitForExit();
}
}
private static bool IsPipedInput() {
try {
bool isKey = Console.KeyAvailable;
return false;
} catch {
return true;
}
}
private static void HandleOutputData(object inProcess, DataReceivedEventArgs inLine) {
//send the data to the console, no matter what it is...
Console.Out.WriteLine(inLine.Data);
}
private static void HandleErrorData(object inProcess, DataReceivedEventArgs inLine) {
//send the data to the console, no matter what it is...
Console.Error.WriteLine(inLine.Data);
}
private static string GetFilteredArgString(string[] inArgs) {
StringBuilder sb = new StringBuilder();
foreach (string arg in inArgs) {
if (arg.Contains("-Xmx")) {
sb.Append("-Xmx2048 ");
} else {
sb.Append(arg);
sb.Append(" ");
}
}
return sb.ToString();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment