473,402 Members | 2,055 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

C# : Compiling Java Code in my program

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?
Oct 30 '07 #1
2 2888
r035198x
13,262 8TB
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.         }
Oct 31 '07 #2
r035198x
13,262 8TB
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.
Oct 31 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Herman | last post by:
Hi everyone, I'm implementing Dijkstra's algorithm for a class project, and I'm having trouble compiling the class that runs the algorithm. All of the other files compile fine except for this one....
5
by: Jo Vermeulen | last post by:
Hello, I was wondering if Java supports a feature of C# (or .NET in general), namely compiling code at runtime. This is nice for scripting purposes. Users can script the application in real...
29
by: Maurice LING | last post by:
Hi, I remembered reading a MSc thesis about compiling Perl to Java bytecodes (as in java class files). At least, it seems that someone had compiled scheme to java class files quite successfully....
1
by: genc ymeri | last post by:
Hi, I'm coding a C# Win Application. Somehow we need to let the "end-users" of our application to write some Java code in one of our app's winform called : "Coding". What I would like to do is...
12
kirara
by: kirara | last post by:
Hello, I am new to rmi and linux enviroment, I have been trying to test this code (java) on uinx where there are three machines: -One as a Controller -an execution host - a submitter I...
6
by: aureao4 | last post by:
I'm new to Java and programming. I'm trying to code a payroll program and continue getting errors. The program worked last week, this week I have to add set, get and a class. I've written the class...
3
eyeofsoul
by: eyeofsoul | last post by:
hello..i am really new to java..i am using netbean 4.1.i also install netbean 5. the problem is i got this error when compiling my CopyFile.java..the error said "java.lang.NoClassDefFoundError:...
4
by: betalpha | last post by:
I'm having trouble compiling my program in Unix. I initially developed it on Windows in Eclipse. I have two files: Tokenizer.java and TokenizerMain.java. They are both in a directory called...
4
by: anon538 | last post by:
I have been interested in switching from Java to C# for a long time now. I typically write small, stand alone programs for contests. Each program is completely self contained. I am used to, in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.