1
import java.io.*;

public class chk 
{
    String className;
    String command,command1,command2;
    public String getMsg(String fileName,File Path1) 
    {
        String dir;
        command = "tcc "+fileName;
        String output = executeCommand(command,Path1);
        if(output.compareTo("")==0)             
            output = "Compilation Successfull!!";
        return output;
    }

    private String executeCommand(String command,File Path1) 
    {
        StringBuffer output = new StringBuffer();
        Process p;
        try 
        {
            p = Runtime.getRuntime().exec(command,null,Path1);
            p.waitFor();
            BufferedReader reader1 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
            BufferedReader reader2 = new BufferedReader(new InputStreamReader(p.getInputStream()));
            String line = "";           
            while ((line = reader1.readLine())!= null) 
            {
                output.append(line + "\n");
            }
            while ((line = reader2.readLine())!= null) 
            {
                output.append(line + "\n");
            }
        } catch (Exception e) 
        {
            e.printStackTrace();
        }
        return output.toString();
    }

    public static void main(String args[])throws IOException
    {
        String x;
        File dir=new File("D:\\test");
        chk ob = new chk();
        x = ob.getMsg("hello.c",dir);
        System.out.println("OUtput : "+x);
    }
}

ERROR

enter image description here

I am trying to compile a C code from a java class. I am using Turbo C/C++ compiler and have also set its path i.e "C:/TC/bin" even my programs are compiling when i compile them directly from command prompt but when i am trying to compile it with a java file the following error message appears. HELP!!

4
  • @TonythePony which compiler should i use to run on windows ? Commented Feb 9, 2014 at 20:26
  • MinGW is popular (and free): mingw.org Commented Feb 9, 2014 at 20:29
  • You're less likely to run into any environment issues, since you don't need the MS-DOS subsystem, but there might be other problems. Commented Feb 9, 2014 at 20:33
  • 2
    Oh dear, why are you still using a 16-bit compiler in 2014!? Commented Feb 9, 2014 at 20:34

1 Answer 1

2

Your code seems fine after adding a missing import: import java.io.*; however it seems you're using a very old compiler that's made for 16-bit /DOSWindows and that is probably the reason it won't work for you.

Try using a modern compiler like GCC instead, for Windows you'll want to use MinGW which is a version of the GCC compiler built for Windows. I tried your code using GCC 4.8.2 (MinGW) and it worked fine.

Another alternative would be to use Microsofts Visual C++ compiler which also can be run from the command line, (but be aware that it only supports C89, with some features from later standards).

Sign up to request clarification or add additional context in comments.

2 Comments

@rick Through the MinGW project, yes.
@rick To use GCC with your program you'll need to change the command = "tcc "+fileName; to command = "gcc "+fileName;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.