Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 25-12-2008, 05:03 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2008
Posts: 2
Smile Online Compiler


I need help in building my application.
My application allows user to enter C/C++/java code.when they add their code they should be able to check the code whether it runs.So it requires a online compiler so that i can send the code to tat compiler and get the output.
or if there is any jar file which can be attached with the proj is also fine.
My application is web application..i'm building it with servlets and JSP..

Can anyone give me some suggestion.. Atleast if it compiles C code its enough..
bhags21 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 25-12-2008, 08:27 PM   #2 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Online Compiler

Here's a quickie online C compiler. It assumes that your server has gcc installed.

JSP Page:
Code:
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Online Compiler</title>
    </head>
    <body>
        <h1>Enter code</h1>
        <form method="post" action="CompilerServlet">
            <textarea name="code" cols="80" rows="25"></textarea>
            <br>
            <input type="submit" value="Compile Code"></input>
        </form>
    </body>
</html>
Servlet Code:
Code:
public class CompilerServlet extends HttpServlet {

    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/plain");
        PrintWriter out = response.getWriter();
        File sourceFile = File.createTempFile("src", ".c");
        File execFile = File.createTempFile("exec", ".out");

        // Write to temporary source file
        PrintWriter sourceWriter = new PrintWriter(sourceFile);
        sourceWriter.println(request.getParameter("code"));
        sourceWriter.close();

        // Compile
        Process compilerProc = Runtime.getRuntime().exec("gcc -Wall -o "
                + execFile.getAbsolutePath()
                + " " + sourceFile.getAbsolutePath());

        // Capture Compiler output
        BufferedReader compilerReader = new BufferedReader(new InputStreamReader(compilerProc.getErrorStream()));
        out.println("Compiler output:\n");
        String line;
        while ((line = compilerReader.readLine()) != null) {
            out.println(line);
        }

        compilerReader.close();
        sourceFile.delete();
        execFile.delete();
        out.close();
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }
}
Warning: This is just a quick hack. The servlet code may leave streams unclosed at times.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 25-12-2008, 09:20 PM   #3 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default Re: Online Compiler

I wouldn't recommend you do this without going through the code and eliminating harmful procedures. Something like what http://codepad.org does.
__________________
Harsh J
www.harshj.com
QwertyManiac is offline  
Old 25-12-2008, 10:32 PM   #4 (permalink)
CAFEBABE
 
chandru.in's Avatar
 
Join Date: Mar 2008
Location: Bangalore
Posts: 474
Default Re: Online Compiler

^^ The code I provided just compiles and does not execute the submitted code (unlike codepad.org), so it basically is just a syntax check before posting and doesn't post any risk, even if the submitted source code is malicious.
__________________
Chandru

http://tuxychandru.blogspot.com
chandru.in is offline  
Old 20-01-2009, 02:45 PM   #5 (permalink)
Right Off the Assembly Line
 
Join Date: Dec 2008
Posts: 2
Default Re: Online Compiler

Thanks a lot..i implemented the compiler
bhags21 is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
C compiler gamirahult Open Source 5 12-10-2008 02:46 PM
need a c-compiler! direfulsky Chit-Chat 2 30-09-2007 10:52 AM
Help with C compiler.. plsoft Open Source 5 14-04-2006 02:41 PM
C compiler Generic Superhero QnA (read only) 6 12-01-2006 06:22 PM
c++ compiler shivi4 Programming 1 16-12-2005 03:55 PM

 
Latest Threads
- by gforz
- by soumya
- by Sujeet
- by icebags
- by Charan

Advertisement




All times are GMT +5.5. The time now is 03:04 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2