Results 1 to 14 of 14
  1. #1
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default 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"

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    take the code written by the user and try to compile it
    See the JavaCompiler class for one option on compiling from within a java program. You'll need the JDK installed for this to work.

  3. #3
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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 :)

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    The JavaCompiler can compile from a String. No external file needed.

    You'll need the JDK installed for this to work.

  5. #5
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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?

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Sorry, I don't have any links to sites describing how to use the JavaCompiler class.

  7. #7
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,371
    Blog Entries
    7
    Rep Power
    20

    Default

    Quote Originally Posted by Norm View Post
    Sorry, I don't have any links to sites describing how to use the JavaCompiler class.
    It's all in the API documentation for the JavaCompiler interface. There's even an example of a SimpleJavaObject implementation that delivers the source given a String (i.e. no actual file is needed).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  8. #8
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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 :)

  9. #9
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
    at Ventana.compile(Ventana.java:93)
    What variable is null on line 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.

  10. #10
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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).

  11. #11
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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

  12. #12
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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?

  13. #13
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,306
    Rep Power
    25

    Default

    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?

  14. #14
    anarelle's Avatar
    anarelle is offline Member
    Join Date
    Mar 2011
    Location
    Argentina
    Posts
    21
    Rep Power
    0

    Default

    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

  1. Help! set dinamically visibility of panels
    By unkus_nob in forum JavaFX
    Replies: 5
    Last Post: 06-21-2011, 09:06 PM
  2. simple code not compiling
    By cliffh in forum New To Java
    Replies: 8
    Last Post: 09-30-2010, 05:47 AM
  3. my code compiling but not running
    By girishkumar in forum New To Java
    Replies: 16
    Last Post: 03-16-2010, 04:45 PM
  4. Help me out in compiling the source code
    By aks.nitw in forum Advanced Java
    Replies: 3
    Last Post: 10-17-2008, 08:33 AM
  5. Trouble compiling code
    By waelhelbawi in forum New To Java
    Replies: 1
    Last Post: 05-12-2008, 04:25 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •