4

I am working on an Editor application where I can compile and run c,cpp and Java file.I am developing this application using java programming language.I am developing it in Eclipse. I am able to create new files(c,cpp and java) on specific locations and also I am able to save file to different-2 locations. And for execution I am using following methods.

String compileFileCommand = "javac "+fileName;

Process compile_process = new ProcessBuilder(compileFileCommand).redirectErrorStream(true).start();
compile_process.waitFor();

BufferedReader reader=new BufferedReader(new InputStreamReader(compile_process.getInputStream())); 
String line=reader.readLine(); 
while(line!=null) { 
    System.out.println(line); 
    line=reader.readLine(); 
} 

My problem is that I am not able to compile files from their corresponding locations.

Always giving Exception

java.io.IOException: error=2, No such file or directory 

Please tell me how can I compile and run all c,c++ & java files. Please also give me any other suggestion for my application.

Edited .. I have used these two methods for compiling and running.On Compiling it creates a class file in case of Java.But all the time I am getting null from InputStreams(both getErrorStream() and getInputStream()).

void compileJavaFile(String fileName)
    {
        String compileFileCommand = "javac " + fileName;
        try
        {
            System.out.println("Executing Java File");

            Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);

            String line = "";
            BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream()));
            BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream()));
            while ((line = bri.readLine()) != null)
            {
                System.out.println(line);
            }
            bri.close();
            while ((line = bre.readLine()) != null)
            {
                System.out.println(line);
            }
            bre.close();
            compileProcess.waitFor();
            System.out.println("Done.");
        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

    void runJavaFile(String fileName)
    {
        String runFileCommand = "java " + fileName.split(".java")[0];
        try
        {
            System.out.println("runFileCommand : " + runFileCommand);
            System.out.println("Running Java File");

            Process runProcess = Runtime.getRuntime().exec(runFileCommand);

            BufferedReader reader = new BufferedReader(new InputStreamReader(runProcess.getInputStream()));
            String line = reader.readLine();
            System.out.println("line = " + line);
            while (line != null)
            {
                System.out.println(line);
                line = reader.readLine();
            }

        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

And for C and C++ I am using.

void compileCFile(String fileName)
    {
        String compileFileCommand = "gcc " + fileName;

        resultString = "";
        try
        {
            System.out.println("Compiling C File");

            Process processCompile = Runtime.getRuntime().exec(compileFileCommand);

            BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
            String errorCompile = brCompileError.readLine();
            if (errorCompile != null)
                System.out.println("Error Compiler = " + errorCompile);

            resultString += errorCompile +"\n";

            BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
            String outputCompile = brCompileRun.readLine();
            if (outputCompile != null)
                System.out.println("Output Compiler = " + outputCompile);

            resultString += outputCompile +"\n";

        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

    void runCFile(String fileName)
    {
        String runFileCommand = "./" + fileName.split(".c")[0];

        try
        {

            System.out.println("Running C File");

            Process processRun = Runtime.getRuntime().exec(runFileCommand);

            BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
            String errorRun = brRun.readLine();
            if (errorRun != null)
                System.out.println("Error Run = " + errorRun);

            BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
            String outputRun = brResult.readLine();
            if (outputRun != null)
                System.out.println("Output Run = " + outputRun);

        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

    void compileCPPFile(String fileName)
    {
        String compileFileCommand = "g++ " + fileName;
        try
        {

            System.out.println("Compiling CPP File");

            Process processCompile = Runtime.getRuntime().exec(compileFileCommand);

            BufferedReader brCompileError = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
            String errorCompile = brCompileError.readLine();
            if (errorCompile != null)
                System.out.println("Error Compiler = " + errorCompile);

            resultString += errorCompile +"\n";

            BufferedReader brCompileRun = new BufferedReader(new InputStreamReader(processCompile.getErrorStream()));
            String outputCompile = brCompileRun.readLine();
            if (outputCompile != null)
                System.out.println("Output Compiler = " + outputCompile);

            resultString += outputCompile +"\n";

        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

    void runCPPFile(String fileName)
    {
        String runFileCommand = "./" + fileName.split(".cpp")[0];

        try
        {
            System.out.println("Running CPP File");

            Process processRun = Runtime.getRuntime().exec(runFileCommand);

            BufferedReader brRun = new BufferedReader(new InputStreamReader(processRun.getErrorStream()));
            String errorRun = brRun.readLine();
            if (errorRun != null)
                System.out.println("Error Run = " + errorRun);

            BufferedReader brResult = new BufferedReader(new InputStreamReader(processRun.getInputStream()));
            String outputRun = brResult.readLine();
            if (outputRun != null)
                System.out.println("Output Run = " + outputRun);

        } catch (Exception e)
        {
            // TODO: handle exception
            System.out.println("Exception ");
            System.out.println(e.getMessage());
        }
    }

In case of C and C++ it show error like

g++: /media/disk/eclipse/\/UniversalIDE/CPP/firstCPP: No such file or directory

Please give me solution for my problems ..

3
  • Two questions: (1) which code line throws that exception? (2) are you sure javac can be found? Commented Jan 25, 2012 at 7:41
  • Exception is thrown at new ProcessBuilder(...). Please suggest anything you can. Commented Jan 25, 2012 at 8:33
  • That line in fact has three statements. So it could be worthwhile to find out exactly which of these throws the exception. And also (as per my 2nd question above) are you sure javac can be found? It could very well be that this is what cannot be found. Try changing compileFileCommand to just javac and see what happens. Commented Jan 25, 2012 at 8:54

2 Answers 2

1

Please try following,

Process p = Runtime.getRuntime().exec(command);

command is a string you pass. in command you can pass "javac Test.java" to compile your java file & just like that you can use other commands.

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

6 Comments

Will it compile from any dir I specify?
yes. but you need to give Dir path properly. Please accept the answer if it is useful for you. so that it can be useful for others.
Ok ,In case of Java it is compiling from any Dir and also creating a class file.But it gives null in response.When I use String line = ""; BufferedReader bri = new BufferedReader(new InputStreamReader(compileProcess.getInputStream())); BufferedReader bre = new BufferedReader(new InputStreamReader(compileProcess.getErrorStream())); while ((line = bri.readLine()) != null) { System.out.println(line); } bri.close(); while ((line = bre.readLine()) != null) { System.out.println(line); }
I think you need to sleep after this line Process compileProcess = Runtime.getRuntime().exec(compileFileCommand);
@Lucifer in my case file is compiled but not run. see this image
|
1

replace the line

String runFileCommand = "./" + fileName.split(".c")[0];

with

String runFileCommand = "./a.out";

Comments

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.