473,695 Members | 2,626 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# : Compiling Java Code in my program

4 New Member
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 2907
r035198x
13,262 MVP
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 RedirectStandar dOutput 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 MVP
Try using StartInfo and RedirectStandar dOutput 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
2523
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. I am using the compiler like this: javac -classpath .. <filename.java> The source code of the classes follow. Dijkstra.java is the class that won't compile, because the compiler doesn't recgonise the Vertex and GraphMap classes, even...
5
5909
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 programming languages (such as C#, VB.NET, ...). This is done using the System.CodeDom.Compiler namespace, if I recall correctly. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCodeDomCompiler.asp
29
3445
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. I'm wondering if something of such had been attempted in python, as in compiling X language into .pyc. I do not understand the schematics of .pyc files but I assume that they are the so called python bytecode files. Or is there any...
1
1627
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 to have a button that compiles that code and if there are errors to show them up,,,,, So far I'm planing to use the Sun javac.exe. So my question is " Does anyone know how to call JavaC.exe & retrieving the
12
2946
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 do the following to run them: - type "make" in all the machines to compile the all the java classes along with the remote objects.
6
5433
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 but when I compile the program I get 13 errors. Can anyone assist me with this? Thank you! Errors I get: C:\Java>javac *.java Payroll3.java:18: illegal start of type while ( !exit ) ^ Payroll3.java:18: <identifier>...
3
1350
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: test1/CopyFile Exception in thread "main" Java Result: 1" i am compiling using netbean 4.1 somebody plz help me..
4
2549
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 "tokenizer" I'm compiling from my home directory: % javac tokenizer/TokenizerMain.java ./tokenizer/Tokenizer.java:5: 'class' or 'interface' expected package tokenizer; ^ tokenizer/TokenizerMain.java:2: cannot access tokenizer.Tokenizer bad class...
4
5479
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 Java, creating a file, say pr21.java with a single class and main method, compiling it, and running it. Since I've switched to C#, I have been using MS Visual C# Express 2008 as my IDE. It has this weird obsession with encapsulating all of my programs...
0
8640
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, weíll explore What is ONU, What Is Router, ONU & Routerís main usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9122
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9001
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8832
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5841
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4348
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4587
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2280
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1980
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.