Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Compiling Problem

Status
Not open for further replies.

Yorkiee

Technical User
Oct 2, 2003
18
IE
hi, i am having some problems in relation to the compiling of java programs. i am building an application whereby users can learn some programming skills. I have my java code entering into a textarea, and with the click of a button on the toolbar, i want this code to get compiled and then immediately see the result in that same textarea.
i would appreciate some feedback on this; what code to use, etc. i know its probably the getruntime method to user here.
also what code to use where the code eventually gets executed?
 
I don't want to question ur design, but I feel that, it is unnecessary to actually compile and run it. Instead, u can tell the user what the exact command is, for compiling it, and u can just show the exact output as plain text or a graphic. This is very much like how the books, or the tutorials on the net do.

But if u do want to do it ur way, one solution is, use Runtime.exec() and pass the argument as
"javac ClassFileName.java" or something like that. It returns a Process object, from which you can retrieve the output and error streams, and readLine() them till they end. That'll give u the output and error message.

Another way, which I haven't used in my life is, the java.lang.Compiler class. Looks like this has some interesting methods to compile ur code on the fly.

LOL A ship is safe in the harbour, but that's not what it is meant for!!! LOL
 
vkarthik :

The java.lang.Compiler class only provides a hook to the active JIT compiler - it does not compile your programs.

Yorkiee :

When they click *compile*, write the java code out to a file such as Tmp.java using a java.io.PrintWriter. Close the stream.

Then, as vkarthik suggested call :

Code:
Process p = Runtime.getRuntime().exec("javac Tmp.java");

to compile the code. The access the Process objects IO streams to retrieve any errors.

Then, as long as everything happended OK, call this code below to invoke the class on the fly :

Code:
Class targetClass = Class.forName("Tmp");
Method[] publicMethods = targetClass.getMethods();
  for (int j = 0; j < publicMethods.length; j++) {
	String fieldName = publicMethods[j].getName();
	Class typeClass = publicMethods[j].getReturnType();
	String fieldType = typeClass.getName();
	System.err.println(" Name: " + fieldName +", Type: " + fieldType);
        Object[] params = // you have to enter your parameters to the method call, or null if there are none
	try {
		publicMethods[j].invoke(this, params);
	} catch (IllegalAccessException iae) {
		iae.printStackTrace(System.err);
	} catch (InvocationTargetException ite) {
		ite.printStackTrace(System.err);
	}
   }

You will need to import the refelction package (import java.lang.reflect.*;) to run this code.

 
At


you can download a zip file. One of the files in the zip is "JavaCompilerServer.java"

From the source code :
Code:
JavaCompilerServer is a class representing an Internet server
 * that will receive Java source code, compile it, and return
 * the bytecode. If a syntax error is detected a String containing
 * the error message(s) is returned.

Probably you can find some usefull stuff. (PS : It uses javac)
 
sedj: But your class Foo has to be defined in a File 'Foo.java', not 'Tmp.java' - or do you think he only want's to compile code - snipplets?

yorkiee: 'immediately see the results' - does that mean '(compiler errors and warnings) or nothing' or 'the generated class - file' or some kind of execution if it works?

The 'beanshell' - project might be a interesting place to find useful things.

If working with 'runtime' and the ordinary compiler, i would suggest:
for the traps using Runtime, and how to catch the Output.
 
stefanwagner :

What are you talking about "Foo" ?? I never mentioned Foo ???!!!

Obviously the guy would have to use his noggin and apply my advice ...
 
thanks for that sedj. i have however started creating some of my own code with tackling this! im am however having a problem with it!
As you can see i have set up an array with the various paths in each element.
below is the the error that i am getting when compiled. The error is in element 3 of the array. i have the paths all correct in the other elements of the array.
what would be the problem here, and what could i do to resolve this? am i missing something in the code?

//error
"ThesisFrame.java".cannot resolve symbol: variable classpath in class thesis.ThesisFrame

// sample code//

String[] execArray = new String[7];


execArray[0] = "c:/JBuilderX/jdk1.4\bin/javac.exe";
execArray[1] = "-deprecation";
execArray[2] = "-classpath";
execArray[3] = classpath.toString();
execArray[4] = "-sourcepath";
execArray[5] = "c:/Thesis/Sample_Programs/";
execArray[6] = "GUIcolor.java";


Process p = Runtime.getRuntime().exec(execArray);

}
 
well yes i defined as a String, (or would that be object)ie:

String classpath = new String();
Is this wat you meant stefanwagner? ne ideas, because kinda stuck on this sticky area of java!
 
Yes. But I guess you defined it in the wrong scope, since the errormessage claims, that ThesisFrame doesn't know 'classpath'.
 
There are a lot of classes that might be helpfull. I have no doc about them and never tried them. They are in
Code:
sun.tools.java.*;
sun.tools.javac.*;
...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top