473,461 Members | 1,878 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 2895
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.