Results 1 to 14 of 14
Thread: Help compiling code dynamically
- 07-23-2011, 05:09 PM #1
Help compiling code dynamically
I'm not sure if what I'm trying to do is possible. This is a university project I'm trying to come up with, but I need some advice on where to go from here.
I'm building a website that will have a few basic OOP lessons with exercises the user has to solve. As for the language used in these exercises, I'm using java (of course).
My website will have a java applet with a textarea, where the user will be able to type their solution to the exercise.
That is all set and working already, but now I need to take the code written by the user and try to compile it, so I can tell the user if what they wrote is correct. This is where I need help: I'm guessing the best way to go is to generate a .java file containing the text the user wrote in the textarea of my applet, and then try to compile it into a .class file and catch any compilation errors so I can tell the user what went wrong.
My first question would be: is that the right way to go? There's another option?
My second question is: how do I achieve this?
I'm open to any recommendations, articles you can link me to, or any information that could help me.
Thanks a lot!!Last edited by Fubarable; 07-30-2011 at 08:24 PM. Reason: Corrected spelling of "Dynamically"
- 07-23-2011, 05:46 PM #2
See the JavaCompiler class for one option on compiling from within a java program. You'll need the JDK installed for this to work.take the code written by the user and try to compile it
- 07-29-2011, 06:47 PM #3
Thanks, Norm. I'm reading some stuff about JavaCompiler and examining some examples.
Now I need to know where to place the file with the code to be compiled.
I was thinking of using f=new File("code.java") to create the file, but since this is all going to work in an applet in a website, what would be the best choice of path to save the file to? What would happen if I just leave it as the default current directory (where is it really stored)?
Again, thanks very much :)
- 07-29-2011, 06:51 PM #4
The JavaCompiler can compile from a String. No external file needed.
You'll need the JDK installed for this to work.
- 07-29-2011, 08:33 PM #5
Thanks, so no file is needed.
Still, I'm a bit lost. I'm reading stuff I find on the internet but they all talk about a JavaFileObject.
I'm quite a newbie... do you know of any reliable source where I can read how to compile from a String without generating any files?
- 07-29-2011, 08:40 PM #6
Sorry, I don't have any links to sites describing how to use the JavaCompiler class.
- 07-29-2011, 10:27 PM #7
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,400
- Blog Entries
- 7
- Rep Power
- 17
When people rob a bank they get a penalty; when banks rob people they get a bonus.
- 07-30-2011, 05:46 PM #8
Thanks. After quite a few hours reading here and there, I came up with some code to put in the method my "compile" button calls.
I understand all my code does, but I'm getting an error and I just don't know what else to try. Maybe someone here can help me isolate the bug? This is my "compile" method:
private void compile(String code){
SimpleJavaFileObject fileObject = new JavaObjectString ("exercise.java", code);
JavaFileObject javaFileObjects[] = new JavaFileObject[]{fileObject} ;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
StandardJavaFileManager stdFileManager = compiler.getStandardFileManager(diagnostics, null, null);
Iterable<? extends JavaFileObject> compilationUnits = Arrays.asList(javaFileObjects);
CompilationTask compilerTask = compiler.getTask(null, stdFileManager, diagnostics, null, null, compilationUnits) ;
boolean status = compilerTask.call();
if (!status){
for (Diagnostic diagnostic : diagnostics.getDiagnostics()){
System.out.format("Error on line %d in %s", diagnostic.getLineNumber(), diagnostic);
}
}
try {
stdFileManager.close() ;//Close the file manager
} catch (IOException e) {
e.printStackTrace();
}
}
I also have a JavaObjectString class with this constructor:
public JavaObjectString(String className, String code){
super(URI.create("string:///" +className.replace('.', '/') + Kind.SOURCE.extension), Kind.SOURCE);
this.setCode(code);
this.setName(className);
}
And the error message looks like this:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Ventana.compile(Ventana.java:93)
at Ventana.btnCompile_OnClick(Ventana.java:134)
at Ventana.access$0(Ventana.java:132)
at Ventana$1.mouseClicked(Ventana.java:143)
at java.awt.AWTEventMulticaster.mouseClicked(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent( Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(U nknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unkno wn Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilter s(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(U nknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarch y(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
("Ventana" is the class containing all of this code).
When I click on Ventana.java:93 I get taken to the StandardJavaFileManager line. But I have no clue of what is wrong there.
Any hints?
Thanks :)
- 07-30-2011, 05:51 PM #9
What variable is null on line 93?Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
at Ventana.compile(Ventana.java:93)
What is the value of the variable: compiler?
Is the JDK available when the code is executed? That is where it gets the compiler from.
- 07-30-2011, 07:38 PM #10
Thanks, Norm.
Apparently, my "compiler" variable is the one with the null value.
I don't know why it turns out null, since I've seen in several places the line JavaCompiler compiler = ToolProvider.getSystemJavaCompiler(); so I'm guessing that's the right assignment to do.
I have JDK 1.6.0.26 installed (I had the previous version but just updated it, and the error continues to show up).
- 07-30-2011, 07:57 PM #11
Are you using the java program that is included with the JDK? It has a jre folder.
This is the command line I use to execute java:
D:\Java\jdk1.6.0_25\jre\bin\java.exe
- 07-30-2011, 08:10 PM #12
I'm using Eclipse to write my code, and running the program in this IDE.
But I checked and I have a C:\program files\Java\jdk1.6.0_19\jre\bin\java.exe file. Is there anything I'm doing wrong here?
- 07-30-2011, 08:20 PM #13
Sorry, I have no idea how your IDE works.
Can you take your program outside of the IDE and execute it there using the java command you showed?
Or can you change the commandline that your IDE uses to execute your program to use the path to java.exe that you posted?
- 07-30-2011, 10:07 PM #14
I tried running it with the java.exe command, but since this is an applet (thus, it has no main() method), it wouldn't run because the main() method is missing.
But I finally got to run it with the IDE. It was pointing to a different java.exe path.
Now my JavaCompiler always says there's an error on line 1 despite of me entering valid code, but I'll try to figure out what that is about.
A big THANKS to you both!Last edited by anarelle; 07-30-2011 at 10:12 PM.
Similar Threads
-
Help! set dinamically visibility of panels
By unkus_nob in forum JavaFXReplies: 5Last Post: 06-21-2011, 09:06 PM -
simple code not compiling
By cliffh in forum New To JavaReplies: 8Last Post: 09-30-2010, 05:47 AM -
my code compiling but not running
By girishkumar in forum New To JavaReplies: 16Last Post: 03-16-2010, 04:45 PM -
Help me out in compiling the source code
By aks.nitw in forum Advanced JavaReplies: 3Last Post: 10-17-2008, 08:33 AM -
Trouble compiling code
By waelhelbawi in forum New To JavaReplies: 1Last Post: 05-12-2008, 04:25 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks