Connecting Tech Pros Worldwide Forums | Help | Site Map

C# : Compiling Java Code in my program

Newbie
 
Join Date: Oct 2007
Posts: 4
#1: Oct 30 '07
Hello, I am attempting to compile java code from within my program. Basically I have a file in the directory with Java source code, and I want to be able to compile it from within my program. The only solution I have found so far is running the javac process :
Process.Start("javac source.java); But this doesn't seem to work (File not found).
And i would also like to get the output after compiling to a string (Error messages etc). Any other suggestions? Help is much appreciated.


Edit : Worked out now using Process.Start("javac","Leecher.java");
Only problem is now how would i get the output of that to a string?

Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#2: Oct 31 '07

re: C# : Compiling Java Code in my program


Quote:

Originally Posted by KyleUbenk

Hello, I am attempting to compile java code from within my program. Basically I have a file in the directory with Java source code, and I want to be able to compile it from within my program. The only solution I have found so far is running the javac process :
Process.Start("javac source.java); But this doesn't seem to work (File not found).
And i would also like to get the output after compiling to a string (Error messages etc). Any other suggestions? Help is much appreciated.


Edit : Worked out now using Process.Start("javac","Leecher.java");
Only problem is now how would i get the output of that to a string?

Try using StartInfo and RedirectStandardOutput set to true. Something like (N.B This has not been tested)
Expand|Select|Wrap|Line Numbers
  1. public static String compileJava (String file) {
  2.         Process p = new Process();
  3.             p.StartInfo.FileName = "javac.exe";
  4.             p.StartInfo.Arguments = file;
  5.             p.StartInfo.UseShellExecute = false;
  6.             p.StartInfo.RedirectStandardOutput = true;
  7.             p.Start();
  8.                   String output = p.StandardOutput.ReadToEnd();
  9.                 Console.WriteLine(output); 
  10.                 return output;    
  11.         }
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#3: Oct 31 '07

re: C# : Compiling Java Code in my program


Quote:

Originally Posted by r035198x

Try using StartInfo and RedirectStandardOutput set to true. Something like (N.B This has not been tested)

Expand|Select|Wrap|Line Numbers
  1. public static String compileJava (String file) {
  2.         Process p = new Process();
  3.             p.StartInfo.FileName = "javac.exe";
  4.             p.StartInfo.Arguments = file;
  5.             p.StartInfo.UseShellExecute = false;
  6.             p.StartInfo.RedirectStandardOutput = true;
  7.             p.Start();
  8.                   String output = p.StandardOutput.ReadToEnd();
  9.                 Console.WriteLine(output); 
  10.                 return output;    
  11.         }

Just tested it. You should remove that .exe from line 3 where I wrote the javac command.
Reply