Monday, 15 July 2013

Is it possible to get the diagnostics printed out for a C program in Java? -



Is it possible to get the diagnostics printed out for a C program in Java? -

i want compile next c codes java programme (the syntax errors have been intentionally left):

/* hello world programme */ #include<stdio.h> main() { printf("hello world") }

i want java programme compile c file , give diagnostics in console. example: line 4 ; expected , line 5 invalid syntax...

i have installed codeblock mingw compiler , have been able codes compiled , got errors printed out.

have @ did:

import java.io.bufferedreader; import java.io.ioexception; import java.io.inputstreamreader; public class c_compile { public static void main(string args[]){ string ret = compile(); system.out.println(ret); } public static string compile() { string log=""; string mydirectory = "c:\\"; seek { string s= null; //change string compilers location process p = runtime.getruntime().exec("cmd /c gcc hello.c", null, new java.io.file(mydirectory)); bufferedreader stderror = new bufferedreader(new inputstreamreader(p.geterrorstream())); boolean error=false; log+="\n....\n"; while ((s = stderror.readline()) != null) { log+=s; error=true; log+="\n"; } if(error==false) log+="compilation successful !!!"; } grab (ioexception e) { e.printstacktrace(); } homecoming log; } public int runprogram() { int ret = -1; seek { runtime rt = runtime.getruntime(); process proc = rt.exec("cmd.exe /c start a.exe"); proc.waitfor(); ret = proc.exitvalue(); } grab (throwable t) { t.printstacktrace(); homecoming ret; } homecoming ret; } }

i got next results:

.... hello.c: in function 'main': hello.c:9:1: error: expected ';' before '}' token

but wanted know if it's possible diagnostic listener. want line numbers, positions, types of syntax errors later input in array farther processing.

below wanted have in java:

http://prntscr.com/sgvya

javacompiler compiler = toolprovider.getsystemjavacompiler(); list<captureerrors> myerrors = new arraylist<captureerrors>(); mydiagnosticlistener listener = new mydiagnosticlistener(); standardjavafilemanager filemanager = compiler.getstandardfilemanager(listener, null, null); string filetocompile = "c:\\users\\user\\desktop\\myc.java"; iterable fileobjects = filemanager.getjavafileobjectsfromstrings(arrays.aslist(filetocompile)); compilationtask task = compiler.gettask(null, filemanager, listener, null, null, fileobjects); boolean result = task.call(); if(result == true){ system.out.println("compilation has succeeded"); } myerrors = listener.getlistoferrors(); (captureerrors e : myerrors) { system.out.println("code: " + e.getcode()); system.out.println("kind: " + e.getkind()); system.out.println("line number: " + e.getlinenumber()); // system.out.println("message: "+ e.getmessage(locale.english)); //system.out.println("source: " + e.getsource()); system.out.println("end position: "+ e.getendposition()); system.out.println("position: "+ e.getposition()); system.out.println("\n"); }

instead of runtime.exec() utilize processbuilder , redirecterrorstream(true) on it. way able capture both stdout , stderr compiler output. , instead of invoking via cmd /c invoke gcc executable straight processbuilder. check exit code status kid process. non 0 exitcode means error in kid process. utilize regex extract error messages , line numbers output stream (which contain stderr output of gcc.exe).

java c eclipse compiler-construction mingw

No comments:

Post a Comment